Before we jump into the authentication functions, we need to create our User model in Sequelize. For this, we need to create a file at /backend/src/models/User.ts. Our model will have the following fields:
- id
- username
- password
- privilege
- active
Let's see the code:
// Dependencies
import { encrypt } from '@contentpi/lib'
// Interfaces
import { IUser, IDataTypes } from '../types'
export default (sequelize: any, DataTypes: IDataTypes): IUser => {
const User = sequelize.define(
'User',
{
id: {
primaryKey: true,
allowNull: false,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4()
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isAlphanumeric: {
args: true,
msg: 'The user just accepts alphanumeric characters'
},
len: {
args: [4, 20],
...