2021-11-16 21:14:46 -05:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { Repository } from 'typeorm';
|
2021-11-20 21:34:10 -05:00
|
|
|
import * as bcrypt from 'bcrypt';
|
2021-11-16 21:14:46 -05:00
|
|
|
import { User } from '../../entities/user.entity';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UsersService {
|
|
|
|
constructor(
|
|
|
|
@InjectRepository(User)
|
|
|
|
private usersRespository: Repository<User>,
|
|
|
|
) {}
|
|
|
|
|
2021-11-23 16:04:12 -05:00
|
|
|
findBy(options: Record<string, any>, relations: string[] = []) {
|
|
|
|
return this.usersRespository.findOne(options, { relations });
|
2021-11-16 21:14:46 -05:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:04:12 -05:00
|
|
|
find(id: number, relations: string[] = []) {
|
|
|
|
return this.usersRespository.findOne(id, { relations });
|
2021-11-16 21:14:46 -05:00
|
|
|
}
|
|
|
|
|
2021-11-20 20:18:58 -05:00
|
|
|
create(user: User) {
|
|
|
|
return this.usersRespository.save(user);
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:14:46 -05:00
|
|
|
async verify(email: string, password: string) {
|
2021-11-23 16:04:12 -05:00
|
|
|
const user = await this.usersRespository.findOne({ email }, { relations: ['refreshTokens'] });
|
2021-11-20 20:18:58 -05:00
|
|
|
if (!user) return { verified: false, user: null };
|
2021-11-23 16:04:12 -05:00
|
|
|
const verified: boolean = await bcrypt.compare(password, user.passwordHash);
|
2021-11-22 16:21:53 -05:00
|
|
|
return { verified, user: verified ? user : null };
|
2021-11-16 21:14:46 -05:00
|
|
|
}
|
|
|
|
}
|