【问题标题】:NestJS : Auth guard flowNestJS:身份验证保护流程
【发布时间】:2021-05-31 18:15:07
【问题描述】:

我正在用nestJS 中的护照实施linkedin 登录策略。 我现在有一个按钮“使用linkedin登录”指向auth/linkedin

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

这很好用,将我带到linkedin登录页面并回击我的回调URL,即auth/linkedin/callbackcode令牌查询字符串这是我无法弄清楚要做什么和如何做的地方返回linkedin用户

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

Linkedin 护照策略:

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: 'abcd123',
      clientSecret: 'abcd123',
      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);
      });
    })
  }
}

注意:我将这个package 用于linkedin 护照策略

问题:如何使用@UseGuards 进一步处理回调,并返回LinkedIn 用户?

【问题讨论】:

    标签: javascript node.js passport.js linkedin nestjs


    【解决方案1】:

    您应该稍微调整一下LinkedinStrategy 类。您不能直接使用done 函数。它将被嵌套调用。您应该有一个validate 方法并从中返回一个用户对象。该对象将设置为请求对象,因此在控制器中您将能够使用req.user 访问它。这大概是您的课程的外观:

    import { Strategy } from 'passport-linkedin-oauth2';
    import { PassportStrategy } from '@nestjs/passport';
    import { Injectable, UnauthorizedException } from '@nestjs/common';
    import { AuthService } from './auth.service';
    
    @Injectable()
    export class LinkedinStrategy extends PassportStrategy(Strategy) {
      constructor(private authService: AuthService) {
        super({
            clientID: 'abcd123',
            clientSecret: 'abcd123',
            callbackURL: 'http://localhost:5000/auth/linkedin/callback',
            scope: ['r_emailaddress', 'r_liteprofile'],
          });
      }
    
      async validate(accessToken: string, refreshToken: string, profile: object): Promise<any> {
        const user = await this.authService.validateUser(profile);
    
        return user;
      }
    }
    

    【讨论】:

    • 有道理,但我如何处理回调 URL auth/linkedin/callback
    • validate 方法的逻辑只会在回调的情况下运行。在第一种情况下,nest 将在调用validate 之前重定向到linkedin 页面。因此,您可以在 validate 方法或控制器中处理回调。同样,您从 validate 方法返回的任何内容都可以从 req.user 访问。
    • 我有不同用途的登录页面...重定向到linkedin页面时有没有办法标记一些自定义参数,所以回调后可以保留和使用?
    【解决方案2】:

    查看这篇文章:OAuth2 in NestJS for Social Login (Google, Facebook, Twitter, etc) 和这个 repo:https://github.com/thisismydesign/nestjs-starter

    LinkedinStrategyvalidate 方法中,您需要查找或创建用户(可能存储在您的数据库中),例如:

    export class LinkedinStrategy extends PassportStrategy(Strategy) {
      // constructor...
    
      async validate(accessToken, refreshToken, profile) {
        let user = await this.usersService.findOneByProvider('linkedin', profile.id);
        if (!user) {
          user = await this.usersService.create({
            provider: 'linkedin',
            providerId: id,
            name: profile.name,
            username: profile.email,
          });
        }
    
        return user;
      }
    }
    

    在控制器的回调端点中,您可以发出 JWT 令牌来处理用户在应用内的会话:

    @Get('auth/linkedin/callback')
    @UseGuards(AuthGuard('linkedin'))
    linkedinCallBack(@Req() req) {
      const { accessToken } = this.jwtAuthService.login(req.user);
      res.cookie('jwt', accessToken);
      return res.redirect('/profile');
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 2021-02-12
      • 2021-01-13
      • 1970-01-01
      • 2018-11-02
      • 2014-11-20
      • 2019-03-28
      • 2012-01-25
      • 2017-09-09
      相关资源
      最近更新 更多