Unable to get profile information in callback, But able to found that information in serializeUser function

74 Views Asked by At

I am trying to authenticate a user using passport-github strategy. It seems like everything working fine except I am unable to fetch profile information inside call back. But Able to get that information in serializeUser function.

var configurePassport = {
    configureGithubStrategy : (app) => {

        // STEP 1 : [Initialize passport into the app]

        app.use(passport.initialize())
        app.use(passport.session()); 

        // STEP 2 : [Implement serialize and deserialize objects of passport]

        passport.serializeUser(function(profile,done){
            console.log(`+++ Inside Serialize +++\n${profile.username}\n+++ Note : Able The Get Profile Information +++`)
            done(null,profile);
        })

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

        // STEP 3 : [Configure GitHub Strategy]

        passport.use(new GitHubStrategy({
            clientID: config.gitHub.clientID,
            clientSecret: config.gitHub.clientSecret,
            callbackURL: config.gitHub.callbackURL
          },
          function(accessToken, refreshToken, profile, done) {
            console.log(`+++ Inside Configuration +++\n${profile}`)
            done(null,profile)
          }
        ));
    }
}
  const passport = require('passport')
  const configurePassport = require('./config/passportConfig')


  configurePassport.configureGithubStrategy(app)

  app.get('/auth/github',passport.authenticate('github'));


  app.get('/auth/github/callback',passport.authenticate('github',{failureRedirect : '/auth/failed'}),function(request,response){
    console.log(`Unable To Get Profile Information -> ${request.profile}`)
    response.redirect('/profile')    
  })


  app.get('/profile',function(request,response){
    response.status(200).json(request.profile)
  })

Inside callback function request.profile gets undefiend

1

There are 1 best solutions below

0
On

Correct me If I am wrong, But I found that the done() function attaches the profile info on the request object as user so it's available on the callback url as request.user