1
- import jwt from ' jsonwebtoken' ;
2
- import bcrypt from ' bcrypt' ;
3
- import { LoginPayload , RegisterPayload } from ' ./auth.validation' ;
4
- import { AuthRepository } from ' ./auth.repository' ;
5
- import { env } from ' config/env' ;
6
- import { BadRequestError } from ' errors/bad-request.error' ;
7
- import { CookieOptions } from ' express' ;
8
- import { User } from ' users/users.model' ;
1
+ import jwt from " jsonwebtoken" ;
2
+ import bcrypt from " bcrypt" ;
3
+ import { LoginPayload , RegisterPayload } from " ./auth.validation" ;
4
+ import { AuthRepository } from " ./auth.repository" ;
5
+ import { env } from " config/env" ;
6
+ import { BadRequestError } from " errors/bad-request.error" ;
7
+ import { CookieOptions } from " express" ;
8
+ import { User } from " users/users.model" ;
9
9
10
10
export class AuthService {
11
11
private authRepository = new AuthRepository ( ) ;
@@ -14,15 +14,15 @@ export class AuthService {
14
14
// Check if email already exists
15
15
const foundUser = await this . authRepository . findUserByEmail ( payload . email ) ;
16
16
if ( foundUser ) {
17
- throw new BadRequestError ( ' User with that email already exists' ) ;
17
+ throw new BadRequestError ( " User with that email already exists" ) ;
18
18
}
19
19
20
20
const user = await this . authRepository . createUser ( {
21
21
...payload ,
22
22
password : await bcrypt . hash ( payload . password , 10 ) , // Hash password
23
23
} ) ;
24
24
if ( ! user ) {
25
- throw new BadRequestError ( ' Failed to create user' ) ;
25
+ throw new BadRequestError ( " Failed to create user" ) ;
26
26
}
27
27
28
28
return this . removePassword ( user ) ;
@@ -31,12 +31,12 @@ export class AuthService {
31
31
public async login ( payload : LoginPayload ) {
32
32
const user = await this . authRepository . findUserByEmail ( payload . email ) ;
33
33
if ( ! user ) {
34
- throw new BadRequestError ( ' Invalid email or password' ) ;
34
+ throw new BadRequestError ( " Invalid email or password" ) ;
35
35
}
36
36
37
37
const isPasswordCorrect = await bcrypt . compare ( payload . password , user . password ) ;
38
38
if ( ! isPasswordCorrect ) {
39
- throw new BadRequestError ( ' Invalid email or password' ) ;
39
+ throw new BadRequestError ( " Invalid email or password" ) ;
40
40
}
41
41
42
42
return this . removePassword ( user ) ;
@@ -55,13 +55,13 @@ export class AuthService {
55
55
return {
56
56
maxAge : 5 * 60 * 60 * 1000 , // 5 hours
57
57
httpOnly : true ,
58
- sameSite : ' lax' ,
59
- secure : process . env . NODE_ENV === ' production' ,
58
+ sameSite : " lax" ,
59
+ secure : process . env . NODE_ENV === " production" ,
60
60
} ;
61
61
}
62
62
63
63
private removePassword ( user : User ) {
64
- const { password, ...userWithoutPassword } = user ;
64
+ const { password : _password , ...userWithoutPassword } = user ;
65
65
return userWithoutPassword ;
66
66
}
67
67
}
0 commit comments