authorization-util.ts


Overview

The authorization-util.ts file provides utility functions and a storage abstraction layer to manage user authorization-related data within a web application. Its primary purpose is to interact with the browser's localStorage to persist and retrieve authentication tokens, user information, and language preferences. Additionally, it offers a helper function to extract authorization tokens from URL search parameters and a redirect utility to navigate users to the login page.

This file simplifies handling authorization state in client-side applications by encapsulating common operations like setting, getting, and removing authorization data, and it aids in maintaining a consistent approach to managing authentication-related local storage keys.


Detailed Explanation of Contents

Imports


KeySet Constant

const KeySet = [Authorization, Token, UserInfo];

storage Object

An encapsulated object exposing methods for interacting with localStorage for authorization-related data.

Methods

Method

Parameters

Returns

Description

getAuthorization()

None

`string \

null`

getToken()

None

`string \

null`

getUserInfo()

None

`string \

null`

getUserInfoObject()

None

`object \

null`

setAuthorization(value)

value: string

void

Stores the authorization string under the Authorization key.

setToken(value)

value: string

void

Stores the token string under the Token key.

setUserInfo(value)

`value: string \

Record<string, unknown>`

void

setItems(pairs)

pairs: Record<string, string>

void

Batch sets multiple key-value pairs into localStorage.

removeAuthorization()

None

void

Removes the Authorization key from localStorage.

removeAll()

None

void

Removes all keys in KeySet (Authorization, Token, UserInfo) from localStorage.

setLanguage(lng)

lng: string

void

Sets the current language preference under the 'lng' key.

getLanguage()

None

string

Retrieves the language preference stored under the 'lng' key. Returns null if not set.

Usage Examples

// Store authorization token
storage.setAuthorization('Bearer abc123');

// Retrieve authorization token
const authToken = storage.getAuthorization();

// Set user info as an object
storage.setUserInfo({ id: 1, name: 'John Doe' });

// Get user info as an object
const userInfo = storage.getUserInfoObject();

// Remove all auth-related items (logout scenario)
storage.removeAll();

getAuthorization Function

export const getAuthorization = () => {
  const auth = getSearchValue('auth');
  const authorization = auth
    ? 'Bearer ' + auth
    : storage.getAuthorization() || '';

  return authorization;
};
const authHeader = getAuthorization();
// e.g., "Bearer xyz123" or ""

redirectToLogin Function

export function redirectToLogin() {
  window.location.href = location.origin + `/login`;
}
redirectToLogin(); // Navigates user to https://currentdomain/login

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[getAuthorization()]
    B[storage.getAuthorization()]
    C[getSearchValue('auth')]
    D[storage.setAuthorization(value)]
    E[storage.getToken()]
    F[storage.setToken(value)]
    G[storage.getUserInfo()]
    H[storage.getUserInfoObject()]
    I[storage.setUserInfo(value)]
    J[storage.setItems(pairs)]
    K[storage.removeAuthorization()]
    L[storage.removeAll()]
    M[storage.setLanguage(lng)]
    N[storage.getLanguage()]
    O[redirectToLogin()]

    A --> C
    A --> B
    D --> storage
    E --> storage
    F --> storage
    G --> storage
    H --> storage
    I --> storage
    J --> storage
    K --> storage
    L --> storage
    M --> storage
    N --> storage

    O -->|Redirects| LoginPage[Login Page URL]

Summary

The authorization-util.ts file is a centralized utility module designed to manage authorization data in local storage and provide convenient accessors and mutators for tokens and user information. It also supports language preference storage and offers a helper to retrieve tokens from URL parameters. The included redirection utility facilitates navigating users to the login page when needed. This module plays a critical role in the authentication lifecycle and state management on the client side.


End of documentation for authorization-util.ts