@accounts/express-session 中文文档教程

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

@accounts/express-session

将访问和刷新令牌存储为会话的一部分 (express-session),这样身份验证流程就可以基于它。

npmMIT License

Install

yarn add @accounts/express-session

Usage

import * as express from 'express';
import * as session from 'express-session';
import { Tokens } from '@accounts/types';
import AccountsSession from '@accounts/express-session';
import { accountsServer } from './setup';

const accountsSession = new AccountsSession(accountsServer, {
  user: {
    name: 'currentUser',
    resolve: (tokens: Tokens) => {
      // function that returns a user object
    }
  }
});

const app = express();

app.use(
  session({
    name: 'id',
    secret: 'secret',
    rolling: true,
    cookie: { ... }, // cookie options
  })
);

app.use(accountsSession.middleware())

app.get('/me', (req, res) => {
  const user = req.currentUser; // middleware assings a user object to `req`

  res.json(user);
});

app.post('/login', (req, res) => {
  let tokens: Tokens; // Tokens  AccountsServer

  // ... a logic to log user in

  accountsSession.set(req, tokens); // sets tokens on request so middleware can access that
});

app.get('/logout', (req, res) => {
  accountsSession.destroy(req); // destroys the session and logs user out
});

Options

user.name

指定持有用户对象的属性的名称。 例如,通过使用 currentUser,可以在 req.currentUser 上访问用户对象。

默认情况下,它使用:user

user.resolve

接收访问和刷新令牌以解析用户对象的函数。

(tokens: Tokens) => User | Promise<User>

默认情况下,它使用 AccountsServer 的 API 来解析用户。

name

指定持有令牌的属性的名称。 例如,通过使用 tokens,可以在 req.tokens 上访问令牌。

默认情况下它使用:accounts-js-tokens

License

MIT

@accounts/express-session

Stores the access and refresh tokens as part of the session (express-session), this way auth flow could be based on it.

npmMIT License

Install

yarn add @accounts/express-session

Usage

import * as express from 'express';
import * as session from 'express-session';
import { Tokens } from '@accounts/types';
import AccountsSession from '@accounts/express-session';
import { accountsServer } from './setup';

const accountsSession = new AccountsSession(accountsServer, {
  user: {
    name: 'currentUser',
    resolve: (tokens: Tokens) => {
      // function that returns a user object
    }
  }
});

const app = express();

app.use(
  session({
    name: 'id',
    secret: 'secret',
    rolling: true,
    cookie: { ... }, // cookie options
  })
);

app.use(accountsSession.middleware())

app.get('/me', (req, res) => {
  const user = req.currentUser; // middleware assings a user object to `req`

  res.json(user);
});

app.post('/login', (req, res) => {
  let tokens: Tokens; // Tokens  AccountsServer

  // ... a logic to log user in

  accountsSession.set(req, tokens); // sets tokens on request so middleware can access that
});

app.get('/logout', (req, res) => {
  accountsSession.destroy(req); // destroys the session and logs user out
});

Options

user.name

Specifies the name of a property that holds a user object. For example, By using currentUser, a user object is accesible on req.currentUser.

By default it uses: user

user.resolve

Function that receives an access and a refresh tokens to resolve a user object.

(tokens: Tokens) => User | Promise<User>

By default it uses AccountsServer's API to resolve a user.

name

Specifies the name of a property that holds the Tokens. For example, By using tokens, tokens are accesible on req.tokens.

By default it uses: accounts-js-tokens

License

MIT

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