Sunday 7 March 2021

NestJS : Auth guard flow

I am implementing linkedin login strategy with passport in nestJS. What I have right now is that I have a button "Login with linkedin" points to auth/linkedin.

@Get('auth/linkedin')
@UseGuards(AuthGuard('linkedin'))
async linkedinAuth(@Req() req) {
    
}

Which works great and takes me to the linkedin login page and hits back my callback URL which is auth/linkedin/callback with code query string of token and this is where I am unable to figure out what todo and how to return linkedin user

@Get('auth/linkedin/callback')
@UseGuards(AuthGuard('linkedin'))
linkedinCallBack(@Req() req) {
  console.log(req)
  return 'handle callback!';
}

Linkedin passport strategy :

import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
const LinkedInAuthStrategy = require('passport-linkedin-oauth2').Strategy;

@Injectable()
export class LinkedinStrategy extends PassportStrategy(LinkedInAuthStrategy) {
  constructor(
  ) {
    super({
      clientID: '775bhmc0y7hq4x',
      clientSecret: 'CF1JMezx7gX0iJPS',
      callbackURL: 'http://localhost:5000/auth/linkedin/callback',
      scope: ['r_emailaddress', 'r_liteprofile'],
    }, function(accessToken, refreshToken, profile, done) {
      process.nextTick(function () {
        console.log(profile);
        return done(null, profile);
      });
    })
  }
}

Note: I am using this package for linkedin passport strategy

Question : How can I handle callback further with @UseGuards, and return LinkedIn user?



from NestJS : Auth guard flow

No comments:

Post a Comment