Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

node.js - passport.authenticate('local') doesn't redirect to some page

I wanted to make a login system with Passport. Everything went well until I tried to login with an username and a password. On invalid/wrong credentials it warns me with a flash message but on valid credentials it justs spins there for an eternity and does literally nothing.

Here's the code

const express = require("express");
const router = express.Router();
const User = require("../models/user");
const catchAsync = require("../utils/catchAsync");
const passport = require("passport");


router.get("/register", (req, res) => {
    res.render("users/register");
});

router.post("/register", catchAsync(async (req, res) => {
    try {
    const {email, username, password} = req.body;
    const user = new User({email, username});
    await User.register(user, password);
    req.flash("success", "Welcome to Yelp Camp!");
    res.redirect("/campgrounds");
    } catch (e) {
        req.flash("error", e.message);
        res.redirect("/register");
    }
}));

router.get("/login", (req, res) => {
    res.render("users/login");
});

router.post("/login", passport.authenticate("local", {failureFlash: true, failureRedirect: "/login"}), (req, res) => {
    req.flash("success", "WELCOME BACK!");
    res.redirect("/campgrounds");
})

module.exports = router;

Even tried with successRedirect from the docs but the results it's still the same. Thanks in advance!

question from:https://stackoverflow.com/questions/65914774/passport-authenticatelocal-doesnt-redirect-to-some-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you should create a get route with this path, you didn't define this route in your project

res.redirect("/campgrounds")

put complate Url as argument to res.redicret

res.redirect("http://127.0.0.1:8080/api/campgrounds")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...