1
votes

I'm trying to perform social auth in my application, and for this i'm using two modules: passport-google-oauth-20 and passport-jwt.

When the user succesfully login with google account, I create a token and send to header as a Bearer token with 'Authorization' header type.

The problem is: When I try to access some protected route, always return unauthorized. Seems like passport.authenicate('jwt') is not being called.

Here's my code:

const express = require('express')
const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt
const GoogleStrategy = require('passport-google-oauth20').Strategy
const passport = require('passport')
const morgan = require('morgan')
const moment = require('moment')
const db = require('./configs/database')
const User = require('./models/user')
const PORT = (process.env.PORT || 8080)
const jwt = require('jsonwebtoken')
const app = express()

app.set(db)
app.use(morgan('dev'))
app.use(function(req, res, next) {  
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials");
    res.header("Access-Control-Allow-Credentials", "true");
    next();
  });




//passport setup
let opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken()
opts.secretOrKey = 'shhhhhhhhhhhh'

passport.use('jwt', new JwtStrategy(opts, function(jwt_payload, done){
    User.findById(jwt_payload.user._id, (err, user)=>{
        if(user){
            return done(null, user)
        } else{
             return done(null, false)
        }
        if(err){
            return done(err, false)
        }
    })
}))

passport.use(new GoogleStrategy({
    clientID: '',
    clientSecret: '',
    callbackURL: "http://localhost:8080/auth/google/callback"
  },
  function(accessToken, refreshToken, profile, cb) {

    User.findOne({'google.id': profile.id }, function (err, user) {
        if(user){
            return cb(null, user)
        }
        else{
            let newUser = {
                'google.id':profile.id,
                'google.name':profile.displayName,
                'google.token':accessToken
            }
            User.create(newUser, (err, user)=>{
                return cb(null, user)
            })
        }
        if(err){
            return cb(err)
        }
    });
  }
));


app.get('/auth/google',
passport.authenticate('google', { scope: ['profile','email'] }));


app.get('/auth/google/callback', 
passport.authenticate('google', { failureRedirect: '/login', session:false}),
    (req, res) => {
    console.log(req.user._id)
    let token = jwt.sign({id:req.user._id, user:req.user, sucess:true}, 'shhhh',{expiresIn:36000})
    res.json({token:'Bearer ' + token})
});


app.get('/dashboard', passport.authenticate('jwt', { session: false }), function(req, res) {  
    res.json({message:'worked', user:req.user})
  });



app.listen(PORT, () => {
    console.log(`Server running on PORT ${PORT}. At: ${moment().format('LT')}`)
})
2

2 Answers

2
votes

https://stackoverflow.com/a/46020083/4548946

The header should include 'Authorization' = 'bearer token_received_on_login' if you are using ExtractJwt.fromAuthHeaderAsBearerToken()

2
votes

Adding for anyone else that runs into this in the future as I recently did, you have to initialize passport:

In a Connect or Express-based application, passport.initialize() middleware is required to initialize Passport. If your application uses persistent login sessions, passport.session() middleware must also be used.

http://www.passportjs.org/docs/configure/