2016-06-30 64 views
3

由於有時我使用express來使用auth0。但現在我有一個問題。 這是我的代碼看起來像:Passport-auth0訪問accessToken

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;

但我怎麼能訪問的accessToken在喜歡的用戶元素的明確要求。我真的不知道如何,但我已經嘗試了一些東西。

Nils

回答

3

我懂了!

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); 
 
});

現在我可以簡單地用req.user對象訪問的accessToken。

相關問題