You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
668 B
24 lines
668 B
|
2 years ago
|
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||
|
|
import { JwtService } from '@nestjs/jwt';
|
||
|
|
|
||
|
|
import { UsersService } from '../users/users.service';
|
||
|
|
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class AuthService {
|
||
|
|
constructor(
|
||
|
|
private usersService: UsersService,
|
||
|
|
private jwtService: JwtService
|
||
|
|
) {}
|
||
|
|
|
||
|
|
async signIn(username: string, pass: string): Promise<any> {
|
||
|
|
const user = await this.usersService.findOne(username);
|
||
|
|
if (user?.password !== pass) {
|
||
|
|
throw new UnauthorizedException();
|
||
|
|
}
|
||
|
|
const payload = { sub: user.userId, username: user.username };
|
||
|
|
return {
|
||
|
|
access_token: await this.jwtService.signAsync(payload),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|