From a9833797a27d35984ebf1321d4f4feb97cc3d7cf Mon Sep 17 00:00:00 2001 From: andrzej Date: Wed, 29 May 2024 11:34:21 +0200 Subject: [PATCH] got token to return! --- auth/auth.mts | 6 ++-- dist/auth/auth.mjs | 6 ++-- dist/routes/routes.mjs | 60 +++++++++++++++-------------------- routes/routes.mts | 72 +++++++++++++++++++++--------------------- 4 files changed, 69 insertions(+), 75 deletions(-) diff --git a/auth/auth.mts b/auth/auth.mts index 963aa8d..8da818c 100644 --- a/auth/auth.mts +++ b/auth/auth.mts @@ -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) } diff --git a/dist/auth/auth.mjs b/dist/auth/auth.mjs index 1e851de..ec39a5f 100644 --- a/dist/auth/auth.mjs +++ b/dist/auth/auth.mjs @@ -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); diff --git a/dist/routes/routes.mjs b/dist/routes/routes.mjs index ae052b5..2756e39 100644 --- a/dist/routes/routes.mjs +++ b/dist/routes/routes.mjs @@ -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; diff --git a/routes/routes.mts b/routes/routes.mts index 96ce729..06ce2b7 100644 --- a/routes/routes.mts +++ b/routes/routes.mts @@ -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