UNPKG

authenzify

Version:

server to manage authentication authorization of users and more

89 lines (74 loc) 2.11 kB
import { USER_SIGNED_UP_OR_IN_BY } from '../constant.js' import { verifyExistence } from '../util/util.js' const fnList = [ 'find', 'create', 'create', 'findOne', 'findOne', 'verifyUser', 'updateUser', 'findByPhone', 'setLastLogin', 'createGoogleUser', 'updateUserPermissions', ] export class UsersService { #iDalUsersService constructor(iDalUsersService) { verifyExistence(iDalUsersService, fnList) this.#iDalUsersService = iDalUsersService } verifyUser(user, verification) { return this.#iDalUsersService.verifyUser(user, verification) } async create(userDetails) { return this.#iDalUsersService.create(userDetails) } async createGoogleUser(userDetails) { return this.#iDalUsersService.createGoogleUser(userDetails) } async findOne(filter) { return this.#iDalUsersService.findOne(filter) } async setLastLogin({ id }, { lastLogin, signedInVia }) { return this.#iDalUsersService.setLastLogin( { id }, { lastLogin, signedInVia }, ) } async updateUser({ id }, userDetails) { return this.#iDalUsersService.updateUser({ id }, userDetails) } async updateUserPermissions({ id }, userDetails, permissions) { return this.#iDalUsersService.updateUserPermissions( { id }, userDetails, permissions, ) } async find(filter) { return this.#iDalUsersService.find(filter) } async findOrCreateByIdentifier({ identifier, signedUpVia }) { const existing = USER_SIGNED_UP_OR_IN_BY.EMAIL === signedUpVia ? await this.#iDalUsersService.findOne({ email: identifier }) : await this.#iDalUsersService.findByPhone({ phone: identifier }) if (existing) { return existing } const identifierWithFieldName = signedUpVia === USER_SIGNED_UP_OR_IN_BY.EMAIL ? { email: identifier } : { phone: identifier } const newUser = await this.#iDalUsersService.create({ password: '', isValid: false, salt: undefined, signedUpVia: signedUpVia, ...identifierWithFieldName, }) return newUser } }