@deepkit/framework
Version:
61 lines • 2.34 kB
JavaScript
/*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionHandler = exports.Session = void 0;
/**
* 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).
*/
class Session {
constructor(id, username) {
this.id = id;
this.username = username;
this.data = {};
this.createdAt = new Date;
this.groups = [];
}
isAnonymous() {
return undefined === this.username;
}
}
exports.Session = Session;
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+'];
/**
*
*/
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;
}
}
exports.SessionHandler = SessionHandler;
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
;