Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

express - Why PassportJs is not calling properly for local strategy?

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

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...