LocChat/server/providers/services/users.service.ts

36 lines
907 B
TypeScript
Raw Normal View History

2021-11-16 21:14:46 -05:00
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import bcrypt from 'bcrypt';
import { User } from '../../entities/user.entity';
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private usersRespository: Repository<User>,
) {}
findBy(options: Record<string, any>) {
return this.usersRespository.findOne(options);
}
find(id: number) {
return this.usersRespository.findOne(id);
}
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) {
const user = await this.usersRespository.findOne({ email });
2021-11-20 20:18:58 -05:00
if (!user) return { verified: false, user: null };
const verified: boolean = await bcrypt.compare(
password,
user.password_hash,
);
return { verified, user };
2021-11-16 21:14:46 -05:00
}
}