docs/site/Loopback-component-authentication.md
This document describes the details of the LoopBack 4 Authentication component
from the @loopback/authentication package.
It begins with the architecture of @loopback/authentication from high level.
Then comes with the sub-sections for each artifact provided by the component.
Each section shows:
Here is a high level overview of the authentication component.
As illustrated in the diagram, this component includes:
Then let's take a look of the detailed overview of the authentication component.
Basically, to secure your API endpoints, you need to:
@authenticate(strategyName, options?) decorator (app developer)The Authentication Component takes care of the rest.
npm install --save @loopback/authentication
To utilize authentication in an application application.ts, you must load
the authentication component named AuthenticationComponent.
import {AuthenticationComponent} from '@loopback/authentication';
//...
export class MyApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options?: ApplicationConfig) {
super(options);
//...
// ------ ADD SNIPPET AT THE BOTTOM ---------
// Mount authentication system
this.component(AuthenticationComponent);
// ------------- END OF SNIPPET -------------
//...
}
}
The AuthenticationComponent is defined as follows:
// ------ CODE THAT EXPLAINS THE MECHANISM ---------
export class AuthenticationComponent implements Component {
providers?: ProviderMap;
constructor() {
this.providers = {
[AuthenticationBindings.AUTH_ACTION.key]: AuthenticateActionProvider,
[AuthenticationBindings.STRATEGY.key]: AuthenticationStrategyProvider,
[AuthenticationBindings.METADATA.key]: AuthMetadataProvider,
};
}
}
As you can see, there are a few providers which make up the bulk of the authentication component.
Essentially
AuthenticationBindings.METADATA.key is bound to
AuthMetadataProvider which returns authentication decorator metadata of type
AuthenticationMetadataAuthenticationBindings.AUTH_ACTION.key is bound to
AuthenticateActionProvider which returns an authenticating function of type
AuthenticateFnAuthenticationBindings.STRATEGY.key is bound to
AuthenticationStrategyProvider which resolves and returns an authentication
strategy of type AuthenticationStrategyThe purpose of these providers and the values they return will be explained in the sections below.
Next topic: Authentication Decorator