I'm trying to authenticate Users with a Passport local Strategy. When Registering a user, the user is created in the DB, but is not redirected correctly. It just sends a "Bad Request" in the browser. Same thing happens for Logging in--just a "Bad Request". Cookie is still created and the user is actually logged in, just not redirected.Most of the code is from a Udemy Course, and it works flawlessly in another Project I tried it. It is literally the same code.All npm packages are installed in Package.json.Here is my app.js:const express = require("express");const bodyParser = require("body-parser");const ejs = require("ejs");const mongoose = require("mongoose");const session = require("express-session");const passport = require("passport");const passportLocalMongoose = require("passport-local-mongoose");const app = express();app.use(express.static("public"));app.set("view engine", "ejs");app.set('views', [__dirname + '/views/admin', __dirname + '/views/admin/partials', __dirname + '/views/login', __dirname + '/views/reshape']);app.use(bodyParser.urlencoded({extended: true}));app.use(session({secret: "secret",resave: false,saveUninitialized: false}));app.use(passport.initialize());app.use(passport.session());mongoose.connect("mongodb://localhost:27017/userDB", {useNewUrlParser: true});mongoose.set("useCreateIndex", true);const userSchema = new mongoose.Schema({name: String,email: String,password: String,role: String,});userSchema.plugin(passportLocalMongoose);const User = new mongoose.model("User", userSchema);passport.use(User.createStrategy());passport.serializeUser(User.serializeUser());passport.deserializeUser(User.deserializeUser());app.get("/", function(req, res) {res.render("home");});app.get("/login", function(req, res) {res.render("pages-login");});app.get("/logout", function(req, res) {res.render("pages-logout");});app.get("/register", function(req, res) {res.render("pages-register");});app.get("/forgot_password", function(req, res) {res.render("pages-recoverpw");});app.get("/admin", function(req, res) {if (req.isAuthenticated()) {res.render("index");} elseres.redirect("/login");});app.get("/product-keys", function(req, res) {res.render("product-keys");});app.post("/register", function(req, res, next) {User.register({username: req.body.registerEmail,name: req.body.registerName}, req.body.registerPassword, function(err, user) {if (err) {console.log(err);res.redirect("/register");} else {passport.authenticate("local")(req, res, function() {console.log(err);res.redirect("/admin");});}});});app.post("/login", function(req, res) {const user = new User({username: req.body.loginEmail,password: req.body.loginPassword});req.login(user, function(err) {if (err) {console.log(err);} else {passport.authenticate("local")(req, res, function() {res.redirect("/admin");});}});});app.listen(3000, function() {console.log("Server started on 3000");});I would appreciate any help. Thanks!
Submitted May 29, 2019 at 09:00PM by DerQualle
No comments:
Post a Comment