-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(user-Test): implementing sign up using jest
- Loading branch information
1 parent
cef80c8
commit 5cec93e
Showing
9 changed files
with
278 additions
and
223 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
File renamed without changes.
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 |
---|---|---|
@@ -1,82 +1,93 @@ | ||
import { Request } from "express"; | ||
import passport from "passport"; | ||
import { Strategy as LocalStrategy } from "passport-local"; | ||
import { User } from "../database/models/User"; | ||
import { isValidPassword } from "../utils/password.checks"; | ||
import { hashPassword } from "../utils/password"; | ||
|
||
passport.serializeUser(function (user: any, done) { | ||
done(null, user); | ||
}); | ||
|
||
passport.deserializeUser(function (user: any, done) { | ||
done(null, user); | ||
}); | ||
|
||
passport.use( | ||
"signup", | ||
new LocalStrategy( | ||
{ | ||
usernameField: "email", | ||
passwordField: "password", | ||
passReqToCallback: true, | ||
}, | ||
async (req, email, password, done) => { | ||
try { | ||
const data = { | ||
email: email.trim(), | ||
password: await hashPassword(password), | ||
confirmPassword: await hashPassword(req.body.confirmPassword), | ||
userName: | ||
req.body.userName == null | ||
? req.body.email.split("@")[0] | ||
: req.body.userName, | ||
firstName: req.body.firstName, | ||
lastName: req.body.lastName, | ||
role: "BUYER", | ||
}; | ||
const user = await User.create({ ...data }); | ||
if (!user) { | ||
return done(null, false, { | ||
message: "Something went wrong", | ||
}); | ||
} | ||
done(null, user); | ||
} catch (error) { | ||
done(error); | ||
} | ||
}, | ||
), | ||
); | ||
|
||
passport.use( | ||
"login", | ||
new LocalStrategy( | ||
{ | ||
usernameField: "email", | ||
passwordField: "password", | ||
passReqToCallback: true, | ||
}, | ||
async (_req: Request, email, password, done) => { | ||
try { | ||
const user = await User.findOne({ where: { email } }); | ||
|
||
if (!user) return done(null, false, { message: "Wrong credentials!" }); | ||
|
||
const currPassword = user.dataValues.password; | ||
|
||
const isValidPass = await isValidPassword(password, currPassword); | ||
|
||
if (!isValidPass) { | ||
return done(null, false, { message: "Wrong credentials!" }); | ||
} | ||
|
||
return done(null, user); | ||
} catch (error) { | ||
done(error); | ||
} | ||
}, | ||
), | ||
); | ||
|
||
export default passport; | ||
import { Request } from "express"; | ||
import passport from "passport"; | ||
import { Strategy as LocalStrategy } from "passport-local"; | ||
import { User } from "../database/models/User"; | ||
import { isValidPassword } from "../utils/password.checks"; | ||
import { hashPassword } from "../utils/password"; | ||
export interface CustomVerifyOptions { | ||
message: string; | ||
status: string; | ||
statusNumber?: number; | ||
} | ||
passport.serializeUser(function (user: any, done) { | ||
done(null, user); | ||
}); | ||
|
||
passport.deserializeUser(function (user: any, done) { | ||
done(null, user); | ||
}); | ||
|
||
passport.use( | ||
"signup", | ||
new LocalStrategy( | ||
{ | ||
usernameField: "email", | ||
passwordField: "password", | ||
passReqToCallback: true, | ||
}, | ||
async (req, email, password, done) => { | ||
try { | ||
const data = { | ||
email: email.trim(), | ||
password: await hashPassword(password), | ||
confirmPassword: await hashPassword(req.body.confirmPassword), | ||
userName: | ||
req.body.userName == null | ||
? req.body.email.split("@")[0] | ||
: req.body.userName, | ||
firstName: req.body.firstName, | ||
lastName: req.body.lastName, | ||
role: "BUYER", | ||
}; | ||
const userEXist = await User.findOne({ | ||
where: { | ||
email: data.email, | ||
}, | ||
}); | ||
if (userEXist) { | ||
const options: CustomVerifyOptions = { | ||
statusNumber: 409, | ||
message: "User already exist!", | ||
status: "CONFLICT", | ||
}; | ||
return done(null, false, options); | ||
} | ||
const user = await User.create({ ...data }); | ||
done(null, user); | ||
} catch (error) { | ||
done(error); | ||
} | ||
} | ||
) | ||
); | ||
|
||
passport.use( | ||
"login", | ||
new LocalStrategy( | ||
{ | ||
usernameField: "email", | ||
passwordField: "password", | ||
passReqToCallback: true, | ||
}, | ||
async (_req: Request, email, password, done) => { | ||
try { | ||
const user = await User.findOne({ where: { email } }); | ||
|
||
if (!user) return done(null, false, { message: "Wrong credentials!" }); | ||
|
||
const currPassword = user.dataValues.password; | ||
|
||
const isValidPass = await isValidPassword(password, currPassword); | ||
|
||
if (!isValidPass) | ||
return done(null, false, { message: "Wrong credentials!" }); | ||
|
||
return done(null, user); | ||
} catch (error) { | ||
done(error); | ||
} | ||
} | ||
) | ||
); | ||
|
||
export default passport; |
Oops, something went wrong.