Skip to content

Commit

Permalink
removed skip
Browse files Browse the repository at this point in the history
  • Loading branch information
MugemaneBertin2001 committed Apr 30, 2024
1 parent 3ac1046 commit e292f32
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
4 changes: 2 additions & 2 deletions __test__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -190,4 +190,4 @@ describe("Testing Google auth", () => {
expect(response.status).toBe(302);
expect(response.header['location']).toContain("redirect_uri");
});
});
});
46 changes: 45 additions & 1 deletion src/controllers/userControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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";
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);
Expand Down
27 changes: 27 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
2 changes: 1 addition & 1 deletion src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand All @@ -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);
Expand Down

0 comments on commit e292f32

Please sign in to comment.