I am trying to make an authentication react app. Here I am using express and passportJS for authentication.But the passportConfig function is not calling? Even I fire console.log() it is not showing anything.
**Server.js**
const mongoose = require("mongoose");
const express = require("express");
const cors = require("cors");
const passport = require("passport");
const bodyParser = require("body-parser");
const passportLocal = require("passport-local").Strategy;
const cookieParser = require("cookie-parser");
const bcrypt = require("bcryptjs");
const session = require("express-session");
const User = require("./user")
//----------------------------------------END OF IMPORTS-------------------------------------------------------------//
//Initialize app
const app = express();
mongoose.connect("mongodb+srv://alfa:drivencode@cluster0.nxvit.mongodb.net/mydb?retryWrites=true&w=majority",{
useNewUrlParser:true,
useUnifiedTopology:true,
},()=>{console.log("Database Running..........")});
//-----------------------------------------END OF APP INITIALIZE------------------------------------------------------//
//Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}));
app.use(cors({
origin:"http://localhost:3000",
credentials:true
}));
app.use(session({
secret:"secretcode",
resave:true,
saveUninitialized:true
}));
app.use(cookieParser("secretcode"));
app.use(passport.initialize());
app.use(passport.session());
require('./passportConfig')(passport);
//-------------------------------------------------------END OF MIDDLEWARE-------------------------------------------------//
//Routes
app.post("/login" , (req,res,next)=>{
passport.authenticate('local', function(err, user, info) {
if (err) {console.log('Error'); return next(err); }
if (!user) { return res.send('Not success'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.send('Success');
});
})(req, res, next);
console.log(req.body);
});
app.post("/user", (req,res)=>{
console.log(req.body);
User.findOne({email:req.body.email} , async (error,doc)=>{
if(error)throw error;
if(doc)res.send("User Already Exist");
if(!doc){
const newUser = new User({
email:req.body.email,
password:req.body.password
});
await newUser.save();
res.send("User created");
}
})
})
//-------------------------------------------------------END OF ROUTES-----------------------------------------------------//
//Start Server
app.listen(4000,()=>{
console.log("Server started port 4000");
});
question from:
https://stackoverflow.com/questions/65948318/why-passportjs-is-not-calling-properly-for-local-strategy 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…