-
Notifications
You must be signed in to change notification settings - Fork 47
/
session-storage.ts
53 lines (47 loc) · 1.42 KB
/
session-storage.ts
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
48
49
50
51
52
53
/*
* @forgerock/javascript-sdk
*
* session-storage.ts
*
* Copyright (c) 2020 ForgeRock. All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
import { Tokens } from '../shared/interfaces';
import { DB_NAME } from './constants';
/**
* Provides wrapper for tokens with sessionStorage.
*/
abstract class SessionStorageWrapper {
/**
* Retrieve tokens.
*/
public static async get(clientId: string): Promise<Tokens> {
const tokenString = sessionStorage.getItem(`${DB_NAME}-${clientId}`);
try {
return Promise.resolve(JSON.parse(tokenString || ''));
} catch (err) {
console.warn(
'Could not parse token from sessionStorage. This could be due to accessing a removed token',
);
// Original behavior had an untyped return of undefined for no token
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return undefined;
}
}
/**
* Saves tokens.
*/
public static async set(clientId: string, tokens: Tokens): Promise<void> {
const tokenString = JSON.stringify(tokens);
sessionStorage.setItem(`${DB_NAME}-${clientId}`, tokenString);
}
/**
* Removes stored tokens.
*/
public static async remove(clientId: string): Promise<void> {
sessionStorage.removeItem(`${DB_NAME}-${clientId}`);
}
}
export default SessionStorageWrapper;