|
1 | 1 | import { Request, Response } from 'express';
|
2 |
| -import { StatusCodes } from 'http-status-codes'; |
3 | 2 | import config from '../config/config.service';
|
4 | 3 | import { RoleType } from '../enums';
|
5 |
| -import { |
6 |
| - ConflictError, |
7 |
| - InvalidCredentialseError, |
8 |
| - NotFoundError, |
9 |
| -} from '../errors/errors.service'; |
10 | 4 | import { GoogleCallbackQuery } from '../types';
|
11 |
| -import { errorResponse, successResponse } from '../utils/api.utils'; |
| 5 | +import { successResponse } from '../utils/api.utils'; |
12 | 6 | import { JwtPayload, signToken } from '../utils/auth.utils';
|
13 | 7 | import { AUTH_COOKIE_KEY, COOKIE_CONFIG } from './auth.constants';
|
14 | 8 | import {
|
@@ -38,242 +32,152 @@ import {
|
38 | 32 | } from './auth.service';
|
39 | 33 |
|
40 | 34 | export const handleSetPassword = async (
|
41 |
| - req: Request<never, never, SetPasswordSchemaType>, |
| 35 | + req: Request<unknown, unknown, SetPasswordSchemaType>, |
42 | 36 | res: Response,
|
43 | 37 | ) => {
|
44 |
| - try { |
45 |
| - await setPassword(req.body); |
| 38 | + await setPassword(req.body); |
46 | 39 |
|
47 |
| - return successResponse(res, 'Password successfully set'); |
48 |
| - } catch (err) { |
49 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
50 |
| - } |
| 40 | + return successResponse(res, 'Password successfully set'); |
51 | 41 | };
|
52 | 42 |
|
53 | 43 | export const handleResetPassword = async (
|
54 |
| - req: Request<never, never, ResetPasswordSchemaType>, |
| 44 | + req: Request<unknown, unknown, ResetPasswordSchemaType>, |
55 | 45 | res: Response,
|
56 | 46 | ) => {
|
57 |
| - try { |
58 |
| - await resetPassword(req.body); |
| 47 | + await resetPassword(req.body); |
59 | 48 |
|
60 |
| - return successResponse(res, 'Password successfully reset'); |
61 |
| - } catch (err) { |
62 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
63 |
| - } |
| 49 | + return successResponse(res, 'Password successfully reset'); |
64 | 50 | };
|
65 | 51 |
|
66 | 52 | export const handleForgetPassword = async (
|
67 |
| - req: Request<never, never, ForgetPasswordSchemaType>, |
| 53 | + req: Request<unknown, unknown, ForgetPasswordSchemaType>, |
68 | 54 | res: Response,
|
69 | 55 | ) => {
|
70 |
| - try { |
71 |
| - const user = await forgetPassword(req.body); |
| 56 | + const user = await forgetPassword(req.body); |
72 | 57 |
|
73 |
| - return successResponse(res, 'Code has been sent', { userId: user._id }); |
74 |
| - } catch (err) { |
75 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
76 |
| - } |
| 58 | + return successResponse(res, 'Code has been sent', { userId: user._id }); |
77 | 59 | };
|
78 | 60 |
|
79 | 61 | export const handleChangePassword = async (
|
80 |
| - req: Request<never, never, ChangePasswordSchemaType>, |
| 62 | + req: Request<unknown, unknown, ChangePasswordSchemaType>, |
81 | 63 | res: Response,
|
82 | 64 | ) => {
|
83 |
| - try { |
84 |
| - await changePassword((req.user as JwtPayload).sub, req.body); |
| 65 | + await changePassword((req.user as JwtPayload).sub, req.body); |
85 | 66 |
|
86 |
| - return successResponse(res, 'Password successfully changed'); |
87 |
| - } catch (err) { |
88 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
89 |
| - } |
| 67 | + return successResponse(res, 'Password successfully changed'); |
90 | 68 | };
|
91 | 69 |
|
92 | 70 | export const handleVerifyOtp = async (
|
93 |
| - req: Request<never, never, VerifyOtpSchemaType>, |
| 71 | + req: Request<unknown, unknown, VerifyOtpSchemaType>, |
94 | 72 | res: Response,
|
95 | 73 | ) => {
|
96 |
| - try { |
97 |
| - const user = await verifyOtp(req.body); |
98 |
| - |
99 |
| - if (req.body.type !== 'RESET_PASSWORD') { |
100 |
| - const token = await signToken({ |
101 |
| - phoneNo: user?.phoneNo, |
102 |
| - email: user?.email, |
103 |
| - role: user.role as RoleType, |
104 |
| - sub: String(user._id), |
105 |
| - }); |
106 |
| - |
107 |
| - res.cookie(AUTH_COOKIE_KEY, token, COOKIE_CONFIG); |
| 74 | + const user = await verifyOtp(req.body); |
| 75 | + |
| 76 | + if (req.body.type !== 'RESET_PASSWORD') { |
| 77 | + const token = await signToken({ |
| 78 | + phoneNo: user?.phoneNo, |
| 79 | + email: user?.email, |
| 80 | + role: user.role as RoleType, |
| 81 | + sub: String(user._id), |
| 82 | + }); |
108 | 83 |
|
109 |
| - return res.json({ accessToken: token }); |
110 |
| - } |
| 84 | + res.cookie(AUTH_COOKIE_KEY, token, COOKIE_CONFIG); |
111 | 85 |
|
112 |
| - return successResponse(res, 'Code verified'); |
113 |
| - } catch (err) { |
114 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
| 86 | + return successResponse(res, 'Login successful', { token: token }); |
115 | 87 | }
|
| 88 | + |
| 89 | + return successResponse(res, 'Code verified'); |
116 | 90 | };
|
117 | 91 |
|
118 | 92 | export const handleRegisterUser = async (
|
119 |
| - req: Request<never, never, RegisterUserByEmailSchemaType>, |
| 93 | + req: Request<unknown, unknown, RegisterUserByEmailSchemaType>, |
120 | 94 | res: Response,
|
121 | 95 | ) => {
|
122 |
| - try { |
123 |
| - const { otpSendTo, user } = await registerUserByEmail(req.body); |
| 96 | + const { otpSendTo, user } = await registerUserByEmail(req.body); |
124 | 97 |
|
125 |
| - return successResponse( |
126 |
| - res, |
127 |
| - `Please check your ${otpSendTo.join(' or ')}, OTP has been sent`, |
128 |
| - { |
129 |
| - userId: user._id, |
130 |
| - }, |
131 |
| - ); |
132 |
| - } catch (err) { |
133 |
| - if (err instanceof ConflictError) { |
134 |
| - return errorResponse(res, err.message, StatusCodes.CONFLICT); |
135 |
| - } |
136 |
| - |
137 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
138 |
| - } |
| 98 | + return successResponse( |
| 99 | + res, |
| 100 | + `Please check your ${otpSendTo.join(' or ')}, OTP has been sent`, |
| 101 | + { |
| 102 | + userId: user._id, |
| 103 | + }, |
| 104 | + ); |
139 | 105 | };
|
140 | 106 |
|
141 | 107 | export const handleLogout = async (_: Request, res: Response) => {
|
142 |
| - try { |
143 |
| - res.cookie(AUTH_COOKIE_KEY, undefined, COOKIE_CONFIG); |
| 108 | + res.cookie(AUTH_COOKIE_KEY, undefined, COOKIE_CONFIG); |
144 | 109 |
|
145 |
| - return res.send('Logged out'); |
146 |
| - } catch (err) { |
147 |
| - return errorResponse(res, (err as Error).message); |
148 |
| - } |
| 110 | + return successResponse(res, 'Logout successful'); |
149 | 111 | };
|
150 | 112 |
|
151 | 113 | export const handleLoginByEmail = async (
|
152 |
| - req: Request<never, never, LoginUserByEmailSchemaType>, |
| 114 | + req: Request<unknown, unknown, LoginUserByEmailSchemaType>, |
153 | 115 | res: Response,
|
154 | 116 | ) => {
|
155 |
| - try { |
156 |
| - const token = await loginUserByEmail(req.body); |
157 |
| - console.log(config.SET_SESSION, typeof config.SET_SESSION); |
158 |
| - if (config.SET_SESSION) { |
159 |
| - res.cookie(AUTH_COOKIE_KEY, token, COOKIE_CONFIG); |
160 |
| - } |
161 |
| - return res.json({ token: token }); |
162 |
| - } catch (err) { |
163 |
| - if (err instanceof InvalidCredentialseError) { |
164 |
| - return errorResponse(res, err.message, StatusCodes.BAD_REQUEST); |
165 |
| - } |
166 |
| - |
167 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
| 117 | + const token = await loginUserByEmail(req.body); |
| 118 | + if (config.SET_SESSION) { |
| 119 | + res.cookie(AUTH_COOKIE_KEY, token, COOKIE_CONFIG); |
168 | 120 | }
|
| 121 | + return successResponse(res, 'Login successful', { token: token }); |
169 | 122 | };
|
170 | 123 |
|
171 | 124 | export const handleLoginByPhoneAndPassword = async (
|
172 |
| - req: Request<never, never, LoginUserByPhoneAndPasswordSchemaType>, |
| 125 | + req: Request<unknown, unknown, LoginUserByPhoneAndPasswordSchemaType>, |
173 | 126 | res: Response,
|
174 | 127 | ) => {
|
175 |
| - try { |
176 |
| - const token = await loginUserByPhoneAndPassword(req.body); |
| 128 | + const token = await loginUserByPhoneAndPassword(req.body); |
177 | 129 |
|
178 |
| - res.json({ accessToken: token }); |
179 |
| - } catch (err) { |
180 |
| - if (err instanceof InvalidCredentialseError) { |
181 |
| - return errorResponse(res, err.message, StatusCodes.BAD_REQUEST); |
182 |
| - } |
183 |
| - |
184 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
185 |
| - } |
| 130 | + return successResponse(res, 'Login successful', { token: token }); |
186 | 131 | };
|
187 | 132 |
|
188 | 133 | export const handleLoginByPhone = async (
|
189 |
| - req: Request<never, never, LoginUserByPhoneSchemaType>, |
| 134 | + req: Request<unknown, unknown, LoginUserByPhoneSchemaType>, |
190 | 135 | res: Response,
|
191 | 136 | ) => {
|
192 |
| - try { |
193 |
| - const user = await loginUserByPhone(req.body); |
| 137 | + const user = await loginUserByPhone(req.body); |
194 | 138 |
|
195 |
| - const phone = `*******${user.phoneNo?.slice(-3)}`; |
| 139 | + const phone = `*******${user.phoneNo?.slice(-3)}`; |
196 | 140 |
|
197 |
| - return successResponse(res, `Code has been sent to ${phone}`, { |
198 |
| - userId: user._id, |
199 |
| - }); |
200 |
| - } catch (err) { |
201 |
| - if (err instanceof InvalidCredentialseError) { |
202 |
| - return errorResponse(res, err.message, StatusCodes.BAD_REQUEST); |
203 |
| - } |
204 |
| - |
205 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
206 |
| - } |
| 141 | + return successResponse(res, `Code has been sent to ${phone}`, { |
| 142 | + userId: user._id, |
| 143 | + }); |
207 | 144 | };
|
208 | 145 |
|
209 | 146 | export const handleValidateLoginCode = async (
|
210 |
| - req: Request<never, never, ValidateLoginOtpSchemaType>, |
| 147 | + req: Request<unknown, unknown, ValidateLoginOtpSchemaType>, |
211 | 148 | res: Response,
|
212 | 149 | ) => {
|
213 |
| - try { |
214 |
| - const token = await validateLoginOtp(req.body); |
215 |
| - |
216 |
| - if (process.env.SET_SESSION) { |
217 |
| - res.cookie(AUTH_COOKIE_KEY, token, COOKIE_CONFIG); |
218 |
| - } |
219 |
| - return res.json({ token: token }); |
220 |
| - } catch (err) { |
221 |
| - if (err instanceof InvalidCredentialseError) { |
222 |
| - return errorResponse(res, err.message, StatusCodes.BAD_REQUEST); |
223 |
| - } |
| 150 | + const token = await validateLoginOtp(req.body); |
224 | 151 |
|
225 |
| - return errorResponse(res, (err as Error).message, StatusCodes.BAD_REQUEST); |
| 152 | + if (process.env.SET_SESSION) { |
| 153 | + res.cookie(AUTH_COOKIE_KEY, token, COOKIE_CONFIG); |
226 | 154 | }
|
| 155 | + |
| 156 | + return successResponse(res, 'Login successful', { token: token }); |
227 | 157 | };
|
228 | 158 |
|
229 | 159 | export const handleGetCurrentUser = async (req: Request, res: Response) => {
|
230 |
| - try { |
231 |
| - const user = req.user; |
232 |
| - |
233 |
| - return res.json({ user }); |
234 |
| - } catch (err) { |
235 |
| - if (err instanceof NotFoundError) { |
236 |
| - return errorResponse(res, err.message, StatusCodes.NOT_FOUND); |
237 |
| - } |
| 160 | + const user = req.user; |
238 | 161 |
|
239 |
| - return errorResponse( |
240 |
| - res, |
241 |
| - 'Not allowed to access this route', |
242 |
| - StatusCodes.UNAUTHORIZED, |
243 |
| - ); |
244 |
| - } |
| 162 | + return successResponse(res, undefined, user); |
245 | 163 | };
|
246 | 164 | export const handleGoogleLogin = async (_: Request, res: Response) => {
|
247 |
| - try { |
248 |
| - const googleAuthURL = `https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=${process.env.GOOGLE_CLIENT_ID}&redirect_uri=${process.env.GOOGLE_REDIRECT_URI}&scope=email profile`; |
249 |
| - res.redirect(googleAuthURL); |
250 |
| - } catch (err) { |
251 |
| - if (err instanceof NotFoundError) { |
252 |
| - return errorResponse(res, err.message, StatusCodes.NOT_FOUND); |
253 |
| - } |
254 |
| - |
255 |
| - return errorResponse( |
256 |
| - res, |
257 |
| - 'Not allowed to access this route', |
258 |
| - StatusCodes.UNAUTHORIZED, |
259 |
| - ); |
260 |
| - } |
| 165 | + const googleAuthURL = `https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=${process.env.GOOGLE_CLIENT_ID}&redirect_uri=${process.env.GOOGLE_REDIRECT_URI}&scope=email profile`; |
| 166 | + res.redirect(googleAuthURL); |
261 | 167 | };
|
262 | 168 | export const handleGoogleCallback = async (
|
263 |
| - req: Request<never, never, never, GoogleCallbackQuery>, |
| 169 | + req: Request<unknown, unknown, unknown, GoogleCallbackQuery>, |
264 | 170 | res: Response,
|
265 | 171 | ) => {
|
266 |
| - try { |
267 |
| - const user = await googleLogin(req.query); |
268 |
| - if (!user) throw new Error('Failed to login'); |
269 |
| - res.cookie( |
270 |
| - AUTH_COOKIE_KEY, |
271 |
| - user.socialAccount?.[0]?.accessToken, |
272 |
| - COOKIE_CONFIG, |
273 |
| - ); |
274 |
| - |
275 |
| - return res.json({ token: user.socialAccount?.[0]?.accessToken }); |
276 |
| - } catch (err) { |
277 |
| - res.status(401).json({ error: (err as { message: string }).message }); |
278 |
| - } |
| 172 | + const user = await googleLogin(req.query); |
| 173 | + if (!user) throw new Error('Failed to login'); |
| 174 | + res.cookie( |
| 175 | + AUTH_COOKIE_KEY, |
| 176 | + user.socialAccount?.[0]?.accessToken, |
| 177 | + COOKIE_CONFIG, |
| 178 | + ); |
| 179 | + |
| 180 | + return successResponse(res, 'Logged in successfully', { |
| 181 | + token: user.socialAccount?.[0]?.accessToken, |
| 182 | + }); |
279 | 183 | };
|
0 commit comments