User Class & create-user Script
You are reading the documentation for version 2 of FoalTS. Instructions for upgrading to this version are available here. The old documentation can be found here.
#
The User EntityThe User
entity is the core of the authentication and authorization system. It is a class that represents the user
table in the database and each of its instances represents a row in this table.
The class definition is usually located in the file src/app/entities/user.entity.ts
. Its attributes represent the columns of the table.
In FoalTS you can customize the User
class to suit your needs. The framework makes no assumptions about the attributes required by the user objects. Maybe you'll need a firstName
column, maybe not. Maybe the authentication will be processed with an email and a password or maybe you will use an authentication token. The choice is yours!
However, FoalTS provides abstract classes from which you can extend the User
entity. Such classes, such as UserWithPermissions
, have useful utilities for handling authentication and authorization, so that you do not have to reinvent the wheel.
#
Creating Users ...There are several ways to create users.
#
... Programmatically#
... with a Shell Script (CLI)You can use the create-user
shell script (located in src/scripts
) to create a new user through the command line.
#
Example (email and password)This section describes how to create users with an email and a password.
#
The User EntityGo to src/app/entities/user.entity.ts
and add two new columns: an email and a password.
Note: The
BeforeInsert
andBeforeUpdate
are TypeORM decorators for Entity Listeners that run before the entity is saved in the db. In this example they take care of hashing the password. More info aboutEntity Listeners
in the TypeORM docs
#
The create-user Shell ScriptRunning the create-user
script will result in an error since we do not provide an email and a password as arguments.
Go to src/scripts/create-user.ts
and uncomment the lines mentionning the emails and passwords.
To get it work, you will also need to install the
password
package:npm install --save @foal/password
. TheisCommon
util helps you to detect if a password is too common (ex: 12345) and thus prevents the script from creating a new user with an unsecured password.
You can now create a new user with these commands:
#
Using another ORM/ODMIn this document, we used TypeORM to define the User
class and the create-user
shell script. However, you can still use another ORM/ODM if you want to.