File

src/app/shared/services/auth-storage.service.ts

Index

Methods

Constructor

constructor()

Methods

getPermissions
getPermissions()
Returns : Permissions
getToken
getToken()
Returns : string
getUsername
getUsername()
Returns : any
isLoggedIn
isLoggedIn()
Returns : boolean
remove
remove()
Returns : void
set
set(username: string, token: string, permissions: object)
Parameters :
Name Type Optional Default value
username string No
token string No
permissions object No {}
Returns : void
import { Injectable } from '@angular/core';

import { Permissions } from '../models/permissions';
import { ServicesModule } from './services.module';

@Injectable({
  providedIn: ServicesModule
})
export class AuthStorageService {
  constructor() {}

  set(username: string, token: string, permissions: object = {}) {
    localStorage.setItem('dashboard_username', username);
    localStorage.setItem('access_token', token);
    localStorage.setItem('dashboard_permissions', JSON.stringify(new Permissions(permissions)));
  }

  remove() {
    localStorage.removeItem('access_token');
    localStorage.removeItem('dashboard_username');
  }

  getToken(): string {
    return localStorage.getItem('access_token');
  }

  isLoggedIn() {
    return localStorage.getItem('dashboard_username') !== null;
  }

  getUsername() {
    return localStorage.getItem('dashboard_username');
  }

  getPermissions(): Permissions {
    return JSON.parse(
      localStorage.getItem('dashboard_permissions') || JSON.stringify(new Permissions({}))
    );
  }
}

result-matching ""

    No results matching ""