Created
November 9, 2020 21:51
-
-
Save blogcacanid/cd857c543c952067ee9cee45a0320276 to your computer and use it in GitHub Desktop.
token-storage.service.ts Authentication JWT Angular 9 Lumen 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| const TOKEN_KEY = 'auth-token'; | |
| const USER_KEY = 'auth-user'; | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class TokenStorageService { | |
| constructor() { } | |
| signOut() { | |
| window.sessionStorage.clear(); | |
| } | |
| public saveToken(token: string) { | |
| window.sessionStorage.removeItem(TOKEN_KEY); | |
| window.sessionStorage.setItem(TOKEN_KEY, token); | |
| } | |
| public getToken(): string { | |
| return sessionStorage.getItem(TOKEN_KEY); | |
| } | |
| public saveUser(user) { | |
| window.sessionStorage.removeItem(USER_KEY); | |
| window.sessionStorage.setItem(USER_KEY, JSON.stringify(user)); | |
| } | |
| public getUser() { | |
| return JSON.parse(sessionStorage.getItem(USER_KEY)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment