@goodrequest/refresh-token-repository-sequelize
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

Build and run tests Publish package to GitHub Packages

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 to false, 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:

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
}

Readme

Keywords

none

Package Sidebar

Install

npm i @goodrequest/refresh-token-repository-sequelize

Weekly Downloads

1

Version

0.1.4

License

ISC

Unpacked Size

67 kB

Total Files

39

Last publish

Collaborators

  • logingoodrequest
  • lubomirigonda