@@ -6,13 +6,18 @@ import { JWT_SECRET } from '../config/utils.js';
6
6
import { ApiError } from '../utils/api-error.js' ;
7
7
import { ApiResponse } from '../utils/api-response.js' ;
8
8
import { asyncHandler } from '../utils/async-handler.js' ;
9
+ import { NextFunction , Request , Response } from 'express' ;
9
10
10
11
//REGULAR EMAIL PASSWORD STRATEGY
11
12
//1.Sign Up
12
- export const signUpWithEmail = asyncHandler ( async ( req , res ) => {
13
+
14
+ export const signUpWithEmail = asyncHandler ( async ( req : Request , res : Response ) => {
13
15
const { userName, fullName, email, password } = req . body ;
14
16
if ( ! userName || ! fullName || ! email || ! password ) {
15
- throw new ApiError ( HTTP_STATUS . BAD_REQUEST , RESPONSE_MESSAGES . COMMON . REQUIRED_FIELDS ) ;
17
+ throw new ApiError ( {
18
+ status : HTTP_STATUS . BAD_REQUEST ,
19
+ message : RESPONSE_MESSAGES . COMMON . REQUIRED_FIELDS ,
20
+ } ) ;
16
21
}
17
22
18
23
const existingUser = await User . findOne ( {
@@ -21,10 +26,16 @@ export const signUpWithEmail = asyncHandler(async (req, res) => {
21
26
22
27
if ( existingUser ) {
23
28
if ( existingUser . userName === userName ) {
24
- throw new ApiError ( HTTP_STATUS . BAD_REQUEST , RESPONSE_MESSAGES . USERS . USER_USERNAME_EXISTS ) ;
29
+ throw new ApiError ( {
30
+ status : HTTP_STATUS . BAD_REQUEST ,
31
+ message : RESPONSE_MESSAGES . USERS . USER_USERNAME_EXISTS ,
32
+ } ) ;
25
33
}
26
34
if ( existingUser . email === email ) {
27
- throw new ApiError ( HTTP_STATUS . BAD_REQUEST , RESPONSE_MESSAGES . USERS . USER_EMAIL_EXISTS ) ;
35
+ throw new ApiError ( {
36
+ status : HTTP_STATUS . BAD_REQUEST ,
37
+ message : RESPONSE_MESSAGES . USERS . USER_EMAIL_EXISTS ,
38
+ } ) ;
28
39
}
29
40
}
30
41
@@ -37,12 +48,15 @@ export const signUpWithEmail = asyncHandler(async (req, res) => {
37
48
38
49
try {
39
50
await user . validate ( ) ;
40
- } catch ( error ) {
41
- const validationErrors = [ ] ;
51
+ } catch ( error : any ) {
52
+ const validationErrors : any = [ ] ;
42
53
for ( const key in error . errors ) {
43
- validationErrors . push ( error . errors [ key ] . message ) ;
54
+ validationErrors . push ( error . errors [ key ] . message as never ) ;
44
55
}
45
- throw new ApiError ( HTTP_STATUS . BAD_REQUEST , validationErrors . join ( ', ' ) ) ;
56
+ throw new ApiError ( {
57
+ status : HTTP_STATUS . BAD_REQUEST ,
58
+ errors : validationErrors . join ( ', ' ) ,
59
+ } ) ;
46
60
}
47
61
48
62
const accessToken = await user . generateAccessToken ( ) ;
@@ -71,24 +85,33 @@ export const signUpWithEmail = asyncHandler(async (req, res) => {
71
85
} ) ;
72
86
73
87
//2.Sign In
74
- export const signInWithEmailOrUsername = asyncHandler ( async ( req , res ) => {
88
+ export const signInWithEmailOrUsername = asyncHandler ( async ( req : Request , res : Response ) => {
75
89
const { userNameOrEmail, password } = req . body ;
76
90
if ( ! userNameOrEmail || ! password ) {
77
- throw new ApiError ( HTTP_STATUS . BAD_REQUEST , RESPONSE_MESSAGES . COMMON . REQUIRED_FIELDS ) ;
91
+ throw new ApiError ( {
92
+ status : HTTP_STATUS . BAD_REQUEST ,
93
+ message : RESPONSE_MESSAGES . COMMON . REQUIRED_FIELDS ,
94
+ } ) ;
78
95
}
79
96
80
97
const user = await User . findOne ( {
81
98
$or : [ { email : userNameOrEmail } , { userName : userNameOrEmail } ] ,
82
99
} ) . select ( '+password' ) ;
83
100
84
101
if ( ! user ) {
85
- throw new ApiError ( HTTP_STATUS . BAD_REQUEST , RESPONSE_MESSAGES . USERS . USER_NOT_EXISTS ) ;
102
+ throw new ApiError ( {
103
+ status : HTTP_STATUS . BAD_REQUEST ,
104
+ message : RESPONSE_MESSAGES . USERS . USER_NOT_EXISTS ,
105
+ } ) ;
86
106
}
87
107
88
108
const isCorrectPassword = await user . isPasswordCorrect ( password ) ;
89
109
90
110
if ( ! isCorrectPassword ) {
91
- throw new ApiError ( HTTP_STATUS . UNAUTHORIZED , RESPONSE_MESSAGES . USERS . INVALID_PASSWORD ) ;
111
+ throw new ApiError ( {
112
+ status : HTTP_STATUS . UNAUTHORIZED ,
113
+ message : RESPONSE_MESSAGES . USERS . INVALID_PASSWORD ,
114
+ } ) ;
92
115
}
93
116
const accessToken = await user . generateAccessToken ( ) ;
94
117
const refreshToken = await user . generateRefreshToken ( ) ;
@@ -115,7 +138,7 @@ export const signInWithEmailOrUsername = asyncHandler(async (req, res) => {
115
138
} ) ;
116
139
117
140
//Sign Out
118
- export const signOutUser = asyncHandler ( async ( req , res , next ) => {
141
+ export const signOutUser = asyncHandler ( async ( req : Request , res : Response , next : NextFunction ) => {
119
142
await User . findByIdAndUpdate (
120
143
req . user ?. _id ,
121
144
{
@@ -129,9 +152,14 @@ export const signOutUser = asyncHandler(async (req, res, next) => {
129
152
) ;
130
153
131
154
// Passport.js logout
132
- req . logout ( ( err ) => {
155
+ req . logout ( ( err : any ) => {
133
156
if ( err ) {
134
- return next ( new ApiError ( HTTP_STATUS . INTERNAL_SERVER_ERROR , 'Logout failed' ) ) ;
157
+ return next (
158
+ new ApiError ( {
159
+ status : HTTP_STATUS . INTERNAL_SERVER_ERROR ,
160
+ message : 'Logout failed' ,
161
+ } )
162
+ ) ;
135
163
}
136
164
137
165
res
@@ -143,7 +171,7 @@ export const signOutUser = asyncHandler(async (req, res, next) => {
143
171
} ) ;
144
172
} ) ;
145
173
// check user
146
- export const isLoggedIn = asyncHandler ( async ( req , res ) => {
174
+ export const isLoggedIn = asyncHandler ( async ( req : Request , res : Response ) => {
147
175
let access_token = req . cookies ?. access_token ;
148
176
let refresh_token = req . cookies ?. refresh_token ;
149
177
const { _id } = req . params ;
@@ -155,18 +183,22 @@ export const isLoggedIn = asyncHandler(async (req, res) => {
155
183
}
156
184
if ( access_token ) {
157
185
try {
158
- await jwt . verify ( access_token , JWT_SECRET ) ;
186
+ if ( JWT_SECRET ) {
187
+ await jwt . verify ( access_token , JWT_SECRET ) ;
188
+ }
159
189
return res
160
190
. status ( HTTP_STATUS . OK )
161
191
. json ( new ApiResponse ( HTTP_STATUS . OK , access_token , RESPONSE_MESSAGES . USERS . VALID_TOKEN ) ) ;
162
- } catch ( error ) {
192
+ } catch ( error : any ) {
163
193
console . log ( 'Access token verification error:' , error . message ) ;
164
194
}
165
195
}
166
196
// If access token is not valid, check the refresh token
167
197
if ( refresh_token ) {
168
198
try {
169
- await jwt . verify ( refresh_token , JWT_SECRET ) ;
199
+ if ( JWT_SECRET ) {
200
+ await jwt . verify ( refresh_token , JWT_SECRET ) ;
201
+ }
170
202
const user = await User . findById ( _id ) ;
171
203
if ( ! user ) {
172
204
return res
@@ -180,7 +212,7 @@ export const isLoggedIn = asyncHandler(async (req, res) => {
180
212
. status ( HTTP_STATUS . OK )
181
213
. cookie ( 'access_token' , access_token , cookieOptions )
182
214
. json ( new ApiResponse ( HTTP_STATUS . OK , access_token , RESPONSE_MESSAGES . USERS . VALID_TOKEN ) ) ;
183
- } catch ( error ) {
215
+ } catch ( error : any ) {
184
216
console . log ( 'Refresh token verification error:' , error . message ) ;
185
217
}
186
218
}
@@ -202,7 +234,9 @@ export const isLoggedIn = asyncHandler(async (req, res) => {
202
234
}
203
235
204
236
try {
205
- await jwt . verify ( refreshToken , JWT_SECRET ) ;
237
+ if ( JWT_SECRET ) {
238
+ await jwt . verify ( refreshToken , JWT_SECRET ) ;
239
+ }
206
240
access_token = await user . generateAccessToken ( ) ;
207
241
refresh_token = await user . generateRefreshToken ( ) ;
208
242
@@ -213,7 +247,7 @@ export const isLoggedIn = asyncHandler(async (req, res) => {
213
247
. cookie ( 'access_token' , access_token , cookieOptions )
214
248
. cookie ( 'refresh_token' , refresh_token , cookieOptions )
215
249
. json ( new ApiResponse ( HTTP_STATUS . OK , access_token , RESPONSE_MESSAGES . USERS . VALID_TOKEN ) ) ;
216
- } catch ( error ) {
250
+ } catch ( error : any ) {
217
251
return res
218
252
. status ( HTTP_STATUS . UNAUTHORIZED )
219
253
. json (
0 commit comments