jasmeetsohal
jasmeetsohal

Reputation: 141

Unknown authentication strategy "github"

Online authentication of GitHub is not working. I've registered the new app in GitHub and still the application won't redirect to OAuth.

The following code I've written and getting Error:Unknown authentication strategy "github"

const passport      = require('passport');
const bcrypt        = require('bcrypt');

module.exports = function (app, db) {

    app.route('/')
      .get((req, res) => { 
        res.render(process.cwd()+'/views/pug/index', {title: 'Hello', message: 'Please login',showLogin: true, showRegistration: true});
      });


    app.route('/login')
      .post(passport.authenticate('local',{ failureRedirect: '/' }),(req,res)=>{
         res.redirect('/profile');
      });

    function ensureAuthenticated(req, res, next) {
      if (req.isAuthenticated()) {
        return next();
      }
      res.redirect('/');
     }

    app.route('/profile')
      .get(ensureAuthenticated, (req,res) => {
         res.render(process.cwd() + '/views/pug/profile',{username:req.user.username});
      });

    app.route('/logout')
      .get((req, res) => {
        req.logout();
        res.redirect('/');
      });


    app.route('/register')
     .post((req, res, next) => {
        var hash = bcrypt.hashSync(req.body.password, 12);
        db.collection('users').findOne({ username: req.body.username }, function (err, user) {
          if(err) {
              next(err);
          } else if (user) {
              res.redirect('/');
          } else {
              db.collection('users').insertOne(
                {username: req.body.username,
                 password: hash},
                (err, doc) => {
                    if(err) {
                        res.redirect('/');
                    } else {
                        next(null, user);
                    }
                }
              )
          }
      })},
    passport.authenticate('local', { failureRedirect: '/' }),
    (req, res, next) => {
        res.redirect('/profile');
    }
    );

  /*GitHub OAuth*/

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

   app.route('/auth/github/callback')
    .get(passport.authenticate('github', { failureRedirect: '/' }), (req,res) => { 
    res.redirect('/profile'); 
  });

  /*End of GitHub OAuth*/

  app.use((req, res, next) => {
    res.status(404)
      .type('text')
      .send('Not Found');
  });
}

It seems I have missing something or anything else for OAuth. The strategy wasn't defined in my side I just accessing default strategy for GitHub.

Upvotes: 1

Views: 1790

Answers (1)

Konrad
Konrad

Reputation: 24691

You have to configure passport github strategy in your script. https://github.com/jaredhanson/passport-github var GitHubStrategy = require('passport-github').Strategy;

passport.use(new GitHubStrategy({
    clientID: GITHUB_CLIENT_ID,
    clientSecret: GITHUB_CLIENT_SECRET,
    callbackURL: "http://127.0.0.1:3000/auth/github/callback"
  },
  function(accessToken, refreshToken, profile, cb) {
    User.findOrCreate({ githubId: profile.id }, function (err, user) {
      return cb(err, user);
    });
  }
));

Upvotes: 3

Related Questions