diff --git a/__test__/user.test.ts b/__test__/user.test.ts index bb758bb..210de58 100644 --- a/__test__/user.test.ts +++ b/__test__/user.test.ts @@ -109,7 +109,7 @@ describe("Testing user Routes", () => { expect(response.body.message).toBe("OTP verification code has been sent ,please use it to verify that it was you"); // expect(spy).toHaveBeenCalled(); - }, 40000); + }, 100000); test("should log a user in to retrieve a token", async () => { const response = await request(app).post("/api/v1/users/login").send({ @@ -190,4 +190,4 @@ describe("Testing Google auth", () => { expect(response.status).toBe(302); expect(response.header['location']).toContain("redirect_uri"); }); - }); \ No newline at end of file + }); diff --git a/src/controllers/userControllers.ts b/src/controllers/userControllers.ts index 7c62afa..3af90f2 100644 --- a/src/controllers/userControllers.ts +++ b/src/controllers/userControllers.ts @@ -2,7 +2,7 @@ import { Request, Response } from "express"; import * as userService from "../services/user.service"; import { generateToken } from "../utils/jsonwebtoken"; import * as mailService from "../services/mail.service"; -import { IUser, STATUS, SUBJECTS } from "../types"; +import { IUser, STATUS, SUBJECTS, UserProfile } from "../types"; import { comparePasswords } from "../utils/comparePassword"; import { loggedInUser } from "../services/user.service"; import { createUserService, getUserByEmail, updateUserPassword } from "../services/user.service"; @@ -127,10 +127,54 @@ export const updatePassword = async (req: Request, res: Response) => { }; export const handleSuccess = async (req: Request, res: Response) => { + //@ts-ignore + const user:UserProfile = req.user; + try { + const foundUser: any = await User.findOne({ + where:{email: user.emails[0].value} + }) + + if(foundUser){ + const token = await generateToken(foundUser) + return res.status(200).json({ + token: token, + message: 'success', + data: foundUser + }) + }else{ + const newUser: any = await User.create({ + name: user.displayName, + username:user.name.familyName, + email: user.emails[0].value, + password: user.emails[0].value, + }); + const token = generateToken(newUser) + return res.status(201).json({ + token: token, + message: "success", + data: newUser, + }); + + } + + } catch (error: any) { + res.status(500).json({ + message: error.message, + }); + } }; export const handleFailure = async (req: Request, res: Response) => { + try { + res.status(401).json({ + message: "unauthorized", + }); + } catch (error: any) { + res.status(500).json({ + message: error.message, + }); + } }; export const tokenVerification = async (req: any, res: Response) => { diff --git a/src/routes/userRoutes.ts b/src/routes/userRoutes.ts index b607fdf..2e5658c 100644 --- a/src/routes/userRoutes.ts +++ b/src/routes/userRoutes.ts @@ -1,4 +1,5 @@ import { Router } from "express"; +require('../auth/auth') import { fetchAllUsers, createUserController, userLogin, updatePassword, tokenVerification, handleSuccess, handleFailure } from "../controllers/userControllers"; import { emailValidation, validateSchema } from "../middleware/validator"; import signUpSchema from "../schemas/signUpSchema"; @@ -6,7 +7,6 @@ import { isLoggedIn } from "../middlewares/isLoggedIn"; import { passwordUpdateSchema } from "../schemas/passwordUpdate"; import { isTokenFound } from "../middlewares/isTokenFound"; import { authenticateUser, callbackFn } from "../services/user.service"; -require('../auth/auth') const userRoutes = Router(); userRoutes.get("/", fetchAllUsers); diff --git a/src/types.ts b/src/types.ts index 8d90b75..765545c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -21,3 +21,30 @@ export enum STATUS { SUCCESS = "Success", FAILED = "Failed", } +export interface UserProfile { + id: string; + displayName: string; + name: { + familyName: string; + givenName: string; + }; + emails: { + value: string; + verified: boolean; + }[]; + photos: { + value: string; + }[]; + provider: string; + _raw: string; + _json: { + sub: string; + name: string; + given_name: string; + family_name: string; + picture: string; + email: string; + email_verified: boolean; + locale: string; + }; +} diff --git a/src/utils/server.ts b/src/utils/server.ts index 8da78d4..b384e2a 100644 --- a/src/utils/server.ts +++ b/src/utils/server.ts @@ -7,6 +7,7 @@ import passport from "passport"; import session from "express-session"; const app = express(); +app.use(cors()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); @@ -24,7 +25,6 @@ app.use( app.use(passport.initialize()); app.use(passport.session()); -app.use(cors()); app.use("/", homeRoute); app.use("/api/v1", appROutes);