diff --git a/src/controllers/authController.ts b/src/controllers/authController.ts index eefdf0d..6f013cf 100644 --- a/src/controllers/authController.ts +++ b/src/controllers/authController.ts @@ -18,6 +18,7 @@ import getAllUsers from '../services/userServices/getAllUsers'; import getUserById from '../services/userServices/getUserById'; import getUserProfile from '../services/userServices/getUserProfile'; import userUpdateProfilePicture from '../services/userServices/userUpdateProfileImage'; +import googleAuth from '../services/userServices/googleAuthservice'; export const userRegistration = async (req: Request, res: Response) => { await userRegistrationService(req, res); @@ -87,3 +88,6 @@ export const getUserProfileController = async (req: Request, res: Response) => { export const userUpdateProfilePictureController = async (req: Request, res: Response) => { await userUpdateProfilePicture(req, res); }; +export const googleOAuthController = async (req: Request, res: Response) => { + await googleAuth(req, res); +}; diff --git a/src/routes/UserRoutes.ts b/src/routes/UserRoutes.ts index 6a0cdd9..d4eec2b 100644 --- a/src/routes/UserRoutes.ts +++ b/src/routes/UserRoutes.ts @@ -1,7 +1,4 @@ import { RequestHandler, Router } from 'express'; -import { responseError } from '../utils/response.utils'; -import { UserInterface } from '../entities/User'; -import jwt from 'jsonwebtoken'; import { disable2FA, enable2FA, @@ -17,6 +14,7 @@ import { getUserByIdController, getUserProfileController, userUpdateProfilePictureController, + googleOAuthController, } from '../controllers'; import { activateUser, disactivateUser, userProfileUpdate } from '../controllers/index'; @@ -24,10 +22,6 @@ import { hasRole } from '../middlewares/roleCheck'; import upload from '../middlewares/multer'; import passport from 'passport'; import '../utils/auth'; -import { start2FAProcess } from '../services/userServices/userStartTwoFactorAuthProcess'; -import { otpTemplate } from '../helper/emailTemplates'; -import { sendOTPEmail } from '../services/userServices/userSendOTPEmail'; -import { sendOTPSMS } from '../services/userServices/userSendOTPMessage'; import { authMiddleware } from '../middlewares/verifyToken'; const router = Router(); @@ -53,57 +47,9 @@ router.get('/google-auth', passport.authenticate('google', { scope: ['profile', router.get( '/auth/google/callback', passport.authenticate('google', { - successRedirect: `${process.env.CLIENT_URL}/login/google-auth`, - failureRedirect: `${process.env.CLIENT_URL}/login/google-auth`, - }) + failureRedirect: `${process.env.CLIENT_URL}/login/google-auth?status='GoogleOAuthFailure'`, + }), + googleOAuthController ); -router.get('/login/success', async (req, res) => { - const user = req.user as UserInterface; - - if (!user) { - responseError(res, 404, 'user not found'); - return; - } - - if (user.status === 'suspended') { - return res.status(400).json({ status: 'error', message: 'Your account has been suspended' }); - } - - 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.status(200).json({ - status: 'success', - data: { - token: token, - message: 'Login success', - }, - }); - } - 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.status(200).json({ - status: 'success', - data: { - email: user.email, - message: 'Please provide the OTP sent to your email or phone', - }, - }); -}); - -router.get('/login/failed', async (req, res) => { - res.status(401).json({ - status: false, - message: 'Login failed', - }); -}); export default router; diff --git a/src/services/index.ts b/src/services/index.ts index 067b53f..34b9fb0 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -9,6 +9,7 @@ export * from './userServices/userLoginService'; export * from './userServices/userResendOTP'; export * from './userServices/logoutServices'; export * from './userServices/userProfileUpdateServices'; +export * from './userServices/googleAuthservice'; // Vendor product services diff --git a/src/services/userServices/googleAuthservice.ts b/src/services/userServices/googleAuthservice.ts new file mode 100644 index 0000000..97268b9 --- /dev/null +++ b/src/services/userServices/googleAuthservice.ts @@ -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; \ No newline at end of file