Failed to fetch user profile (status: 401 data: {"message":"Requires authentication"})
Closed this issue · 2 comments
Hi,
I just tried to use passport-github
, it seems to be straightforward to implement. But I have an error that I don't understand.
I created an application on Github. I set the correct client id
and client secret
. And when I click signup with Github I have the following error:
GET /auth/github 302 2ms - 432
failed to fetch user profile (status: 401 data: {"message":"Requires authentication"})
at Strategy.userProfile (/Users/yyyyyyy/Workspace/xxxxxxxx/node_modules/passport-github/lib/passport-github/strategy.js:90:28)
at passBackControl (/Users/yyyyyyy/Workspace/xxxxxxxx/node_modules/passport-github/node_modules/passport-oauth/node_modules/oauth/lib/oauth2.js:105:9)
at IncomingMessage.exports.OAuth2._executeRequest.request.on.callbackCalled (/Users/yyyyyyy/Workspace/xxxxxxxx/node_modules/passport-github/node_modules/passport-oauth/node_modules/oauth/lib/oauth2.js:124:7)
at IncomingMessage.EventEmitter.emit (events.js:126:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at CleartextStream.socketOnData [as ondata] (http.js:1367:20)
at CleartextStream.CryptoStream._push (tls.js:526:27)
at SecurePair.cycle (tls.js:880:20)
at EncryptedStream.CryptoStream.write (tls.js:267:13)
GET /auth/github/callback?code=<a code> 500 872ms
However, the user seems to be correctly created: the application user counter (on Github) has been incremented to 1.
I think the error is on my side, do you have some idea to help me?
Thanks a lot for your work on passport
Could you describe you how resolved the issue?
It was just a misconfiguration on my side. I had to (re)create my github app and I totally forgot to reload my environment variables file... 😅
I can show some code about my github authentication. (But I can't add a link to a github repository because the project was just a POC and the code was really dirty.... Sorry but I can't share that).
app.coffee
# some requirements
passport = require('passport')
app = express()
app.configure(->
# some configurations
app.use(express.session())
app.use(passport.initialize())
app.use(passport.session())
app.set('orm', require('./models'))
require('./authentication.coffee')(app)
require('./routes')(app)
)
app.configure('development', -> app.use(express.errorHandler()))
http.createServer(app).listen(app.get('port'), -> console.log("Express server listening on port #{app.get('port')}"))
authentications.coffee
module.exports = (app) ->
passport = require('passport')
GitHubStrategy = require('passport-github').Strategy
User = app.get('orm').User
passport.serializeUser((user, done) -> done(null, user.id))
passport.deserializeUser((id, done) -> User.find(id).success((user) -> done(null, user)))
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: 'http://127.0.0.1:3000/auth/github/callback',
}, (accessToken, refreshToken, profile, done) ->
User.findOrCreate({ id: profile.id }, { login: profile.username, fullname: profile.displayName }).success((user, created) -> return done(null, user))
))
routes.coffee
module.exports = (app) ->
passport = require('passport')
app.get('/auth/github', passport.authenticate('github'), (request, result)->)
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/' }), (request, result) ->
result.redirect('/websites')
)
app.get('/signout', (request, result) ->
request.logout()
result.redirect('/')
)
I hope this answer can help you. Otherwise, do you have some code that you can show us?