Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport-auth0 access accessToken

Since sometime I am using auth0 with express. But now I have one question. This is how my code looks like:

var passport = require('passport');
var Auth0Strategy = require('passport-auth0');

var strategy = new Auth0Strategy({
    domain: '',
    clientID: '',
    clientSecret: '',
    callbackURL: '/loginapi/callback'
}, function (accessToken, refreshToken, extraParams, profile, done) {
    // accessToken is the token to call Auth0 API (not needed in the most cases)
    // extraParams.id_token has the JSON Web Token
    // profile has all the information from the user
    return done(null, profile);
});

passport.use(strategy);

// This is not a best practice, but we want to keep things simple for now
passport.serializeUser(function (user, done) {
    done(null, user);
});

passport.deserializeUser(function (user, done) {
    done(null, user);
});

module.exports = strategy;

But how can I access the accessToken in a express request like the user element. I really don't know how, but I already tried some things.

Nils

like image 710
Noim Avatar asked Jun 30 '16 13:06

Noim


1 Answers

I got it guys!

var strategy = new Auth0Strategy({
    domain: '',
    clientID: '',
    clientSecret: '',
    callbackURL: '/loginapi/callback'
}, function (accessToken, refreshToken, extraParams, profile, done) {
    // accessToken is the token to call Auth0 API (not needed in the most cases)
    // extraParams.id_token has the JSON Web Token
    // profile has all the information from the user
    var info = {
        "profile": profile,
        "accessToken": accessToken,
        "refreshToken": refreshToken,
        "extraParams": extraParams
    };
    return done(null, info);
});

Now I can simply access the accessToken with the req.user object.

like image 193
Noim Avatar answered Nov 03 '22 02:11

Noim