-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathauth.service.ts
33 lines (26 loc) · 894 Bytes
/
auth.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import type { JwtPayload } from './auth.interface.js';
import { type User, UserService } from '../user/index.js';
@Injectable()
export class AuthService {
constructor(
private jwt: JwtService,
private user: UserService,
) {}
public async validateUser(username: string, password: string): Promise<User | null> {
const user = await this.user.fetch(username);
if (user?.password === password) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, sonarjs/no-unused-vars
const { password: pass, ...result } = user;
return result;
}
return null;
}
public login(user: User): { access_token: string } {
const payload: JwtPayload = { username: user.name, sub: user.id };
return {
access_token: this.jwt.sign(payload),
};
}
}