2021-11-30 22:16:38 +08:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
|
|
const Authorization = 'Authorization';
|
2021-12-03 09:15:53 +08:00
|
|
|
const refreshToken = 'refresh-token';
|
2021-11-30 22:16:38 +08:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class LocalStorageService {
|
2021-12-23 15:59:49 +08:00
|
|
|
constructor() {}
|
2021-11-30 22:16:38 +08:00
|
|
|
|
|
|
|
|
public putData(key: string, value: string) {
|
|
|
|
|
localStorage.setItem(key, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getData(key: string): string | null {
|
|
|
|
|
const data = localStorage.getItem(key);
|
|
|
|
|
return data === null ? null : data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getAuthorizationToken(): string | null {
|
|
|
|
|
return this.getData(Authorization);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 09:15:53 +08:00
|
|
|
public getRefreshToken(): string | null {
|
|
|
|
|
return this.getData(refreshToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public storageRefreshToken(token: string) {
|
|
|
|
|
return this.putData(refreshToken, token);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-30 22:16:38 +08:00
|
|
|
public storageAuthorizationToken(token: string) {
|
|
|
|
|
return this.putData(Authorization, token);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 15:39:53 +08:00
|
|
|
public hasAuthorizationToken() {
|
2021-12-23 15:59:49 +08:00
|
|
|
return localStorage.getItem(Authorization) != null;
|
2021-12-21 15:39:53 +08:00
|
|
|
}
|
|
|
|
|
|
2021-12-20 17:10:06 +08:00
|
|
|
public clear() {
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
}
|
2021-11-30 22:16:38 +08:00
|
|
|
}
|