how to do the github social login with nestjs?

Viewed 548

Is there any clear documentation to do the GitHub auth and the verifying in Nestjs?. I could not find a way to implement the Github social login with nestjs.

1 Answers

You can use the Github strategy using Passport.js

github.strategy.js

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, done) {
    User.findOrCreate({ githubId: profile.id }, function (err, user) {
      return done(err, user);
    });
  }
));

auth.controller.ts

app.get('/auth/github',
  passport.authenticate('github', { scope: [ 'user:email' ] }));

app.get('/auth/github/callback', 
  passport.authenticate('github', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

It's an Express example, easily convertible to NestJS

Related