Passport.js Github Strategy on Node.js: Github redirect_uri mismatch

484
May 01, 2017, at 07:34 AM

I want to implement a login with Passport.js and Github Strategy. The code works fine on localhost, but when I host it on Digital Ocean, I get this:

AuthorizationError: The redirect_uri MUST match the registered callback URL for this application.

I would like to invite tou to take a look over the code and to help me to fink out what is the problem. I mention that I tested a lot of combination for Github Homepage/Github Callback/Local Callback, but it doesn't work. My setup look like this:

// Github OAuth app
Homepage URL: http://example.com:3000
Callback URL: http://example.com:3000/auth/github/callback
// part of config.js
var config = {
        githubID: '',
        githubSecret: '',
        githubURL: 'http://example.com:3000/auth/github/callback',
    }
// part of app.js
passport.use(new GithubStrategy({
        clientID: config.githubID,
        clientSecret: config.githubSecret,
        callbackURL: config.githubURL
    },
    function(accessToken, refreshToken, profile, done){
        process.nextTick(function(){
            return done(null, profile);
        });
    }
));
app.get('/auth/github', passport.authenticate('github', {scope: ['user:email']}), function(req, res){});
app.get('/auth/github/callback', passport.authenticate('github', {failureRedirect: '/'}), function(req, res){
    res.redirect('/dashboard');
});
app.get('/logout', function(req, res){
    req.logout();
    res.redirect('/');
});
Answer 1

First, you must supply the:

  • GITHUB_CLIENT_ID
  • GITHUB_CLIENT_SECRET

Next, Passport session setup requires To support persistent login sessions, Passport needs to be able to serialize users into and deserialize users out of the session.

Typically, this will be as simple as storing the user ID when serializing, and finding the user by ID when deserializing. the complete GitHub profile is serialized and deserialized.

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

You need to register the oAuth2 authorization callback url on Github:

You can follow the full example that is supplied here

READ ALSO
Integrate Disqus in Hexo

Integrate Disqus in Hexo

I have a very basic but very annoyed question for me that I have a Hexo blog and want to integrate Disqus Comments capabilityI'm using the Beantech Hexo theme and it has a disqus_username: kjanshair property in _config

338
Express.js throw exception and return pre-defined status code

Express.js throw exception and return pre-defined status code

In spring i use define my own class extending Exception annotate it with @ResponseStatus to throw it in specific situation to return to user specific status code and error messageI'd like to do something similar in Express

315
Mongoose: Exclude objects from results based on another model

Mongoose: Exclude objects from results based on another model

Let's say I have a User and Group model, groups have users, like

421
What to use in NodeJS to create process which works long time and is accessible during its lifetime

What to use in NodeJS to create process which works long time and is accessible during its lifetime

I want to create some process which will work for a long time and it will be possible to obtain data from this process by get request

280