forked from manito42/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.service.ts
More file actions
35 lines (32 loc) · 1.21 KB
/
Copy pathauth.service.ts
File metadata and controls
35 lines (32 loc) · 1.21 KB
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
34
35
import { Injectable } from '@nestjs/common';
import { UserService } from '../../models/user/user.service';
import { JwtService } from '@nestjs/jwt';
import { JwtPayloadInterface } from '../../common/interfaces/jwt/jwtPayload.interface';
import { UserGetResponseDto } from '../../models/user/dto/response/userGetResponse.dto';
import { UserCreatePayloadDto } from 'src/models/user/dto/request/userCreatePayload.dto';
@Injectable()
export class AuthService {
constructor(
private readonly userService: UserService,
private readonly jwtService: JwtService,
) {}
async verifyOrCreateUser(user: UserCreatePayloadDto): Promise<UserGetResponseDto> {
const { nickname } = user;
const userExist = await this.userService.findByNickname(nickname);
if (!userExist) {
return await this.userService.create(user);
} else {
return await this.userService.updateLastLogin(userExist.id);
}
}
async createToken(user: UserGetResponseDto): Promise<string> {
// create jwt token and return it
const payload: JwtPayloadInterface = {
id: user.id,
nickname: user.nickname,
role: user.role,
profileImage: user.profileImage,
};
return this.jwtService.sign(payload);
}
}