-
-
Save DashBarkHuss/859959b5979913f12da6bec2682be58b to your computer and use it in GitHub Desktop.
Node.js, Passport.js, connect-flash message when login. Doesn't work with axios
This file contains hidden or 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
{ | |
"dependencies": { | |
"axios": "^0.19.2", | |
"body-parser": "^1.9.0", | |
"connect-flash": "^0.1.1", | |
"cookie-parser": "^1.4.5", | |
"express": "^4.17.1", | |
"express-session": "^1.17.1", | |
"passport": "^0.4.1", | |
"passport-local": "^1.0.0" | |
} | |
} |
This file contains hidden or 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
var express = require("express"), | |
session = require("express-session"), | |
cookieParser = require("cookie-parser"), | |
bodyParser = require("body-parser"), | |
passport = require("passport"), | |
LocalStrategy = require("passport-local").Strategy, | |
axios = require("axios"); | |
flash = require("connect-flash"); | |
var app = express(); | |
// Test user so don't need database | |
var USER = { | |
email: "[email protected]", | |
validPassword: function (passwd) { | |
return passwd === "test"; | |
}, | |
}; | |
var getUser = function (email, cb) { | |
if (email !== "[email protected]") return cb(null, null); | |
return cb(null, USER); | |
}; | |
passport.serializeUser(function (user, cb) { | |
cb(null, user.email); | |
}); | |
passport.deserializeUser(function (email, cb) { | |
cb(null, USER); | |
}); | |
passport.use( | |
new LocalStrategy( | |
{ | |
usernameField: "email", | |
}, | |
async (username, password, done) => { | |
try { | |
const user = await getUser(username, function (err, user) { | |
return user ? user : null; | |
}); | |
if (!user) { | |
return done(null, false, { message: "Invalid username." }); | |
} | |
const passwordOK = await USER.validPassword(password); | |
if (!passwordOK) { | |
return done(null, false, { message: "invalid password." }); | |
} | |
console.log(`about to return done`); | |
return done(null, user); | |
} catch (err) { | |
console.log(`error logging in: ${err}`); | |
return done(null); | |
} | |
} | |
) | |
); | |
app.use(cookieParser()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
app.use( | |
session({ | |
secret: "secret cat", | |
resave: true, | |
saveUninitialized: true, | |
}) | |
); | |
app.use(flash()); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.get("/login", (req, res) => { | |
const flashmsg = req.flash("error")[0]; | |
if (req.query.error === "false") return res.send(`Welcome `); | |
return res.send("Error from Flash Message: " + flashmsg); | |
}); | |
app.post( | |
"/login", | |
passport.authenticate("local", { | |
successRedirect: "/login?error=false", | |
failureRedirect: "/login?error=true", | |
failureFlash: true, | |
}) | |
); | |
axios | |
.post("http://localhost:3300/login", { | |
email: "[email protected]", | |
password: "pasksword", //wrong password, should give us a flash message | |
}) | |
.then((response) => { | |
// handle success | |
console.log("axios--" + response.data); | |
}) | |
.catch((error) => { | |
// handle error | |
console.log(error.message); | |
}); | |
app.listen(3300); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment