import bcrypt from 'bcrypt' ; import { Injectable } from '@nestjs/common' ; import { UserService } from '../user/user.service' ; import { User } from '../../entities/user.entity' ; @ Injectable ( ) export class AuthService { constructor ( private readonly userService ) { } public async registerUserIfNotExist ( name , email , password ) { let user = await this . userService . findOneByEmail ( email ) ; if ( user && await bcrypt . compare ( password , user . password ) ) { const { password , ... result } = user ; return result ; } user = await this . userService . save ( await this . userService . create ( { name , email , password , } ) ) ; if ( user ) { const { password , ... result } = user ; return result ; } return null ; } public async validateUser ( email , password ) { const user = await this . userService . findOneByEmail ( email ) ; if ( user && await bcrypt . compare ( password , user . password ) ) { const { password , ... result } = user ; return result ; } return null ; } }