UNPKG

@deepkit/framework

Version:

56 lines 2.15 kB
/* * Deepkit Framework * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt * * This program is free software: you can redistribute it and/or modify * it under the terms of the MIT License. * * You should have received a copy of the MIT License along with this program. */ /** * This is the default session object, that can be used in your application. * * If you want to receive the Session object you can simply use this Session class as dependency injection token. * However, this will always create a new session (creating a session id + store it in the session storage). * If you simply want to check whether a session exists (user has a valid authenticaton token/cookie), use * SessionHandler. * * If you need more fields, you can create your own Session class. Make sure to * annotate all fields using `@t` of @deepkit/type, since the whole object is serialized * in a session storage (either memory, local file system, or external databases like redis/mysql/etc). */ export class Session { constructor(id, username) { this.id = id; this.username = username; this.data = {}; this.createdAt = new Date; this.groups = []; } isAnonymous() { return undefined === this.username; } } Session.__type = ['data', function () { return {}; }, 'createdAt', function () { return new Date; }, 'groups', function () { return []; }, 'id', 'username', 'constructor', 'isAnonymous', 'Session', 'P&"LM3!>"T3#>$&F3%>&P&2\':9&2(8:9"0)P)0*5w+']; /** * */ export class SessionHandler { setSession(session) { this.session = session; } hasSession() { return this.session !== undefined; } getSessionOrUndefined() { return this.session; } getSession() { if (!this.session) { throw new Error('No session loaded'); } return this.session; } } SessionHandler.__type = [() => Session, 'session', () => Session, 'setSession', 'hasSession', () => Session, 'getSessionOrUndefined', () => Session, 'getSession', 'SessionHandler', 'P7!3"8<PPP7#-J2""0$P)0%PPP7&-J0\'PP7(0)5w*']; //# sourceMappingURL=session.js.map