-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauth.controller.ts
95 lines (91 loc) ยท 2.9 KB
/
auth.controller.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {
Controller,
Post,
Body,
NotFoundException,
BadRequestException,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { Public } from './public.decorator';
import { KakaoUserDto } from './dto/kakao-user.dto';
import { UsersService } from '../users/users.service';
import { RefreshTokenDto } from './dto/refresh-token.dto';
import { ProfilesService } from '../profiles/profiles.service';
import { ApiTags, ApiResponse, ApiOperation } from '@nestjs/swagger';
import customEnv from '../config/env';
import { RefreshTokensService } from './refresh-tokens.service';
@Controller('auth')
@ApiTags('auth')
export class AuthController {
constructor(
private authService: AuthService,
private usersService: UsersService,
private profilesService: ProfilesService,
private refreshTokensService: RefreshTokensService,
) {}
@Post('kakao-oauth')
@Public()
@ApiOperation({ summary: 'kakao login' })
@ApiResponse({
status: 200,
description: 'Return the token data.',
})
@ApiResponse({
status: 404,
description: 'Not Found.',
})
async kakaoLogin(@Body() kakaoUserDto: KakaoUserDto) {
const kakaoUserAccount = await this.authService.getKakaoAccount(
kakaoUserDto.kakaoUserId,
);
if (!kakaoUserAccount) throw new NotFoundException();
const email = kakaoUserAccount.email;
const user = await this.usersService.findUserByEmailAndProvider(
email,
'kakao',
);
let userUuid = user?.uuid;
if (!userUuid) {
const data = { email, provider: 'kakao' };
const createdUser = await this.usersService.createUser(data);
userUuid = createdUser.uuid;
const profileData = {
user_id: createdUser.uuid,
image: customEnv.BASE_IMAGE_URL,
nickname: '์ต๋ช
์ ์ฌ์ฉ์',
};
await this.profilesService.createProfile(profileData);
}
const tokenData = await this.authService.login(userUuid);
return { statusCode: 200, message: 'Success', data: tokenData };
}
@Post('token')
@Public()
@ApiOperation({ summary: 'Renew Access Token' })
@ApiResponse({
status: 200,
description: 'Return the access token data.',
})
@ApiResponse({
status: 401,
description: 'Refresh token expired. Please log in again.',
})
async renewAccessToken(@Body() refreshTokenDto: RefreshTokenDto) {
const refreshToken = refreshTokenDto.refresh_token;
const accessToken = await this.authService.renewAccessToken(refreshToken);
return {
statusCode: 200,
message: 'Success',
data: { access_token: accessToken },
};
}
@Post('logout')
@Public()
async logout(@Body() refreshTokenDto: RefreshTokenDto) {
const refreshToken = refreshTokenDto.refresh_token;
const token =
await this.refreshTokensService.deleteRefreshToken(refreshToken);
if (!token) throw new BadRequestException();
return { statusCode: 204, message: 'No Content' };
}
}