got token to return!
This commit is contained in:
parent
17fa8939a2
commit
a9833797a2
|
@ -17,7 +17,7 @@ passport.use('signup', new localStrategy(
|
|||
}
|
||||
}))
|
||||
|
||||
passport.use(
|
||||
passport.use('login',
|
||||
new localStrategy(
|
||||
{
|
||||
usernameField: "email",
|
||||
|
@ -25,6 +25,7 @@ passport.use(
|
|||
session: false
|
||||
},
|
||||
async (email, password, done) => {
|
||||
console.log("local strategy called")
|
||||
try {
|
||||
const user: User = await UserModel.findOne({ email })
|
||||
console.log(`user: ${user}`)
|
||||
|
@ -33,12 +34,13 @@ passport.use(
|
|||
}
|
||||
|
||||
const validate: boolean = await user.isValidPassword(password)
|
||||
console.log(`isValidPassword? ${validate}`)
|
||||
|
||||
if (!validate) {
|
||||
return done(null, false, { message: "wrong password" })
|
||||
}
|
||||
|
||||
return done(null, false, { message: "logged in successfully" })
|
||||
return done(null, user, { message: "logged in successfully" })
|
||||
} catch (error) {
|
||||
return done(error)
|
||||
}
|
||||
|
|
|
@ -23,11 +23,12 @@ passport.use('signup', new localStrategy({
|
|||
done(err);
|
||||
}
|
||||
})));
|
||||
passport.use(new localStrategy({
|
||||
passport.use('login', new localStrategy({
|
||||
usernameField: "email",
|
||||
passwordField: "password",
|
||||
session: false
|
||||
}, (email, password, done) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
console.log("local strategy called");
|
||||
try {
|
||||
const user = yield UserModel.findOne({ email });
|
||||
console.log(`user: ${user}`);
|
||||
|
@ -35,10 +36,11 @@ passport.use(new localStrategy({
|
|||
return done(null, false, { message: "user not found" });
|
||||
}
|
||||
const validate = yield user.isValidPassword(password);
|
||||
console.log(`isValidPassword? ${validate}`);
|
||||
if (!validate) {
|
||||
return done(null, false, { message: "wrong password" });
|
||||
}
|
||||
return done(null, false, { message: "logged in successfully" });
|
||||
return done(null, user, { message: "logged in successfully" });
|
||||
}
|
||||
catch (error) {
|
||||
return done(error);
|
||||
|
|
|
@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|||
};
|
||||
import express from "express";
|
||||
import passport from "passport";
|
||||
import jwt from 'jsonwebtoken';
|
||||
const router = express.Router();
|
||||
router.post("/signup", passport.authenticate("signup", { session: false }), (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
res.json({
|
||||
|
@ -16,39 +17,28 @@ router.post("/signup", passport.authenticate("signup", { session: false }), (req
|
|||
user: req.user
|
||||
});
|
||||
}));
|
||||
router.post("/login", passport.authenticate('local'), function (req, res) {
|
||||
console.log("BOOM!");
|
||||
});
|
||||
// router.post(
|
||||
// '/login',
|
||||
// async (req, res, next) => {
|
||||
// passport.authenticate(
|
||||
// 'login',
|
||||
// async (err, user, info) => {
|
||||
// try {
|
||||
// if (err || !user) {
|
||||
// const error = new Error('An error occurred.');
|
||||
//
|
||||
// return next(error);
|
||||
// }
|
||||
//
|
||||
// req.login(
|
||||
// user,
|
||||
// { session: false },
|
||||
// async (error) => {
|
||||
// if (error) return next(error);
|
||||
//
|
||||
// const body = { _id: user._id, email: user.email };
|
||||
// const token = jwt.sign({ user: body }, 'TOP_SECRET');
|
||||
//
|
||||
// return res.json({ token });
|
||||
// }
|
||||
// );
|
||||
// } catch (error) {
|
||||
// return next(error);
|
||||
// }
|
||||
// }
|
||||
// )(req, res, next);
|
||||
// }
|
||||
// );
|
||||
// router.post("/login", passport.authenticate('local'),
|
||||
// function(req, res) {
|
||||
// res.json({ res })
|
||||
// })
|
||||
router.post('/login', (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
passport.authenticate('login', (err, user, info) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
try {
|
||||
if (err || !user) {
|
||||
const error = new Error('An error occurred.');
|
||||
return next(error);
|
||||
}
|
||||
req.login(user, { session: false }, (error) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
if (error)
|
||||
return next(error);
|
||||
const body = { _id: user._id, email: user.email };
|
||||
const token = jwt.sign({ user: body }, 'TOP_SECRET');
|
||||
return res.json({ token });
|
||||
}));
|
||||
}
|
||||
catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}))(req, res, next);
|
||||
}));
|
||||
export default router;
|
||||
|
|
|
@ -12,41 +12,41 @@ router.post("/signup",
|
|||
user: req.user
|
||||
})
|
||||
})
|
||||
router.post("/login", passport.authenticate('local'),
|
||||
function(req, res) {
|
||||
console.log("BOOM!")
|
||||
})
|
||||
// router.post(
|
||||
// '/login',
|
||||
// async (req, res, next) => {
|
||||
// passport.authenticate(
|
||||
// 'login',
|
||||
// async (err, user, info) => {
|
||||
// try {
|
||||
// if (err || !user) {
|
||||
// const error = new Error('An error occurred.');
|
||||
//
|
||||
// return next(error);
|
||||
// }
|
||||
//
|
||||
// req.login(
|
||||
// user,
|
||||
// { session: false },
|
||||
// async (error) => {
|
||||
// if (error) return next(error);
|
||||
//
|
||||
// const body = { _id: user._id, email: user.email };
|
||||
// const token = jwt.sign({ user: body }, 'TOP_SECRET');
|
||||
//
|
||||
// return res.json({ token });
|
||||
// }
|
||||
// );
|
||||
// } catch (error) {
|
||||
// return next(error);
|
||||
// }
|
||||
// }
|
||||
// )(req, res, next);
|
||||
// }
|
||||
// );
|
||||
// router.post("/login", passport.authenticate('local'),
|
||||
// function(req, res) {
|
||||
// res.json({ res })
|
||||
// })
|
||||
router.post(
|
||||
'/login',
|
||||
async (req, res, next) => {
|
||||
passport.authenticate(
|
||||
'login',
|
||||
async (err, user, info) => {
|
||||
try {
|
||||
if (err || !user) {
|
||||
const error = new Error('An error occurred.');
|
||||
|
||||
return next(error);
|
||||
}
|
||||
|
||||
req.login(
|
||||
user,
|
||||
{ session: false },
|
||||
async (error) => {
|
||||
if (error) return next(error);
|
||||
|
||||
const body = { _id: user._id, email: user.email };
|
||||
const token = jwt.sign({ user: body }, 'TOP_SECRET');
|
||||
|
||||
return res.json({ token });
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
)(req, res, next);
|
||||
}
|
||||
);
|
||||
|
||||
export default router
|
||||
|
|
Loading…
Reference in New Issue