简体   繁体   中英

Github oAuth with PassportJS not authenticated

I'm trying to use passportJS to authenticate with the github-strategy. I've previously used passportJS with success, but not with the github-strategy, though I don't figure it should be much different. /viewData is the route in which I'm testing for authentication.

The strategy authenticates successfully and puts the data in the DB and when I serialize and deserialize the user data is in fact there. The problem comes when I try to check in any route after if the req.isAuthenticated() or even if req.session.passport.user exists and it returns false and undefined respectively. The weird thing the same check in app.js after I've redirected through /testAuth the authentication logs correctly with the true and id#. So I feel like its some issue with express-session or the passport integration with that that's breaking it, but I can't find it. I've been on this for a long time so hopefully someone can help me.

app.use(cookieParser('test'));
app.use(session({
    secret: 'test',
    resave: false,
    saveUninitialized: true
}));
//pasport setup
app.use(passport.initialize());
app.use(passport.session());

// Passport init
passport.use(new GithubStrategy({
    clientID: config.github.clientID,
    clientSecret: config.github.clientSecret,
    callbackURL: config.github.callbackURL },
    function(accessToken, refreshToken, profile, done) {
        User.findOne({ oauthID: profile.id }, function(err, user) {
            if(err) {
                console.log(err);  // handle errors!
            }
            if (!err && user !== null) {
                done(null, user);
            } else {
                user = new User({
                    oauthID: profile.id,
                    name: profile.displayName,
                    created: Date.now()
                });
                user.save(function(err) {
                    if(err) {
                        console.log(err);  // handle errors!
                    } else {
                       console.log("saving user ...");
                       done(null, user);
                    }
                });
            }
        });
    }
));

// test authentication
function ensureAuthenticated(req, res, next) {
    if (req.isAuthenticated()) { return next(); }
    console.log("Not Authenticated", req.user);
    res.redirect('/');
}

app.get('/testAuth', ensureAuthenticated, function(req, res){
    User.findById(req.session.passport.user, function(err, user) {
        if(err) {
            console.log(err);  // handle errors
        } else {
            res.redirect('/viewData');
        }
    });
});

//auth pages
app.get('/auth/github',
    passport.authenticate('github'),
    function(req, res){});
app.get('/auth/github/callback',
    passport.authenticate('github', { failureRedirect: '/' }),
    function(req, res) {
        res.redirect('/testAuth');
    });

// serialize and deserialize for session
passport.serializeUser(function(user, done) {
    console.log('serializeUser: ' + user);
    done(null, user._id);
});
passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user){
        console.log('deserializeUser: ' + user);
        if(!err) done(null, user);
        else done(err, null);
    });
});

So this is a weird one, but I was fixing something else that was weird and in doing so solved this problem. I just moved my

app.use('/', index);
app.use('/viewData', viewData);

To below all the passport code and it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM