1
1
import jwt from 'jsonwebtoken' ;
2
2
import bcrypt from 'bcryptjs' ;
3
- import { BadRequestError , UnauthorizedError } from '../../utils/exceptions' ;
3
+ import {
4
+ BadRequestError ,
5
+ InternalServerError ,
6
+ UnauthorizedError ,
7
+ } from '../../utils/exceptions' ;
4
8
import { userTable } from '../user/user.model' ;
5
9
import { connectdb } from '../../configs/db' ;
6
10
import { eq } from 'drizzle-orm' ;
@@ -19,7 +23,7 @@ export class AuthService {
19
23
constructor ( ) {
20
24
this . accessTokenSecret = process . env . ACCESS_TOKEN_SECRET ;
21
25
this . refreshTokenSecret = process . env . REFRESH_TOKEN_SECRET ;
22
- this . accessTokenExpiry = '12h ' ;
26
+ this . accessTokenExpiry = '15m ' ;
23
27
this . refreshTokenExpiry = '7d' ;
24
28
}
25
29
@@ -87,39 +91,58 @@ export class AuthService {
87
91
}
88
92
89
93
async login ( { email, password } : { email : string ; password : string } ) {
90
- // Check if email and password are provided
91
94
if ( ! email || ! password ) {
92
95
throw new BadRequestError ( 'Email and password are required' ) ;
93
96
}
94
97
95
- // Fetch the user by email
96
- // const user = await UserModel.findOne({ email });
98
+ // Fetch user by email
97
99
const { db, connection } = await connectdb ( ) ;
98
100
const [ user ] = await db
99
101
. select ( )
100
102
. from ( userTable )
101
103
. where ( eq ( userTable . email , email ) )
102
104
. execute ( ) ;
103
- await connection . end ( ) ;
104
105
105
106
if ( ! user ) {
106
107
throw new UnauthorizedError ( 'Invalid credentials' ) ;
107
108
}
108
109
109
- // Compare the plain password with the hashed password in the database
110
110
const isPasswordValid = await this . comparePassword ( password , user . hash ) ;
111
111
if ( ! isPasswordValid ) {
112
112
throw new UnauthorizedError ( 'Invalid credentials' ) ;
113
113
}
114
114
115
- console . log ( { user } ) ;
116
115
// Generate access token
117
116
const accessToken = this . generateAccessToken ( {
118
117
email : user . email ,
119
118
id : user . id ,
120
119
} ) ;
121
120
122
- // Return the generated tokens
121
+ // Handle refresh token verification or renewal
122
+ let refreshToken = user . refresh_token ;
123
+ try {
124
+ this . verifyRefreshToken ( refreshToken ) ;
125
+ } catch ( err ) {
126
+ console . log ( 'Refresh token is invalid, generating a new one...' ) ;
127
+ refreshToken = this . generateRefreshToken ( {
128
+ id : user . id ,
129
+ email : user . email ,
130
+ } ) ;
131
+
132
+ try {
133
+ await db
134
+ . update ( userTable )
135
+ . set ( { refresh_token : refreshToken } )
136
+ . where ( eq ( userTable . id , user . id ) )
137
+ . execute ( ) ;
138
+ console . log ( 'Refresh token updated successfully' ) ;
139
+ await connection . end ( ) ;
140
+ } catch ( updateErr ) {
141
+ console . error ( 'Database update error:' , updateErr ) ;
142
+ throw new InternalServerError ( "Couldn't refresh token" ) ;
143
+ }
144
+ }
145
+
123
146
return {
124
147
message : 'Login successful' ,
125
148
user : {
@@ -128,7 +151,7 @@ export class AuthService {
128
151
} ,
129
152
data : {
130
153
accessToken,
131
- refreshToken : user . refresh_token ,
154
+ refreshToken,
132
155
} ,
133
156
} ;
134
157
}
0 commit comments