forked from authts/oidc-client-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebStorageStateStore.ts
More file actions
53 lines (42 loc) · 1.6 KB
/
Copy pathWebStorageStateStore.ts
File metadata and controls
53 lines (42 loc) · 1.6 KB
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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import { Log } from "./utils";
import { StateStore } from "./StateStore";
export class WebStorageStateStore implements StateStore {
private _store: Storage
private _prefix: string
constructor({ prefix = "oidc.", store = localStorage } = {}) {
this._store = store;
this._prefix = prefix;
}
set(key: string, value: string): Promise<void> {
Log.debug("WebStorageStateStore.set", key);
key = this._prefix + key;
this._store.setItem(key, value);
return Promise.resolve();
}
get(key: string): Promise<string | null> {
Log.debug("WebStorageStateStore.get", key);
key = this._prefix + key;
const item = this._store.getItem(key);
return Promise.resolve(item);
}
remove(key: string): Promise<string | null> {
Log.debug("WebStorageStateStore.remove", key);
key = this._prefix + key;
const item = this._store.getItem(key);
this._store.removeItem(key);
return Promise.resolve(item);
}
getAllKeys(): Promise<string[]> {
Log.debug("WebStorageStateStore.getAllKeys");
const keys = [];
for (let index = 0; index < this._store.length; index++) {
const key = this._store.key(index);
if (key && key.indexOf(this._prefix) === 0) {
keys.push(key.substr(this._prefix.length));
}
}
return Promise.resolve(keys);
}
}