node.js - .isAuthenticated() returns false even if I am logged in -
i have been working on project requires github log in , used passport-github2 npm, reason, after log in (i did console.log success message if logged in), when run .isauthenticated() returns false. set session cookie , all, , don't know what's wrong it.
here code auth.js file
const express = require('express'); const router = express.router(); const githubstrategy = require('passport-github2'); const passport = require('passport'); const user = require('../models/user.model'); const venue = require('../models/venue.model'); router.post('/', (req, res) => { if (req.user) { //first check see if user attending bar, if decrement venue.findoneandupdate({ id: req.body.barid, totalattending: 1, usersattending: req.user.username || req.user.displayname }, { $set: { 'totalattending': 0 } }, { new: true }, (err, venue) => { if (err) throw err; if (!venue) { venue.findoneandupdate({ id: req.body.barid }, { $inc: { 'totalattending': 1 }, $addtoset: { 'usersattending': req.user.username || req.user.displayname } }, { new: true }, (err, venue) => { if (err) throw err; res.send('done'); }); } else { res.send('done'); } } ); } else { res.send('please sign in first'); } }); router.get('/', passport.authenticate('github')); //passport setup passport.use(new githubstrategy({ clientid: "e5b156bfabd594e3ce1e", clientsecret: "e5e13f4d75a3046aa4e1d581fc635702ceadc209", callbackurl: "https://nightlife-app-elisal.c9users.io/auth/github/callback" }, (accesstoken, refreshtoken, profile, done) => { //create user user.findone({ username: profile.username }, (err, user) => { if (err) return done(err); if (!user) { var newuser = new user({ username: profile.username, displayname: profile.displayname }).save((err, user) => { console.log('error'); if (err) throw err; done(null, user); }); } else { done(null, user); } }); } )); passport.serializeuser((user, done) => { done(null, user); }); passport.deserializeuser((user, done) => { done(null, user); }); router.get('/callback', passport.authenticate('github', { failiureredirect: '/' }), (req, res) => { res.redirect('/'); console.log("you're logged in"); }); module.exports = router;
and here server.js file
const yelp = require('yelp-fusion'); const express = require('express'); const app = express(); const token = "xxxxxxxxxx"; const client = yelp.client(token); const home = require("./routes/home"); const mongoose = require("mongoose"); const passport = require('passport'); const session = require("express-session"); const auth = require("./routes/auth"); mongoose.connect('xxxxxxxx'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error')); db.once('open', () => { console.log('connection database successful'); }); app.use(passport.initialize()); app.use(passport.session()); app.use(express.static(__dirname + '/public')); app.set('port', process.env.port || process.env.ip ); app.set('view engine', 'ejs'); app.use(session({ secret: 'elktrjosd0983jlwsf08easd90', resave: true, saveuninitialized: true, })); //sets links different sites app.use("/", home); app.use("/auth/github", auth); //starts port var port = process.env.port || 3000; app.listen(port, function () { console.log('node.js listening on port ' + port + '...'); });
does know why .isauthenticated() returns false , how solve it?
Comments
Post a Comment