我已经构建了一个简单的应用程序,该应用程序允许用户使用护照的Google策略通过Google登录,并且一切正常。
我也尝试添加本地策略,但找不到解决方案。
在注册过程中创建用户时,数据库可以正常工作。但是在注册后无法立即重定向到/profile
。相反,我被重定向到登录页面。当用户尝试访问/profile
页面但未登录时,通常会发生这种情况。
登录似乎最初可以正常工作,因为我可以像预期的那样"/"
被重定向到successRedirect: "/"
。但是,如果我尝试转到个人资料页面,则无法。另外,如果我运行req.isAuthenticated()
,它将返回false。
编辑:似乎身份验证本身可以工作,但是以某种方式不能跟上。如果我在注册后立即运行console.log("req.isAuthenticated")
,则返回true,但是一旦我转到另一条路线(例如:/
),则通过在{{1}中运行console.log("req.isAuthenticated")
该路线的请求我得到了错误。就像我可以登录,但我不能保持登录状态。这可能与Cookie有关吗?有人知道如何解决吗?
以下是一些代码:
app.js
get
passport-local-setup.js
const express = require("express"),
mongoose = require("mongoose"),
bodyParser = require("body-parser"),
cookieSession = require("cookie-session"),
passport = require("passport");
const keys = require("./config/keys"); // requiring keys
const passportSetup = require("./config/passport-setup"); //requiring passport config
const localPassportSetup = require("./config/passport-local-setup"); // requiring local passport config
const authRoutes = require("./routes/auth"); // requiring auth routes
const profileRoutes = require("./routes/profile");
const Thought = require("./models/thought"); // requiring Thought model
//Initialize express app
const app = express();
app.use(express.static(__dirname + "/public"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieSession({
maxAge: 24 * 60 * 60 * 1000,
keys: [keys.session.cookieKey]
}));
//initialize passport
app.use(passport.initialize());
app.use(passport.session());
app.use("/auth", authRoutes); // setup auth routes
app.use("/profile", profileRoutes); // setup profile routes
mongoose.connect('mongodb://localhost/thoughtApp'); // connecting database
auth.js
const passport = require("passport");
const localStrategy = require("passport-local");
const mongoose = require("mongoose");
const LocalUser = require("../models/localUser");
passport.use(new localStrategy(LocalUser.authenticate()))
passport.serializeUser(LocalUser.serializeUser());
passport.deserializeUser(LocalUser.deserializeUser());
localUser.js
const router = require("express").Router();
const passport = require("passport");
const passportLocal = require("passport-local");
const localUser = require("../models/localUser");
const authCheck = function (req, res, next) {
if (!req.user) {
next();
} else {
res.redirect("/");
}
};
//login
router.get("/login", authCheck, (req, res) => {
res.render("login", {user: req.user});
});
router.post("/login", passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login"
}), (req, res) => {
});
// logout
router.get("/logout", (req, res) => {
//handle with passport
req.logout();
res.redirect("/");
});
//register
router.get("/signup", authCheck, (req, res) => {
res.render("signup", {user: req.user});
});
router.post("/signup", (req, res) => {
const newUser = new localUser({username: req.body.username});
localUser.register(newUser, req.body.password, (err, user) => {
if(err) {
console.log(err);
return res.render("/signup");
}
passport.authenticate("local")(req, res, function(){
console.log(req.user);
res.redirect("/profile");
});
})
});
我一直在搜索网络,但是找不到解决方案。为什么这不起作用?