@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.
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) => {
}
}
});
const app = express();
app.use(
session({
name: 'id',
secret: 'secret',
rolling: true,
cookie: { ... },
})
);
app.use(accountsSession.middleware())
app.get('/me', (req, res) => {
const user = req.currentUser;
res.json(user);
});
app.post('/login', (req, res) => {
let tokens: Tokens;
accountsSession.set(req, tokens);
});
app.get('/logout', (req, res) => {
accountsSession.destroy(req);
});
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