blob: 62bc07e530411963a74e59dc8451e1e3786d39b3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import { Injectable } from '@angular/core';
import { Guid } from 'guid-typescript';
import jwt_decode from 'jwt-decode';
import { IJWTPayload } from 'src/interfaces/jwt-payload';
import { IUserCredentials } from 'src/interfaces/user-credentials';
import { AppConstants } from '../app-constants.module';
@Injectable({
providedIn: 'root'
})
export class TokenService {
constructor()
{ }
/* Session storage */
setUserTokenToSessionStorage(response: object): void {
const token = JSON.stringify(response);
sessionStorage.setItem(AppConstants.SESSION_TOKEN_KEY, token.substr(10, token.length - 12));
}
getTokenFromSessionStorage(): string {
return sessionStorage.getItem(AppConstants.SESSION_TOKEN_KEY) ?? '';
}
getUserIdFromSessionStorageToken(): Guid {
const jwt: IJWTPayload = {
token: this.getTokenFromSessionStorage()
};
const userCred = jwt_decode<IUserCredentials>(jwt.token);
return userCred.ID;
}
getUsernameFromSessionStorageToken(): string {
const jwt: IJWTPayload = {
token: this.getTokenFromSessionStorage()
};
const userCred = jwt_decode<IUserCredentials>(jwt.token);
return userCred.Username;
}
logoutUserFromSessionStorage(): void {
sessionStorage.removeItem(AppConstants.SESSION_TOKEN_KEY);
}
}
|