@@ -9,22 +9,24 @@ import { JwtService } from '@nestjs/jwt';
99import * as bcrypt from 'bcrypt' ;
1010import { UsersService } from '../users/users.service' ;
1111import { ConfigService } from '@nestjs/config' ;
12- import { SendGridService } from '../sendgrid/sendgrid .service' ;
12+ import { MailService } from '../mail/mail .service' ;
1313
1414@Injectable ( )
1515export class AuthService {
1616 constructor (
1717 private readonly usersService : UsersService ,
1818 private readonly jwtService : JwtService ,
1919 private readonly configService : ConfigService ,
20- private readonly sendGridService : SendGridService
20+ private readonly mailService : MailService
2121 ) { }
2222
2323 async validateUser ( email : string , password : string ) : Promise < any > {
2424 const user = await this . usersService . findByEmail ( email ) ;
2525 if ( user && ( await bcrypt . compare ( password , user . password ) ) ) {
2626 if ( ! user . isEmailVerified ) {
27- throw new UnauthorizedException ( 'Please verify your email before logging in.' ) ;
27+ throw new UnauthorizedException (
28+ 'Please verify your email before logging in.'
29+ ) ;
2830 }
2931 // User verified, continue login
3032 return user ;
@@ -35,23 +37,28 @@ export class AuthService {
3537
3638 async login ( user : any ) {
3739 const foundUser = await this . usersService . findByEmail ( user . email ) ;
38-
40+
3941 if ( ! foundUser ) {
4042 throw new UnauthorizedException ( 'Invalid credentials' ) ;
4143 }
42-
43- const passwordMatches = await bcrypt . compare ( user . password , foundUser . password ) ;
44+
45+ const passwordMatches = await bcrypt . compare (
46+ user . password ,
47+ foundUser . password
48+ ) ;
4449 if ( ! passwordMatches ) {
4550 throw new UnauthorizedException ( 'Invalid credentials' ) ;
4651 }
47-
52+
4853 if ( ! foundUser . isEmailVerified ) {
49- throw new UnauthorizedException ( 'Email not verified. Please check your email to verify your account.' ) ;
54+ throw new UnauthorizedException (
55+ 'Email not verified. Please check your email to verify your account.'
56+ ) ;
5057 }
51-
58+
5259 // Set user as online
5360 await this . usersService . update ( foundUser . id , { isOnline : true } ) ;
54-
61+
5562 const payload = {
5663 email : foundUser . email ,
5764 sub : foundUser . id ,
@@ -62,7 +69,7 @@ export class AuthService {
6269 userId : foundUser . id ,
6370 } ;
6471 }
65-
72+
6673 async logout ( userId : string ) : Promise < void > {
6774 const user = await this . usersService . findOne ( userId ) ;
6875 if ( ! user ) {
@@ -71,8 +78,7 @@ export class AuthService {
7178 user . isOnline = false ;
7279 await this . usersService . update ( userId , { isOnline : false } ) ;
7380 }
74-
75-
81+
7682 async register ( user : any ) {
7783 const hashedPassword = await bcrypt . hash ( user . password , 10 ) ;
7884 await this . usersService . create ( {
@@ -98,37 +104,39 @@ export class AuthService {
98104
99105 const resetLink = `${ this . configService . get < string > ( 'FRONTEND_URL' ) } /reset-password?token=${ resetToken } ` ;
100106
101- await this . sendGridService . sendPasswordResetEmail ( user . email , resetLink ) ;
107+ await this . mailService . sendPasswordResetEmail ( user . email , resetLink ) ;
102108 }
103109
104110 async resetPassword ( token : string , newPassword : string ) : Promise < void > {
105111 try {
106112 const payload = this . jwtService . verify ( token , {
107113 secret : this . configService . get < string > ( 'RESET_PASSWORD_SECRET' ) ,
108114 } ) ;
109-
115+
110116 const user = await this . usersService . findByEmail ( payload . email ) ;
111-
117+
112118 if ( ! user ) {
113119 throw new NotFoundException ( 'User not found' ) ;
114120 }
115121
116-
117-
118- await this . usersService . update ( user . id , { password : newPassword } ) ;
119-
122+ await this . usersService . update ( user . id , { password : newPassword } ) ;
123+
120124 const updatedUser = await this . usersService . findByEmail ( payload . email ) ;
121-
122- const passwordMatches = await bcrypt . compare ( newPassword , updatedUser . password ) ;
125+
126+ const passwordMatches = await bcrypt . compare (
127+ newPassword ,
128+ updatedUser . password
129+ ) ;
123130 if ( ! passwordMatches ) {
124- throw new InternalServerErrorException ( 'Password update verification failed' ) ;
131+ throw new InternalServerErrorException (
132+ 'Password update verification failed'
133+ ) ;
125134 }
126-
127135 } catch ( error ) {
128136 console . error ( 'Error during password reset:' , error . message ) ;
129137 throw new BadRequestException ( 'Invalid or expired token' ) ;
130138 }
131- }
139+ }
132140
133141 async generateTokens ( userId : string , email : string ) {
134142 const [ accessToken , refreshToken ] = await Promise . all ( [
@@ -137,14 +145,14 @@ export class AuthService {
137145 {
138146 secret : this . configService . get < string > ( 'JWT_SECRET' ) ,
139147 expiresIn : this . configService . get < string > ( 'JWT_EXPIRES_IN' ) ,
140- } ,
148+ }
141149 ) ,
142150 this . jwtService . signAsync (
143151 { sub : userId , email } ,
144152 {
145153 secret : this . configService . get < string > ( 'JWT_REFRESH_SECRET' ) ,
146154 expiresIn : this . configService . get < string > ( 'JWT_REFRESH_EXPIRES_IN' ) ,
147- } ,
155+ }
148156 ) ,
149157 ] ) ;
150158
@@ -161,7 +169,7 @@ export class AuthService {
161169 await this . jwtService . verifyAsync ( refreshToken , {
162170 secret : this . configService . get < string > ( 'JWT_REFRESH_SECRET' ) ,
163171 } ) ;
164-
172+
165173 return this . generateTokens ( userId , refreshToken ) ;
166174 } catch {
167175 throw new UnauthorizedException ( 'Invalid refresh token' ) ;
0 commit comments