-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add strategy local with passport in the routes request
- Loading branch information
Showing
2 changed files
with
34 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,40 @@ | ||
import jwt from "jsonwebtoken"; | ||
import bcrypt from "bcrypt-nodejs"; | ||
import path from "path"; | ||
import passport from "passport"; | ||
import { UNAUTHORIZED, OK } from "../../utils/HttpStatusCode"; | ||
import LocalStrategy from "../../config/AuthenticateStrategies/LocalStrategy"; | ||
|
||
export default (User, setCsrf) => { | ||
return (req, res, next) => { | ||
const { email, password } = req.body; | ||
|
||
//find user by email | ||
User.findOne( | ||
{ | ||
email: email | ||
}, | ||
(err, user) => { | ||
return (req, res) => { | ||
passport.use(LocalStrategy); | ||
passport.authenticate("local", { session: false }, (err, user) => { | ||
if (err || !user) { | ||
return res | ||
.status(UNAUTHORIZED) | ||
.json({ success: false, message: "Password invalid" }) | ||
.end(); | ||
} | ||
req.login(user, { session: false }, err => { | ||
if (err) { | ||
return next(err); | ||
res.send(err); | ||
} | ||
if (user) { | ||
if (!bcrypt.compareSync(password, user.password)) { | ||
return res | ||
.status(UNAUTHORIZED) | ||
.json({ success: false, message: "Password invalid" }) | ||
.end(); | ||
} | ||
|
||
//authentication is valid | ||
let payload = { | ||
_id: user._id, | ||
name: user.name, | ||
lastname: user.lastname, | ||
email: user.email, | ||
phone: user.phone, | ||
image: | ||
typeof user.image == "object" | ||
? path.join(process.env.UPLOAD_PATH, user.image.newFilename) | ||
: "/images/user.png", | ||
firstAccess: user.firstAccess | ||
}; | ||
const token = jwt.sign(payload, process.env.SECRET, { | ||
expiresIn: "24h" | ||
}); | ||
const token = jwt.sign(user, process.env.SECRET, { | ||
expiresIn: "24h" | ||
}); | ||
|
||
setCsrf(req, res, () => | ||
res | ||
.status(OK) | ||
.cookie("token", token, { httpOnly: true }) | ||
.json({ | ||
token, | ||
user: payload, | ||
success: true, | ||
message: "Login succesfull! Redirecting..." | ||
}) | ||
.end() | ||
); | ||
} else { | ||
//user not exists | ||
return res | ||
.status(UNAUTHORIZED) | ||
.json({ success: false, message: "User not found" }) | ||
.end(); | ||
} | ||
} | ||
); | ||
return setCsrf(req, res, () => | ||
res | ||
.status(OK) | ||
.cookie("token", token, { httpOnly: true }) | ||
.json({ | ||
token, | ||
user: user, | ||
success: true, | ||
message: "Login succesfull! Redirecting..." | ||
}) | ||
.end() | ||
); | ||
}); | ||
})(req, res); | ||
}; | ||
}; |
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