lbx-jwt
Version:
Provides JWT authentication for loopback applications. Includes storing roles inside tokens and handling refreshing. Built-in reuse detection.
109 lines (96 loc) • 2.54 kB
text/typescript
import { inject } from '@loopback/core';
import { Entity, hasMany, hasOne, model, property } from '@loopback/repository';
import { BiometricCredentials } from './biometric-credentials.model';
import { Credentials } from './credentials.model';
import { LbxJwtBindings } from '../keys';
/**
* The base user model with data that all user types share.
*/
()
export class BaseUser<RoleType extends string> extends Entity {
/**
* The id of the user.
*/
({
type: 'string',
required: true,
defaultFn: 'uuidv4',
id: true
})
id: string;
/**
* The email of the user.
* Needs to be unique and in a valid format.
*/
({
type: 'string',
required: true,
index: { unique: true },
jsonSchema: {
format: 'email'
}
})
email: string;
/**
* The roles the user has.
* Is used by authorization.
*/
({
type: 'array',
itemType: 'string',
required: true
// json schema restricting to certain roles is set in constructor.
})
roles: RoleType[];
/**
* Whether or not this user account has two factor authentication enabled.
*/
({
type: 'boolean',
required: false
})
twoFactorEnabled?: boolean;
/**
* Whether or not this user needs to change his password.
*/
({
type: 'boolean',
required: false
})
requiresPasswordChange?: boolean;
/**
* The credentials of the user.
* Contains the hashed password.
*/
(() => Credentials)
credentials: Credentials;
/**
* The credentials of the user.
* Contains the hashed password.
*/
(() => BiometricCredentials)
biometricCredentials?: BiometricCredentials[];
/**
* Helper for defining the roles open api.
*/
(LbxJwtBindings.ROLES)
private readonly roleValues: RoleType[];
constructor(data?: Partial<BaseUser<RoleType>>) {
super(data);
BaseUser.definition.properties['roles'].jsonSchema = {
items: {
enum: this.roleValues
}
};
}
}
/**
* Properties of the entity relations.
*/
export interface BaseUserRelations {
// describe navigational properties here
}
/**
* The entity with its relation properties.
*/
export type BaseUserWithRelations<RoleType extends string> = BaseUser<RoleType> & BaseUserRelations;