UNPKG

@accounts/typeorm

Version:
375 lines 13.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AccountsTypeorm = void 0; const tslib_1 = require("tslib"); const typeorm_1 = require("typeorm"); const User_1 = require("./entity/User"); const UserEmail_1 = require("./entity/UserEmail"); const UserService_1 = require("./entity/UserService"); const UserSession_1 = require("./entity/UserSession"); const types_1 = require("./types"); const graphql_modules_1 = require("graphql-modules"); let AccountsTypeorm = class AccountsTypeorm { options; connection; UserEntity; UserEmailEntity; UserServiceEntity; UserSessionEntity; userRepository; emailRepository; serviceRepository; sessionRepository; constructor(options, connection, UserEntity = User_1.User, UserEmailEntity = UserEmail_1.UserEmail, UserServiceEntity = UserService_1.UserService, UserSessionEntity = UserSession_1.UserSession) { this.options = options; this.connection = connection; this.UserEntity = UserEntity; this.UserEmailEntity = UserEmailEntity; this.UserServiceEntity = UserServiceEntity; this.UserSessionEntity = UserSessionEntity; const setRepositories = () => { if (this.connection) { this.userRepository = this.connection.getRepository(this.UserEntity); this.emailRepository = this.connection.getRepository(this.UserEmailEntity); this.serviceRepository = this.connection.getRepository(this.UserServiceEntity); this.sessionRepository = this.connection.getRepository(this.UserSessionEntity); } else { this.userRepository = (0, typeorm_1.getRepository)(this.UserEntity, this.options.connectionName); this.emailRepository = (0, typeorm_1.getRepository)(this.UserEmailEntity, this.options.connectionName); this.serviceRepository = (0, typeorm_1.getRepository)(this.UserServiceEntity, this.options.connectionName); this.sessionRepository = (0, typeorm_1.getRepository)(this.UserSessionEntity, this.options.connectionName); } }; // direct or lazy support if (connection && !connection.isConnected) { connection.connect().then(setRepositories); } else { setRepositories(); } } async findUserByEmail(email) { const userEmail = await this.emailRepository.findOne({ where: { address: email.toLocaleLowerCase() }, cache: this.options.cache, }); if (userEmail) { return this.findUserById(userEmail.userId); } return null; } async findUserByUsername(username) { const user = await this.userRepository.findOne({ where: { username }, cache: this.options.cache, }); if (user) { return user; } return null; } async findUserById(userId) { const user = await this.userRepository.findOne({ where: { id: userId }, cache: this.options.cache, }); if (!user) { // throw new Error('User not found'); return null; } return user; } async findUserByResetPasswordToken(token) { const service = await this.serviceRepository.findOne({ where: { name: 'password.reset', token, }, cache: this.options.cache, }); if (service) { return this.findUserById(service.userId); } return null; } async findUserByEmailVerificationToken(token) { const service = await this.serviceRepository.findOne({ where: { name: 'email.verificationTokens', token, }, cache: this.options.cache, }); if (service) { return this.findUserById(service.userId); } return null; } async createUser(createUser) { const { username, email, password, ...otherFields } = createUser; const user = new this.UserEntity(); if (email) { const userEmail = new this.UserEmailEntity(); userEmail.address = email.toLocaleLowerCase(); userEmail.verified = false; await this.emailRepository.save(userEmail); user.emails = [userEmail]; } if (password) { const userService = new this.UserServiceEntity(); userService.name = 'password'; userService.options = { bcrypt: password }; await this.serviceRepository.save(userService); user.allServices = [userService]; } if (username) { user.username = username; } Object.assign(user, otherFields); await this.userRepository.save(user); return user.id; } async setUsername(userId, newUsername) { const user = await this.findUserById(userId); if (user) { user.username = newUsername; await this.userRepository.save(user); return; } throw new Error('User not found'); } async findUserByServiceId(serviceName, serviceId) { const service = await this.serviceRepository.findOne({ where: { name: serviceName, serviceId, }, cache: this.options.cache, }); if (service) { return this.findUserById(service.userId); } return null; } async getService(userId, serviceName) { const user = await this.findUserById(userId); if (user) { const service = user.allServices.find((s) => s.name === serviceName); if (service) { return service; } } return null; } async setService(userId, serviceName, data, token) { let service = await this.getService(userId, serviceName); if (!service) { const user = await this.findUserById(userId); if (user) { service = new this.UserServiceEntity(); service.name = serviceName; service.user = user; } } const { id = null, ...options } = data; if (service) { service.options = options; if (id) { service.serviceId = id; } if (token) { service.token = token; } await this.serviceRepository.save(service); } } async unsetService(userId, serviceName) { const user = await this.findUserById(userId); if (user) { const service = user.allServices.find((s) => s.name === serviceName); if (service) { await this.serviceRepository.remove(service); } } } async findPasswordHash(userId) { const service = await this.getService(userId, 'password'); if (service) { return service.options.bcrypt; } return null; } async setPassword(userId, newPassword) { const user = await this.findUserById(userId); if (user) { await this.setService(userId, 'password', { bcrypt: newPassword }); await this.userRepository.update({ id: user.id }, {}); return; } throw new Error('User not found'); } async addResetPasswordToken(userId, email, token, reason) { await this.setService(userId, 'password.reset', { address: email.toLocaleLowerCase(), when: new Date().toJSON(), reason, }, token); } async addEmail(userId, newEmail, verified) { const user = await this.findUserById(userId); if (user) { const userEmail = new this.UserEmailEntity(); userEmail.user = user; userEmail.address = newEmail.toLocaleLowerCase(); userEmail.verified = verified; await this.emailRepository.save(userEmail); await this.userRepository.update({ id: user.id }, {}); return; } throw new Error('User not found'); } async removeEmail(userId, email) { const user = await this.findUserById(userId); if (user) { const userEmail = user.emails.find((s) => s.address === email.toLocaleLowerCase()); if (!userEmail) { throw new Error('Email not found'); } await this.emailRepository.remove(userEmail); await this.userRepository.update({ id: user.id }, {}); return; } throw new Error('User not found'); } async verifyEmail(userId, email) { const user = await this.findUserById(userId); if (user) { const userEmail = user.emails.find((s) => s.address === email.toLocaleLowerCase()); if (!userEmail) { throw new Error('Email not found'); } userEmail.verified = true; await this.emailRepository.save(userEmail); await this.unsetService(userId, 'email.verificationTokens'); await this.userRepository.update({ id: user.id }, {}); return; } throw new Error('User not found'); } async addEmailVerificationToken(userId, email, token) { await this.setService(userId, 'email.verificationTokens', { address: email.toLocaleLowerCase(), when: new Date(), }, token); } async removeAllResetPasswordTokens(userId) { await this.unsetService(userId, 'password.reset'); } async setUserDeactivated(userId, deactivated) { const user = await this.findUserById(userId); if (user) { user.deactivated = deactivated; await this.userRepository.save(user); } } async findSessionById(sessionId) { try { const session = await this.sessionRepository.findOne({ where: { id: sessionId }, cache: this.options.cache, }); if (session) { return session; } } catch (err) { // noop } return null; } async findSessionByToken(token) { const session = await this.sessionRepository.findOne({ where: { token }, cache: this.options.cache, }); if (!session) { return null; } return session; } async findUserByLoginToken(token) { const service = await this.serviceRepository.findOne({ where: { name: 'magicLink.loginTokens', token, }, cache: this.options.cache, }); if (service) { return this.findUserById(service.userId); } return null; } async addLoginToken(userId, email, token) { await this.setService(userId, 'magicLink.loginTokens', { address: email.toLocaleLowerCase(), when: new Date().toJSON(), }, token); } async removeAllLoginTokens(userId) { await this.unsetService(userId, 'magicLink.loginTokens'); } async createSession(userId, token, connection = {}, extra) { const user = await this.findUserById(userId); const session = new this.UserSessionEntity(); session.user = user; session.token = token; session.userAgent = connection.userAgent; session.ip = connection.ip; if (extra) { session.extra = extra; } session.valid = true; await this.sessionRepository.save(session); return session.id; } async updateSession(sessionId, connection) { const session = await this.findSessionById(sessionId); if (session) { session.userAgent = connection.userAgent; session.ip = connection.ip; await this.sessionRepository.save(session); } } async invalidateSession(sessionId) { const session = await this.findSessionById(sessionId); if (session) { session.valid = false; await this.sessionRepository.save(session); } } async invalidateAllSessions(userId, excludedSessionIds) { const selector = { userId }; if (excludedSessionIds && excludedSessionIds.length > 0) { selector.id = (0, typeorm_1.Not)((0, typeorm_1.In)(excludedSessionIds)); } await this.sessionRepository.update(selector, { valid: false, }); } }; exports.AccountsTypeorm = AccountsTypeorm; exports.AccountsTypeorm = AccountsTypeorm = tslib_1.__decorate([ (0, graphql_modules_1.Injectable)({ global: true, }), tslib_1.__param(0, (0, graphql_modules_1.Inject)(types_1.AccountsTypeORMConfigToken)), tslib_1.__param(1, (0, graphql_modules_1.Inject)(typeorm_1.Connection)), tslib_1.__param(2, (0, graphql_modules_1.Inject)(types_1.UserToken)), tslib_1.__param(3, (0, graphql_modules_1.Inject)(types_1.UserEmailToken)), tslib_1.__param(4, (0, graphql_modules_1.Inject)(types_1.UserServiceToken)), tslib_1.__param(5, (0, graphql_modules_1.Inject)(types_1.UserSessionToken)), tslib_1.__metadata("design:paramtypes", [Object, typeorm_1.Connection, Object, Object, Object, Object]) ], AccountsTypeorm); //# sourceMappingURL=typeorm.js.map