UNPKG

@grouparoo/core

Version:
56 lines (55 loc) 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionInitializer = void 0; const actionhero_1 = require("actionhero"); const Session_1 = require("../models/Session"); const authentication_1 = require("../modules/middleware/authentication"); class SessionInitializer extends actionhero_1.Initializer { constructor() { super(); this.name = "session"; this.startPriority = 100; } async initialize() { actionhero_1.api.session = { prefix: "session", ttl: 1000 * 60 * 60 * 24 * 30, load: async (connection) => { const session = await Session_1.Session.findOne({ where: { fingerprint: connection.fingerprint }, }); if (!session) return null; return session; }, create: async (connection, teamMember) => { await Session_1.Session.destroy({ where: { fingerprint: connection.fingerprint }, }); const session = await Session_1.Session.create({ fingerprint: connection.fingerprint, teamMemberId: teamMember.id, expiresAt: new Date().getTime() + actionhero_1.api.session.ttl, }); await teamMember.update({ lastLoginAt: new Date() }); return session; }, destroy: async (connection) => { await Session_1.Session.destroy({ where: { fingerprint: connection.fingerprint }, }); }, }; } async start() { if (!actionhero_1.config.general.serverToken || actionhero_1.config.general.serverToken === "") { throw new Error("SERVER_TOKEN environment variable missing"); } actionhero_1.action.addMiddleware(authentication_1.AuthenticatedActionMiddleware); actionhero_1.action.addMiddleware(authentication_1.OptionallyAuthenticatedActionMiddleware); actionhero_1.chatRoom.addMiddleware(authentication_1.ChatRoomMiddleware); actionhero_1.api.params.globalSafeParams.push("csrfToken"); actionhero_1.api.params.globalSafeParams.push("apiKey"); } } exports.SessionInitializer = SessionInitializer;