UNPKG

@wepublish/api

Version:
85 lines 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthenticationService = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const client_1 = require("@prisma/client"); const auth_session_1 = require("./auth-session"); const api_1 = require("../../../user-api/src"); let AuthenticationService = exports.AuthenticationService = class AuthenticationService { constructor(prisma) { this.prisma = prisma; } getSessionByToken(token) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const [tokenMatch, session] = yield Promise.all([ this.prisma.token.findFirst({ where: { token } }), this.prisma.session.findFirst({ where: { token } }) ]); if (tokenMatch) { return { type: auth_session_1.AuthSessionType.Token, id: tokenMatch.id, name: tokenMatch.name, token: tokenMatch.token, roles: yield this.prisma.userRole.findMany({ where: { id: { in: tokenMatch.roleIDs } } }) }; } if (session) { const user = yield this.prisma.user.findUnique({ where: { id: session.userID }, select: api_1.unselectPassword }); if (!user) { return null; } return { type: auth_session_1.AuthSessionType.User, id: session.id, token: session.token, createdAt: session.createdAt, expiresAt: session.expiresAt, user, roles: yield this.prisma.userRole.findMany({ where: { id: { in: user.roleIDs } } }) }; } return null; }); } isSessionValid(session) { if (!session) { return false; } if (session.type === auth_session_1.AuthSessionType.User) { return session.expiresAt > new Date(); } return true; } }; exports.AuthenticationService = AuthenticationService = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__metadata("design:paramtypes", [client_1.PrismaClient]) ], AuthenticationService); //# sourceMappingURL=authentication.service.js.map