-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(google-auth): fixes google authentication issue
- Loading branch information
Showing
4 changed files
with
52 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Request, Response } from 'express'; | ||
import { responseError } from '../../utils/response.utils'; | ||
import { UserInterface } from '../../entities/User'; | ||
import jwt from 'jsonwebtoken'; | ||
import { start2FAProcess } from './userStartTwoFactorAuthProcess'; | ||
import { otpTemplate } from '../../helper/emailTemplates'; | ||
import { sendOTPEmail } from './userSendOTPEmail'; | ||
import { sendOTPSMS } from './userSendOTPMessage'; | ||
|
||
const googleAuth = async (req: Request, res: Response) => { | ||
try { | ||
const user = req.user as UserInterface; | ||
if (!user) { | ||
return res.redirect(`${process.env.CLIENT_URL}/login/google-auth?status=userNotfound`); | ||
} | ||
|
||
if (user.status === 'suspended') { | ||
return res.redirect(`${process.env.CLIENT_URL}/login/google-auth?status=userSuspended`); | ||
} | ||
|
||
if (!user.twoFactorEnabled) { | ||
const payload = { | ||
id: user?.id, | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
email: user?.email, | ||
role: user?.role, | ||
}; | ||
const token = jwt.sign(payload, process.env.JWT_SECRET as string, { expiresIn: '24h' }); | ||
return res.redirect(`${process.env.CLIENT_URL}/login/google-auth?status=success&token=${token}&role=${user.role?.toLowerCase()}`); | ||
} | ||
|
||
const otpCode = await start2FAProcess(user.email); | ||
const OTPEmailcontent = otpTemplate(user.firstName, otpCode.toString()); | ||
await sendOTPEmail('Login OTP Code', user.email, OTPEmailcontent); | ||
await sendOTPSMS(user.phoneNumber, otpCode.toString()); | ||
return res.redirect(`${process.env.CLIENT_URL}/login/google-auth?status=otp&email=${user.email}`); | ||
} catch (error) { | ||
return res.redirect(`${process.env.CLIENT_URL}/login/google-auth?status=error`); | ||
} | ||
}; | ||
|
||
export default googleAuth; |