Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure passport-github to acknowledge the user on callback?

I have an service and my goal is to let a user auth us to his / her Github account via OAuth. The API layer is stateless, so I don't want to maintain any session information. The code I have so far is:

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

  passport.use new GitHubStrategy
    clientID: global.config.oauth.github.clientID
    clientSecret: global.config.oauth.github.clientSecret
    callbackURL: "http://localhost:9500/auth/github/callback"
    passReqToCallback: true
  , (req, accessToken, refreshToken, profile, done) ->
    console.log req.params
    serviceQuery =
      service: 'github'
      provider_id: "#{profile.id}"
      UserId: 13

    global.db.Service.find
      where: serviceQuery
    .then (dbService) ->
      if not dbService
        newService =
          service: 'github'
          token: accessToken
          secret: null
          provider_id: profile.id
          raw: profile._json
          UserId: 13
        global.db.Service.create newService
      else
        dbService.token = accessToken
        dbService.raw = profile._json
        dbService.save()
    .then ->
      done null, true


  app.get '/auth/github/:sessionToken', (req, res, next) ->
    console.log req.params
    next()
  , passport.authenticate 'github',
    scope: 'repo'
    session: false

  app.get '/auth/github/callback', passport.authenticate('github',
    scope: 'repo'
    session: false
  ), (req, res) ->
    res.redirect 'http://localhost:8080/app'

So that when the user is directed to /auth/github/:sessionToken, I want passport to do its magic and redirect the user to github. This works great. But when they return to /auth/github/callback, I need to still be able to identify which user it is.

Since this is the API service, it is supposed to be stateless, so as I mentioned before, I'm looking for some way to pass that token to the /auth/github/callback. How can I accomplish this?

like image 330
Shamoon Avatar asked Apr 17 '15 22:04

Shamoon


1 Answers

Try a console log of req.user in your /auth/github/callback. I believe the req.user will still be storing their user information. As it appears in this tutorial they pass the user information to be displayed by passing in the req.user.

like image 63
tilted Avatar answered Oct 07 '22 05:10

tilted