Skip to content

Commit

Permalink
fix(validation):joi validation for login
Browse files Browse the repository at this point in the history
-login data from request.body are validated by joi

[Fixes #187419169]
  • Loading branch information
niyobertin committed Apr 24, 2024
1 parent 1a54b1d commit 462f7c7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion __test__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test('should return 400 when registering with an invalid credential', async () =
test("Should return status 401 to indicate Unauthorized user",async() =>{
const loggedInUser ={
email:userData.email,
password:"test",
password:"test12345",
};
const spyonOne = jest.spyOn(User,"findOne").mockResolvedValueOnce({
//@ts-ignore
Expand Down
9 changes: 5 additions & 4 deletions src/controllers/userControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ export const userLogin = async(req:Request,res:Response) =>{
const user = await loggedInUser(email);
const accessToken = await generateToken(user);
if(!user){
res.status(404).json({
status:404,
message:'User Not Found ! Please Register new ancount'

res.status(401).json({
status:401,
message:'Invalid credentials'
});
}else{
const match = await comparePasswords(password,user.password);
if(!match){
res.status(401).json({
status:401,
message:' User email or password is incorrect!'
message:' Invalid credentials'
});
}else{
res.status(200).json({
Expand Down
21 changes: 21 additions & 0 deletions src/helpers/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Request,Response,NextFunction } from "express";
export const dataValidation = async(req:Request,res:Response,next:NextFunction,data:any) => {
const {error} = data.validate(req.body);
if(error){
return res.status(406)
.json({
status:406,
mesage:`Error in User Data : ${error.message}`
})
}
const allowedFields = Object.keys(data.describe().keys);
const unknownFields = Object.keys(req.body).filter(field => !allowedFields.includes(field));
if (unknownFields.length > 0) {
return res.status(406).json({
status: 406,
message: `Unknown fields: ${unknownFields.join(", ")}`
});
}else{
next();
}
}
3 changes: 2 additions & 1 deletion src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
validateSchema,
} from "../middleware/validator";
import signUpSchema from "../schemas/signUpSchema";
import { loginSchema } from "../schemas/signUpSchema";

const userRoutes = Router();

userRoutes.get("/", fetchAllUsers);
userRoutes.post('/login',userLogin);
userRoutes.post('/login',emailValidation,validateSchema(loginSchema),userLogin);
userRoutes.post("/register",
emailValidation,
validateSchema(signUpSchema),
Expand Down
12 changes: 12 additions & 0 deletions src/schemas/signUpSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ export const signUpSchema = Joi.object({
.required()
}).options({ allowUnknown: false });


export const loginSchema = Joi.object({
email: Joi.string()
.min(6)
.required()
.email(),
password: Joi.string()
.min(6)
.max(20)
.required()
}).options({ allowUnknown: false });

export default signUpSchema

0 comments on commit 462f7c7

Please sign in to comment.