0.2.81 • Published 1 year ago

cp-auth-service v0.2.81

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Virava

Virava is a lightweight service designed to handle authentication inside a web application.

Dependencies

Getting started with Virava

This guide explains how to set up your project to begin using Virava. It includes information on prerequisites, installing and configuring Virava.

1. Install Virava

To install Virava you will need to have npm previously installed and then type in a terminal:

npm install virava

This will add project dependencies to package.json.

2. Usage

Virava currently supports 2 ways of handling authentication depending on the API used. One uses Keycloak and its default flow. The other - a custom Backend REST API.

All of them share similar approach. It is recommended to use the factory class to instantiate the Virava. Depending on the flow you are using you should cast the service instantiated so you can get additional/correct methods.

  • Keycloak approach:
import { AuthServiceFactory, ServiceType, KeycloakServiceDefault } from 'virava';

const authService = AuthServiceFactory.create(ServiceType.DEFAULT) as KeycloakServiceDefault;

Then you will need to add the configuration for the service (again depending on the flow).

import { KeycloakConfigDefault } from 'virava';

const config: KeycloakConfigDefault = {
  clientId:'',
  baseUrl: '',
  realm: '',
  tokenStorageKey: '', //optional
  refreshTokenStorageKey: '', //optional
  expirationDateStorageKey: '', //optional
  expirationRefreshDateStorageKey: '', //optional
  redirectUri: '' //optional
};

authService.init(config);
  • Custom approach:
import { AuthServiceFactory, ServiceType, KeycloakServiceCustom } from 'virava';

const authService = AuthServiceFactory.create(ServiceType.CUSTOM) as KeycloakServiceCustom;

Then you will need to add the configuration for the service (again depending on the flow).

import { KeycloakConfigCustom } from 'virava';

const config: KeycloakConfigCustom = {
  clientId: '',
  baseUrl: '',
  realm: '',
  tokenStorageKey: '', //optional
  refreshTokenStorageKey: '', //optional
  expirationDateStorageKey: '', //optional
  expirationRefreshDateStorageKey: '', //optional
  gatewayBaseUrl: ''
};

authService.init(config);

3. Start using Virava

To start using Virava simply import your instance of authService and trigger some of it's methods:

  • Keycloak Default pages approach:
/**
 * Initialises the auth service configuration. It returns Promise.
 */
authService.init(configuration);

/**
 * Redirects the user to the keycloak login page. It returns Promise.
 */
authService.login();

/**
 * Redirects the user to the register page. It returns Promise.
 */
authService.register(email, password, confirmPassword);

/**
 * Returns a boolean if the user is authenticated or not.
 */
authService.isAuthenticated();

/**
 * Logouts user and remove tokens from `localStorage`. It returns Promise.
 */
authService.logout();
  • Keycloak Custom pages approach:
/**
 * Initialises the auth service configuration.
 */
authService.init(configuration);

/**
 * This method is designed to work with Direct Access Grant flow of OAuth. It returns Promise.
 */
authService.login(username, password);

/**
 * This method registers the user. It returns Promise.
 */
authService.register(email, password, confirmPassword);

/**
 * This method resets user's password. It returns Promise.
 */
authService.resetPassword(email);

/**
 * This method changes the user's password. It returns Promise.
 */
authService.changePassword(email, currentPassword, newPassword, confirmPassword);

/**
 * This method refreshes the token and sets the new ones in `localStorage`. It returns Promise.
 */
authService.refreshToken();

/**
 * Logouts user and remove tokens from `localStorage`. It returns Promise.
 */
authService.logout();

4. Test your application

Run your local dev server:

  • in React:
npm start
  • in Angular
ng serve

Examples

0.2.81

1 year ago

0.2.63

2 years ago

0.2.62

2 years ago

0.2.61

2 years ago

0.2.60

2 years ago

0.2.69

2 years ago

0.2.68

2 years ago

0.2.67

2 years ago

0.2.66

2 years ago

0.2.65

2 years ago

0.2.64

2 years ago

0.2.59

2 years ago

0.2.58

2 years ago

0.2.57

2 years ago

0.2.56

2 years ago

0.2.80

1 year ago

0.2.74

2 years ago

0.2.73

2 years ago

0.2.72

2 years ago

0.2.71

2 years ago

0.2.70

2 years ago

0.2.79

1 year ago

0.2.78

1 year ago

0.2.77

1 year ago

0.2.75

2 years ago

0.2.27

2 years ago

0.2.26

2 years ago

0.2.25

2 years ago

0.2.24

2 years ago

0.2.23

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.52

2 years ago

0.2.51

2 years ago

0.2.50

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.12

2 years ago

0.2.55

2 years ago

0.2.11

2 years ago

0.2.54

2 years ago

0.2.10

2 years ago

0.2.53

2 years ago

0.2.41

2 years ago

0.2.40

2 years ago

0.2.49

2 years ago

0.2.48

2 years ago

0.2.47

2 years ago

0.2.46

2 years ago

0.2.45

2 years ago

0.2.44

2 years ago

0.2.43

2 years ago

0.2.42

2 years ago

0.2.39

2 years ago

0.2.30

2 years ago

0.2.38

2 years ago

0.2.37

2 years ago

0.2.36

2 years ago

0.2.35

2 years ago

0.2.34

2 years ago

0.2.33

2 years ago

0.2.32

2 years ago

0.2.31

2 years ago

0.2.29

2 years ago

0.2.28

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.8

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.1

3 years ago