|
1 |
| -import { NextFunction, Request, Response, RequestHandler } from 'express'; |
| 1 | +import { NextFunction, Request, Response, RequestHandler, CookieOptions } from 'express'; |
2 | 2 | import logger from '../configs/logger.config';
|
3 |
| -import { UserWithTokenDto } from '../types'; |
| 3 | +import { LoginResponse, UserWithTokenDto } from '../types'; |
4 | 4 | import { UserService } from '../services/user.service';
|
5 |
| - |
6 | 5 | export class UserController {
|
7 | 6 | constructor(private userService: UserService) {}
|
8 | 7 |
|
9 |
| - login = (async (req: Request, res: Response, next: NextFunction) => { |
| 8 | + private cookieOption(): CookieOptions { |
| 9 | + const isProd = process.env.NODE_ENV === 'production'; |
| 10 | + |
| 11 | + const baseOptions: CookieOptions = { |
| 12 | + httpOnly: isProd, |
| 13 | + secure: isProd, |
| 14 | + domain: process.env.COOKIE_DOMAIN || 'localhost', |
| 15 | + }; |
| 16 | + |
| 17 | + if (isProd) { |
| 18 | + baseOptions.sameSite = 'lax'; |
| 19 | + } |
| 20 | + |
| 21 | + return baseOptions; |
| 22 | + } |
| 23 | + |
| 24 | + login: RequestHandler = async (req: Request, res: Response<LoginResponse>, next: NextFunction): Promise<void> => { |
10 | 25 | try {
|
11 |
| - const { id, email, profile } = req.user; |
| 26 | + const { id, email, profile, username } = req.user; |
12 | 27 | const { accessToken, refreshToken } = req.tokens;
|
13 | 28 |
|
14 | 29 | const userWithToken: UserWithTokenDto = { id, email, accessToken, refreshToken };
|
15 | 30 | const isExistUser = await this.userService.handleUserTokensByVelogUUID(userWithToken);
|
16 |
| - return res.status(200).json({ |
| 31 | + |
| 32 | + res.clearCookie('access_token'); |
| 33 | + res.clearCookie('refresh_token'); |
| 34 | + |
| 35 | + res.cookie('access_token', accessToken, this.cookieOption()); |
| 36 | + res.cookie('refresh_token', refreshToken, this.cookieOption()); |
| 37 | + |
| 38 | + res.status(200).json({ |
17 | 39 | success: true,
|
18 | 40 | message: '로그인에 성공하였습니다.',
|
19 |
| - data: { id: isExistUser.id, email: isExistUser.email, profile }, |
| 41 | + data: { id: isExistUser.id, username, profile }, |
| 42 | + error: null, |
20 | 43 | });
|
21 | 44 | } catch (error) {
|
22 | 45 | logger.error('로그인 실패 : ', error);
|
23 | 46 | next(error);
|
24 | 47 | }
|
25 |
| - }) as RequestHandler; |
| 48 | + }; |
| 49 | + |
| 50 | + logout: RequestHandler = async (req: Request, res: Response) => { |
| 51 | + res.clearCookie('access_token'); |
| 52 | + res.clearCookie('refresh_token'); |
| 53 | + |
| 54 | + res.status(200).json({ success: true, message: '로그아웃에 성공하였습니다.', data: {}, error: null }); |
| 55 | + }; |
| 56 | + |
| 57 | + fetchCurrentUser: RequestHandler = (req: Request, res: Response) => { |
| 58 | + const { user } = req; |
| 59 | + res.status(200).json({ |
| 60 | + success: true, |
| 61 | + message: '프로필 조회에 성공하였습니다.', |
| 62 | + data: { user }, |
| 63 | + error: null, |
| 64 | + }); |
| 65 | + }; |
26 | 66 | }
|
0 commit comments