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.
There are several ways to create users.
You can use the
create-user shell script (located in
src/scripts) to create a new user through the command line.
This section describes how to create users with an email and a password.
src/app/entities/user.entity.ts and add two new columns: an email and a password.
BeforeUpdateare 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 about
Entity Listenersin the TypeORM docs
create-user script will result in an error since we do not provide an email and a password as arguments.
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
npm install --save @foal/password. The
isCommonutil 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:
In 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.