### File

`src/app/Services/token.service.ts`

### Constructor

`constructor()`

### Methods

signOut
`signOut()`
Function to sign out user Returns: `void` void

Public saveToken
`saveToken(token: string)`

Function to save token

Parameters :

- token

Returns: `void`

void

Public getToken
`getToken()`
Function to get token Returns: `string` string

Public saveUser
`saveUser(user: any)`

Function to save user

Parameters :

- user

Returns: `void`

void

Public getUser
`getUser()`
Function to get user Returns: `any` any

    import { Injectable } from '@angular/core';
    const TOKEN_KEY = 'auth-token';
    const USER_KEY = 'auth-user';
    @Injectable({
      providedIn: 'root',
    })
    export class TokenStorageService {
      constructor() {}

      /**
       * Function to sign out user
       * @returns void
       */
      signOut(): void {
        window.sessionStorage.clear();
      }

      /**
       * Function to save token
       * @param  {string} token
       * @returns void
       */
      public saveToken(token: string): void {
        window.sessionStorage.removeItem(TOKEN_KEY);
        window.sessionStorage.setItem(TOKEN_KEY, token);
      }

      /**
       * Function to get token
       * @returns string
       */
      public getToken(): string | null {
        return window.sessionStorage.getItem(TOKEN_KEY);
      }

      /**
       * Function to save user
       * @param  {any} user
       * @returns void
       */
      public saveUser(user: any): void {
        window.sessionStorage.removeItem(USER_KEY);
        window.sessionStorage.setItem(USER_KEY, JSON.stringify(user));
      }

      /**
       * Function to get user
       * @returns any
       */
      public getUser(): any {
        const user = window.sessionStorage.getItem(USER_KEY);
        if (user) {
          return JSON.parse(user);
        }
        return {};
      }
    }

# results matching ""

# No results matching ""