src/module/auth/service/auth.service.ts
Methods |
|
constructor(userRepository: Repository
|
||||||
Defined in src/module/auth/service/auth.service.ts:10
|
||||||
Parameters :
|
Async authenticate |
authenticate(email: string, password: string)
|
Defined in src/module/auth/service/auth.service.ts:16
|
Returns :
Promise<User>
|
Async authorize | ||||||
authorize(userId: number)
|
||||||
Defined in src/module/auth/service/auth.service.ts:30
|
||||||
Parameters :
Returns :
Promise<User>
|
import * as bcrpyt from "bcryptjs";
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { InvalidEmailOrPasswordException } from "../exception/invalid.email.or.password.exception";
import { UnauthorizedException } from "../exception/unauthorized.exception";
import { User } from "../../../entity/user.entity";
@Injectable()
export class AuthService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>
) { }
async authenticate(email: string, password: string): Promise<User> {
const user = await this.userRepository.findOne({
where: {
email,
}, relations: ["group"]
});
if (user === undefined || !bcrpyt.compareSync(password, user.password)) {
throw new InvalidEmailOrPasswordException();
}
return user;
}
async authorize(userId: number): Promise<User> {
const user = await this.userRepository.findOne({
where: {
id: userId,
}, relations: ["group"]
});
if (user === undefined) {
throw new UnauthorizedException();
}
return user;
}
}