@acceleratxr/passport-discord-token 中文文档教程

发布于 3年前 浏览 18 项目主页 更新于 3年前

passport-discord-token

使用 OAuth 2.0 API 使用 Discord 访问令牌进行身份验证的 Passport 策略。

此模块允许您使用 Passport 框架在 Node.js 应用程序中使用 Discord 进行身份验证。

Installation

npm install passport-discord-token

或者

yarn add passport-discord-token

Usage

Configure Strategy

Discord 身份验证策略使用 Discord 帐户和 OAuth 2.0 令牌对用户进行身份验证。 策略 需要一个 verify 回调来执行对用户帐户的查找,给定一个经过验证的 OAuth 2.0 令牌。

JavaScript

const DiscordTokenStrategy = require('passport-discord-token');

passport.use(new DiscordTokenStrategy({
    clientID: DISCORD_APP_ID,
    clientSecret: DISCORD_APP_SECRET,
    apiVersion: "v8"
  }, function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({discordId: profile.id}, function (error, user) {
      return done(error, user);
    });
  }
));

TypeScript

import { DiscordTokenStrategy } from "passport-discord-token";

passport.use(new DiscordTokenStrategy({
    clientID: DISCORD_APP_ID,
    clientSecret: DISCORD_APP_SECRET,
    apiVersion: "v8"
  }, function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({discordId: profile.id}, function (error, user) {
      return done(error, user);
    });
  }
));

Authenticate Requests

使用 passport.authenticate(),指定 discord-token 作为验证请求的策略。

app.post('/auth/discord/token',
  passport.authenticate('discord-token'),
  function (req, res) {
    // do something with req.user
    res.send(req.user? 200 : 401);
  }
);

Client Requests

客户端可以使用查询参数向使用 passport-discord-token 策略的路由发送请求。 客户必须 传输 Discord 登录后收到的 code 参数。 客户端也可以选择传输一个state 范围。

GET /auth/discord/token?code=<TOKEN_HERE>&state=<STATE_HERE>

passport-discord-token

Passport strategy for authenticating with Discord access tokens using the OAuth 2.0 API.

This module lets you authenticate using Discord in your Node.js applications using the Passport framework.

Installation

npm install passport-discord-token

or

yarn add passport-discord-token

Usage

Configure Strategy

The Discord authentication strategy authenticates users using a Discord account and OAuth 2.0 tokens. The strategy requires a verify callback to perform a look up of the user account given a verified OAuth 2.0 token.

JavaScript

const DiscordTokenStrategy = require('passport-discord-token');

passport.use(new DiscordTokenStrategy({
    clientID: DISCORD_APP_ID,
    clientSecret: DISCORD_APP_SECRET,
    apiVersion: "v8"
  }, function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({discordId: profile.id}, function (error, user) {
      return done(error, user);
    });
  }
));

TypeScript

import { DiscordTokenStrategy } from "passport-discord-token";

passport.use(new DiscordTokenStrategy({
    clientID: DISCORD_APP_ID,
    clientSecret: DISCORD_APP_SECRET,
    apiVersion: "v8"
  }, function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({discordId: profile.id}, function (error, user) {
      return done(error, user);
    });
  }
));

Authenticate Requests

Use passport.authenticate(), specifying discord-token as the strategy to authenticate requests.

app.post('/auth/discord/token',
  passport.authenticate('discord-token'),
  function (req, res) {
    // do something with req.user
    res.send(req.user? 200 : 401);
  }
);

Client Requests

Clients can send requests to routes that use the passport-discord-token strategy using query params. Clients must transmit the code parameter that is received after Discord login. Clients may also optionally transmit a state parameter.

GET /auth/discord/token?code=<TOKEN_HERE>&state=<STATE_HERE>
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文