javascript - 使用 NestJS 授予 Auth0 授权码

标签 javascript node.js typescript auth0 nestjs

我正在寻找如何实现 Authorization Code Grant 的示例使用 NestJS 的 Auth0 流程。

最佳答案

为了在处理 exchange of the Authorization Code for an Access Token 的后端创建一个 POST 端点 /authenticate ,我们需要首先定义dto端点期望的。

我们期望从客户端获得一个具有 authorization_codeorigin 字段的对象。

授权请求.dto.ts

export class AuthorizationRequestDto {
  readonly authorization_code: string;
  readonly origin: string;
}

现在我们可以创建一个 controller它将处理发送到 /authenticatePOST 请求:

import { Body, Controller, HttpService, Post } from '@nestjs/common';
import { AxiosResponse } from '@nestjs/common/http/interfaces/axios.interfaces';
import { Observable } from 'rxjs/internal/Observable';
import { map } from 'rxjs/operators';

import { AuthorizationRequestDto } from './authorization-request.dto';

@Controller('authenticate')
export class AuthController {

  constructor(private readonly httpService: HttpService) {}

  @Post()
  authenticate(@Body() authorizationRequestDto: AuthorizationRequestDto): Observable<AxiosResponse<any>> {
    return this.httpService.post('https://YOUR_AUTH0_DOMAIN/oauth/token', {
      grant_type: 'authorization_code',
      client_id: 'YOUR_CLIENT_ID',
      client_secret: 'YOUR_CLIENT_SECRET',
      code: authorizationRequestDto.authorization_code,
      redirect_uri: authorizationRequestDto.origin,
    }).pipe(
      map(response => response.data),
    );
  }
}

当然,我们需要替换上面的代码 YOUR_AUTH0_DOMAINYOUR_CLIENT_IDYOUR_CLIENT_SECRET

关于javascript - 使用 NestJS 授予 Auth0 授权码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354631/

相关文章:

背景图像上的 Angular (负载)

javascript - 使用 JS 如何输出标题属性上的数据?

javascript - SugarCRM - 可能的 Vardefs/数组属性列表在哪里?

javascript - RaphaelJS 缩放在 Chrome 中损坏

javascript - 在 Nodejs 中发送多封邮件

linux - 在nodejs中使用子进程创建密码保护的zip文件

javascript - 如何使用变量名创建字段?

reactjs - 在 TypeScript 模块解析期间未找到 Material-UI 组件

php - JavaScript 冲突?

typescript - Angular 2服务的异步初始化