如何解决如何找到错误passport-local-mongoose?
我使用 MERN 堆栈,并且在身份验证中我想使用passport-local-mongoose。但是在我的代码中我找不到错误。请帮我我试过这个问题。
登录或注册后,我想知道 /current
这条路线中的 req.user。
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const session = require('express-session');
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const cors = require('cors');
const app = express();
app.use(cors({
origin: 'http://localhost:3000',credentials: true
}));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(session({
secret: "Our little secret.",resave: false,saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect("mongodb://localhost:27017/QuizApp",{useNewUrlParser: true,useCreateIndex:true,useUnifiedTopology:true});
const userSchema = new mongoose.Schema ({
username: String,password: String,});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User",userSchema);
passport.use(User.createStrategy());
passport.serializeUser(function(user,done) {
done(null,user.id);
});
passport.deserializeUser(function(id,done) {
User.findById(id,function(err,user) {
done(err,user);
});
});
app.get("/logout",function(req,res){
req.logout();
});
app.post("/register",res){
User.register({username: req.body.username},req.body.password,user){
if (err) {
console.log(err);
} else {
passport.authenticate("local")(req,res,function(){
});
}
});
});
app.post("/login",res){
const user = new User({
username: req.body.username,password: req.body.password
});
req.login(user,function(err){
if (err) {
console.log(err);
} else {
passport.authenticate("local")(req,function(){
console.log("ish bitdi");
});
}
});
});
app.get('/current',(req,res) => {
res.send(req.user);
});
app.listen(3001,function() {
console.log("Server started on port 3001.");
});
这是我的 axios 发送代码。我使用 dispatch react-redux
import { USER_LOGIN_FAIL,USER_LOGIN_REQUEST,USER_LOGIN_SUCCESS,USER_REGISTER_FAIL,USER_REGISTER_REQUEST,USER_REGISTER_SUCCESS } from "../constants/all";
import axios from "axios";
const user_Register = (email,password) => async (dispatch) =>{
dispatch({ type: USER_REGISTER_REQUEST });
try {
const {data} = await axios( {
method: "POST",data: {
"username": email,"password": password
},withCredentials: true,url: "http://localhost:3001/register"
});
dispatch({ type: USER_REGISTER_SUCCESS,payload: data });
} catch (error) {
dispatch({type: USER_REGISTER_FAIL,error: error.message});
}
}
const user_Login = (email,password) => async (dispatch) =>{
dispatch({ type: USER_LOGIN_REQUEST });
try {
const {data} = await axios( {
method: "POST",url: "http://localhost:3001/login"
});
dispatch({ type: USER_LOGIN_SUCCESS,payload: data });
} catch (error) {
dispatch({type: USER_LOGIN_FAIL,error: error.message});
}
}
export {user_Register,user_Login};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。