|
1 |
| -import { RequestHandler } from 'express'; |
2 |
| -import sendResponse from '../../../shared/sendResponse'; |
3 |
| -import catchAsync from '../../../shared/catchAsync'; |
4 |
| -import httpStatus from 'http-status'; |
5 |
| -import { AuthService } from './auth.service'; |
| 1 | +import { RequestHandler } from 'express' |
| 2 | +import sendResponse from '../../../shared/sendResponse' |
| 3 | +import catchAsync from '../../../shared/catchAsync' |
| 4 | +import httpStatus from 'http-status' |
| 5 | +import { AuthService } from './auth.service' |
6 | 6 | import {
|
7 | 7 | IRefreshTokenResponse,
|
8 | 8 | IUserLoginResponse,
|
9 | 9 | IUser#Response,
|
10 |
| -} from './auth.interface'; |
11 |
| -import config from '../../../config'; |
| 10 | +} from './auth.interface' |
| 11 | +import config from '../../../config' |
12 | 12 |
|
13 | 13 | const createUser: RequestHandler = catchAsync(async (req, res) => {
|
14 |
| - const { ...userData } = req.body; |
| 14 | + const { ...userData } = req.body |
15 | 15 |
|
16 | 16 | const { result, refreshToken, accessToken } = await AuthService.createUser(
|
17 | 17 | userData
|
18 |
| - ); |
| 18 | + ) |
19 | 19 |
|
20 | 20 | // set refresh token in the browser cookie
|
21 | 21 | const cookieOptions = {
|
22 | 22 | secure: config.node_env === 'production',
|
23 | 23 | httpOnly: true,
|
24 |
| - }; |
| 24 | + } |
25 | 25 |
|
26 |
| - res.cookie('refreshToken', refreshToken, cookieOptions); |
| 26 | + res.cookie('refreshToken', refreshToken, cookieOptions) |
27 | 27 |
|
28 | 28 | sendResponse<IUser#Response>(res, {
|
29 | 29 | statusCode: httpStatus.OK,
|
30 | 30 | success: true,
|
31 | 31 | message: 'user created successfully',
|
32 | 32 | data: { result, accessToken },
|
33 |
| - }); |
34 |
| -}); |
| 33 | + }) |
| 34 | +}) |
35 | 35 |
|
36 | 36 | const login: RequestHandler = catchAsync(async (req, res) => {
|
37 |
| - const { ...loginData } = req.body; |
| 37 | + const { ...loginData } = req.body |
38 | 38 |
|
39 |
| - const result = await AuthService.login(loginData); |
| 39 | + const result = await AuthService.login(loginData) |
40 | 40 |
|
41 |
| - const { refreshToken, ...accessToken } = result; |
| 41 | + const { refreshToken, ...accessToken } = result |
42 | 42 |
|
43 | 43 | // set refresh token in the browser cookie
|
44 | 44 | const cookieOptions = {
|
45 | 45 | secure: config.node_env === 'production',
|
46 | 46 | httpOnly: true,
|
47 |
| - }; |
| 47 | + } |
48 | 48 |
|
49 |
| - res.cookie('refreshToken', refreshToken, cookieOptions); |
| 49 | + res.cookie('refreshToken', refreshToken, cookieOptions) |
50 | 50 |
|
51 | 51 | sendResponse<IUserLoginResponse>(res, {
|
52 | 52 | statusCode: httpStatus.OK,
|
53 | 53 | success: true,
|
54 | 54 | message: 'user logged in successfully',
|
55 | 55 | data: accessToken,
|
56 |
| - }); |
57 |
| -}); |
| 56 | + }) |
| 57 | +}) |
58 | 58 |
|
59 | 59 | const refreshToken: RequestHandler = catchAsync(async (req, res) => {
|
60 |
| - const { refreshToken } = req.cookies; |
| 60 | + const { refreshToken } = req.cookies |
61 | 61 |
|
62 |
| - const result = await AuthService.refreshToken(refreshToken); |
| 62 | + const result = await AuthService.refreshToken(refreshToken) |
63 | 63 |
|
64 | 64 | // set refresh token in the browser cookie
|
65 | 65 | const cookieOptions = {
|
66 | 66 | secure: config.node_env === 'production',
|
67 | 67 | httpOnly: true,
|
68 |
| - }; |
69 |
| - res.cookie('refreshToken', refreshToken, cookieOptions); |
| 68 | + } |
| 69 | + res.cookie('refreshToken', refreshToken, cookieOptions) |
70 | 70 |
|
71 | 71 | sendResponse<IRefreshTokenResponse>(res, {
|
72 | 72 | statusCode: httpStatus.OK,
|
73 | 73 | success: true,
|
74 | 74 | message: 'New access token generated successfully !',
|
75 | 75 | data: result,
|
76 |
| - }); |
77 |
| -}); |
| 76 | + }) |
| 77 | +}) |
| 78 | + |
| 79 | +const googleAuth: RequestHandler = catchAsync(async (req, res) => { |
| 80 | + const { tokenId } = req.body |
| 81 | + |
| 82 | + const result = await AuthService.googleAuth(tokenId) |
| 83 | + // console.log('result', result) |
| 84 | + let refreshToken, accessToken |
| 85 | + if (result) { |
| 86 | + ;({ refreshToken, ...accessToken } = result) |
| 87 | + } |
| 88 | + // set refresh token in the browser cookie |
| 89 | + const cookieOptions = { |
| 90 | + secure: config.node_env === 'production', |
| 91 | + httpOnly: true, |
| 92 | + } |
| 93 | + |
| 94 | + res.cookie('refreshToken', refreshToken, cookieOptions) |
| 95 | + |
| 96 | + sendResponse<IUserLoginResponse>(res, { |
| 97 | + statusCode: httpStatus.OK, |
| 98 | + success: true, |
| 99 | + message: 'user logged in successfully', |
| 100 | + data: accessToken, |
| 101 | + }) |
| 102 | +}) |
78 | 103 |
|
79 | 104 | export const AuthController = {
|
80 | 105 | createUser,
|
81 | 106 | login,
|
82 | 107 | refreshToken,
|
83 |
| -}; |
| 108 | + googleAuth, |
| 109 | +} |
0 commit comments