UNPKG

@grouparoo/core

Version:
92 lines (91 loc) 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionDestroy = exports.SessionView = exports.SessionCreate = void 0; const actionhero_1 = require("actionhero"); const OAuthRequest_1 = require("../models/OAuthRequest"); const authenticatedAction_1 = require("../classes/actions/authenticatedAction"); const clsAction_1 = require("../classes/actions/clsAction"); const TeamMember_1 = require("../models/TeamMember"); const errors_1 = require("../modules/errors"); class SessionCreate extends clsAction_1.CLSAction { constructor() { super(...arguments); this.name = "session:create"; this.description = "to create a session and sign in"; this.inputs = { email: { required: true }, password: { required: false }, requestId: { required: false }, }; this.outputExample = {}; } isWriteTransaction() { return true; } async runWithinTransaction({ connection, params, }) { if (!params.password && !params.requestId) { throw new errors_1.Errors.AuthenticationError(`password or an oAuth requestId is required`); } const teamMember = await TeamMember_1.TeamMember.findOne({ where: { email: params.email.toLocaleLowerCase() }, }); if (!teamMember) { throw new errors_1.Errors.AuthenticationError("team member not found"); } if (params.password) { const match = await teamMember.checkPassword(params.password); if (!match) throw new errors_1.Errors.AuthenticationError("password does not match"); } else { const oauthRequest = await OAuthRequest_1.OAuthRequest.findOne({ where: { id: params.requestId, consumed: false }, }); if (!oauthRequest) throw new errors_1.Errors.AuthenticationError(`cannot find OAuthRequest ${params.requestId}`); const identity = oauthRequest.identities.find((i) => i.email === teamMember.email); if (!identity) throw new errors_1.Errors.AuthenticationError(`${teamMember.email} was not returned in oAuth request ${oauthRequest.id}`); await oauthRequest.update({ consumed: true }); } const session = await actionhero_1.api.session.create(connection, teamMember); return { success: true, teamMember: await teamMember.apiData(), csrfToken: session.id, }; } } exports.SessionCreate = SessionCreate; class SessionView extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "session:view"; this.description = "to view session information"; this.permission = { topic: "*", mode: "read" }; this.outputExample = {}; } async runWithinTransaction({ connection, session: { teamMember }, }) { const session = await actionhero_1.api.session.load(connection); if (!session) throw new Error("session not found"); return { teamMember: await teamMember.apiData(), csrfToken: session.id, }; } } exports.SessionView = SessionView; class SessionDestroy extends actionhero_1.Action { constructor() { super(...arguments); this.name = "session:destroy"; this.description = "to destroy a session and sign out"; this.outputExample = {}; } async run({ connection }) { await actionhero_1.api.session.destroy(connection); return { success: true }; } } exports.SessionDestroy = SessionDestroy;