Skip to content

Commit

Permalink
Add strategy local with passport in the routes request
Browse files Browse the repository at this point in the history
  • Loading branch information
robisson committed Nov 23, 2018
1 parent aa826e6 commit 68f05fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 56 deletions.
86 changes: 30 additions & 56 deletions src/server/controllers/Authentication/index.js
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);
};
};
4 changes: 4 additions & 0 deletions src/server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import LoginFactory from "../controllers/Authentication";
import { requireAuth } from "../middlewares/requireAuth";
import upload from "../middlewares/upload";
import cors from "cors";
import passport from "passport";
import LocalStrategy from "../config/AuthenticateStrategies/LocalStrategy";

let corsOptions = {
origin: process.env.URL_APPLICATION,
Expand All @@ -19,6 +21,8 @@ const DeleteAccount = DeleteAccountFactory(User);
const UpdateUserAccount = UpdateUserAccountFactory(User);
const Login = LoginFactory(User, setCsrf);

passport.use(LocalStrategy(User));

const router = Express.Router();
/**
* @api {post} /api/v1/user Creating account user
Expand Down

0 comments on commit 68f05fb

Please sign in to comment.