Skyframe Documentation

Authentication

Authentication

Authentication is a core part of most applications. Knowing the identity of a user allows you to provide a personalized experience to your clients. There are many different approaches and strategies to handle authentication. The approach taken for any project depends on its particular application requirements.

In this chapter, we'll implement a complete end-to-end authentication solution for a RESTful API server using Skyframe powerful and flexible modules. You can use the concepts described here to customize your authentication scheme.

Installation

yarn add @skyframe/auth
# or
npm install @skyframe/auth

Implementing authentication

Creating the User Entity

We'll start by creating a new entity that will store all the user information.

import { UserEntity, SkfUser, Username, Password } from '@skyframe/core';
@UserEntity()
export class User extends SkfUser {
@Username()
email: string;
@Password()
password: string;
}

There is a lot of new things in that example, so lets go one by one:

  • @UserEntity(): We annotate User with the UserEntity decorator to tell Skyframe that we want to authenticate using this class.
  • extends SkfUser: Skyframe provides a lot of core functionalities for authentication and the User class is inheriting them from SkfUser.
  • @Username(): Makes the field unique and indicates that the User entity will be identified using the email field.
  • @Password(): This decorator indicates Skyframe to always encrypt the field and to never retrieve it.

Server User Entity

The last step is to create the Server User Entity. This entity will handle the authentication requests and make sure to encrypt all the sensitive information!

import { Domain } from '@your-project/shared';
import { ServerUserEntity } from '@skyframe/auth';
export class User extends ServerUserEntity(Domain.User) {}

By extending ServerUserEntity the class is now able to create and authenticate User Accounts.

Authenticating users

We are all set to start using authentication in our application!

The next steps are to create an account and login to it.

The Authentication Provider

Skyframe also has an out of the box solution for creating and logging into user accounts.

All you have to do is import the Angular service SkfAuthenticationProvider into your NgModule

import { SkfAuthenticationProvider } from '@skyframe/angular';
@NgModule({
providers: [SkfAuthenticationProvider]
})
export class AppModule {}

Creating a new User Account

In order to create a new account all you have to do is run the following command

this.skfAuthenticationProvider.signup(newUser);

Where newUser is a User Instance containing the credentials for the new account.

This method will send the information to the backend, there the ServerUserEntity will encrypt the password and save the entity in the database.

Authenticating as a User

The last step is to actually login to the application using the new account.

this.skfAuthenticationProvider.login(userAccount);

The Authentication Provider receives the User Instance userAccount and sends it to the backend, where ServerUserEntity will check if the given credentials are valid and do match an account.

If the credentials are indeed valid, the ServerUserEntity will return a JWT which will be stored in the cache in order to keep the session active.