@goodrequest/refresh-token-repository-sequelize
v0.1.4
Published
[![Build and run tests](https://github.com/GoodRequest/refresh-token-repository-sequelize/actions/workflows/build.yaml/badge.svg)](https://github.com/GoodRequest/refresh-token-repository-sequelize/actions/workflows/build.yaml) [![Publish package to GitHub
Downloads
9
Readme
Refresh Token Repository using Sequelize
This is implementation of refresh token repository interface specified in passport-jwt-wrapper library. It uses SQL database as storage. Sequelize is used as ORM driver.
Installation
npm i --save @goodrequest/refresh-token-repository-sequelize
Usage
import { RefreshTokenRepository } from '@goodrequest/refresh-token-repository-sequelize'
import { models } from './db/models'
initAuth(passport, {
userRepository: new UserRepository(models.User),
refreshTokenRepository: new RefreshTokenRepository(models.UserToken)
})
Notes
- Transactions are not used, since passport-jwt-wrapper is not dependent on sequelize. But they are not needed, since most operations are done in single database call.
paranoid
is set tofalse
, since old tokens are not needed. Some tokens might still be left in the DB, so some repeating cleaning (using cron job) would be nice.
Models Templates
Library also exports basic Sequelize models:
BaseUserTokenModelClass
: BaseUserTokenModelClass -id
is not defined in the attributes, needs to expandedUUIDUserTokenModelClass
: UUIDUserTokenModelClass - Usesuuid
asid
BigIntUserTokenModelClass
: BigIntUserTokenModelClass- UsesBIGINT
with autoincrement asid
Model definition is divided into:
[Xyz]UserTokenModelClass
[Xyz]UserTokenModelAttributes
[Xyz]UserTokenModelOptions
These parts are used in the sequelize init
method:
UserTokenModel.init(UserTokenModelAttributes, UserTokenModelOptions)
Model needs to be defined and can be expanded using these parts. Examples are in the tests folder:
import { DataTypes, Sequelize } from 'sequelize'
import { UserModel } from './user'
import {
UUIDUserTokenModelAttributes,
UUIDUserTokenModelClass,
UUIDUserTokenModelOptions
} from '../../../src/models/uuidUserToken'
export class UUIDUserTokenModel extends UUIDUserTokenModelClass {
declare user: UserModel
}
export default (sequelize: Sequelize, modelName: string) => {
UUIDUserTokenModel.init({
...UUIDUserTokenModelAttributes,
// foreign keys
userID: {
type: DataTypes.UUID,
allowNull: true
},
}, {
...UUIDUserTokenModelOptions,
sequelize,
modelName,
tableName: 'uuid_user_tokens'
})
return UUIDUserTokenModel
}