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.
npm i --save @goodrequest/refresh-token-repository-sequelize
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)
})
- 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.
Library also exports basic Sequelize models:
-
BaseUserTokenModelClass
: BaseUserTokenModelClass -id
is not defined in the attributes, needs to expanded -
UUIDUserTokenModelClass
: 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
}