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
195 views
in Technique[技术] by (71.8m points)

node.js - passport.authenticate('local') doesn't redirect me somewhere

I wanted to make a login system with Passport. When I log with bad information, it warns me but when I do it with good credentials, the favicon just spins there

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;

UPDATE: Campgrounds code:

const express = require("express");
const router = express.Router();
const catchAsync = require("../utils/catchAsync");
const ExpressError = require("../utils/ExpressError");
const Campground = require("../models/campground");
const {campgroundSchema} = require("../schemas.js");


const validateCampground = (req, res, next) => {
    const {error} = campgroundSchema.validate(req.body); 
    if(error) {
        const msg = error.details.map(el => el.message).join(", ");
        throw new ExpressError(msg, 400);
    } else {
        next();
    }
}

// The main page
router.get("/", catchAsync(async(req, res) => {
    const campground = await Campground.find({});
    res.render("campgrounds/index", {campground});
}));

// The form page
router.get("/new", (req, res) => {
     res.render("campgrounds/new");
});

// Sending the form
router.post("/", validateCampground, catchAsync(async (req, res, next) => {
    req.flash("success", "Successfully made a new campground!");
    const campground = new Campground(req.body.campground);
    await campground.save();
    res.redirect(`/campgrounds/${campground._id}`);
}));

// The show page
router.get("/:id", catchAsync(async(req, res) => {
    const {id} = req.params;
    const campground = await Campground.findById(id).populate('reviews');
    if(!campground) {
        req.flash("error", "Cannot find that campground!")
       res.redirect("/campgrounds");
    }
    res.render("campgrounds/show", {campground});
}));

// Get the edit page
router.get("/:id/edit", catchAsync(async(req, res) => {
    const {id} = req.params;
    const campground = await Campground.findById(id);
    res.render("campgrounds/edit", {campground});
}));

// Update 
router.put("/:id", validateCampground,  catchAsync(async(req, res, next) =>{
    const {id} = req.params;
    const campground = await Campground.findByIdAndUpdate(id, {...req.body.campground}, {new: true});
    req.flash("success", "Successfully edited campground!");
    res.redirect(`/campgrounds/${campground._id}`);
}));

// Delete
router.delete("/:id", catchAsync(async(req, res) => {
    const {id} = req.params;
    const campground = await Campground.findByIdAndDelete(id);
    req.flash("success", "Successfully deleted campground!");
    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/65916383/passport-authenticatelocal-doesnt-redirect-me-somewhere

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

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

...