Building an Online Game With LoopBack 4 - User Authentication and Role-Based Access Control (Part 4)
Originally published on strongloop.com
Part 4: User Authentication and Role-Based Access Control
In This Episode
We already have some APIs that allow users to customize their characters. However, a user should not get access to characters that belong to other users. With that in mind, we will add user authentication and role-based access control to this project.
You can check here for this episode's code.
Introduction
In this series, I’m going to help you learn LoopBack 4 and how to use it to easily build your own API and web project. We’ll create a new project I’ve been working on: an online web text-based adventure game. In this game, you can create your own account to build characters, fight monsters and find treasures. You will be able to control your character to take a variety of actions: attacking enemies, casting spells, and getting loot. This game also allows multiple players to log in and play with their friends.
Previously on Building an Online Game With LoopBack 4
In the last episode, we created customized APIs to manage weapon, armor, and skill for character.
Here are the previous episodes:
- Part 1: Building a Simple LoopBack Project With MongoDB
- Part 2: Generating Universally Unique ID and Managing Models Relationships
- Part 3: Customizing APIs in Controller
Basic Structure
LoopBack 4 provides us a built-in authentication package. This package includes an authentication system as the skeleton to verify the identity of a request. It invokes an authentication strategy provided by the developer to process the authentication information in the request and to then return the corresponding user profile.
In this episode, I will combine LoopBack authentication package with my self-defined authorization. This diagram shows the basic structure:

LoopBack Authentication Package
The one in the middle is the @loopback/authentication package. It has three main components:
-
Providers:
- AuthMetadataProvider: this reads the decorator metadata from the controller methods wherever the
@authenticatedecorator is used. - AuthenticateActionProvider: this provides the authentication action which uses AuthenticationStrategyProvider to obtain a registered authentication strategy (if one exists), calls the authenticate(request) method of the given authentication strategy, and places the user profile on the request context.
- AuthenticationStrategyProvider: this is the extension point for you to add your own authentication strategies. I will show you how to do that later. It also has the job of finding a strategy of a given name (if existed), and returning it to AuthenticateActionProvider.
- AuthMetadataProvider: this reads the decorator metadata from the controller methods wherever the
-
Services: all services in this package are interfaces. You can create your own services as well.
- TokenService: an interface for generating and verifying an authentication token.
- UserService: an interface for performing the login action in an authentication strategy, and for converting a User object into a UserProfile object. To keep this project as simple as possible, I am not going to use this interface. I will integrate this to the TokenService.
-
Decorators:
@authenticate. Annotate the APIs that need authentication with this decorator.
Self-defined Authorization
The one in the bottom left is our self-defined authorization. It has three components:
-
Providers:
- UserPermissionsProvider: this will check user's permission. We will create different user permissions for different users. This provider will be invoked in AuthorizationInterceptor.
-
Strategies: this is where we add our own authentication strategies.
- JWTStrategy: we are going to create a custom authentication strategy based on JSON Web Token.
-
Services:
- JWTService: a service associate with JWTStrategy to generate and verify JWT.
-
Interceptors:
- AuthorizationInterceptor: a middle layer comes after authentication that use UserPermissionsProvider to verify user's permission. You can find more information about LoopBack 4 Interceptor at here.
Here is a diagram to show you what will happen after an API call.
