-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathLocalAuthStore.ts
141 lines (118 loc) · 3.56 KB
/
LocalAuthStore.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { BaseAuthStore, AuthRecord } from "@/stores/BaseAuthStore";
/**
* The default token store for browsers with auto fallback
* to runtime/memory if local storage is undefined (e.g. in node env).
*/
export class LocalAuthStore extends BaseAuthStore {
private storageFallback: { [key: string]: any } = {};
private storageKey: string;
constructor(storageKey = "pocketbase_auth") {
super();
this.storageKey = storageKey;
this._bindStorageEvent();
}
/**
* @inheritdoc
*/
get token(): string {
const data = this._storageGet(this.storageKey) || {};
return data.token || "";
}
/**
* @inheritdoc
*/
get record(): AuthRecord {
const data = this._storageGet(this.storageKey) || {};
return data.record || data.model || null;
}
/**
* @deprecated use `record` instead.
*/
get model(): AuthRecord {
return this.record;
}
/**
* @inheritdoc
*/
save(token: string, record?: AuthRecord) {
this._storageSet(this.storageKey, {
token: token,
record: record,
});
super.save(token, record);
}
/**
* @inheritdoc
*/
clear() {
this._storageRemove(this.storageKey);
super.clear();
}
// ---------------------------------------------------------------
// Internal helpers:
// ---------------------------------------------------------------
/**
* Retrieves `key` from the browser's local storage
* (or runtime/memory if local storage is undefined).
*/
private _storageGet(key: string): any {
if (typeof window !== "undefined" && window?.localStorage) {
const rawValue = window.localStorage.getItem(key) || "";
try {
return JSON.parse(rawValue);
} catch (e) {
// not a json
return rawValue;
}
}
// fallback
return this.storageFallback[key];
}
/**
* Stores a new data in the browser's local storage
* (or runtime/memory if local storage is undefined).
*/
private _storageSet(key: string, value: any) {
if (typeof window !== "undefined" && window?.localStorage) {
// store in local storage
let normalizedVal = value;
if (typeof value !== "string") {
normalizedVal = JSON.stringify(value);
}
window.localStorage.setItem(key, normalizedVal);
} else {
// store in fallback
this.storageFallback[key] = value;
}
}
/**
* Removes `key` from the browser's local storage and the runtime/memory.
*/
private _storageRemove(key: string) {
// delete from local storage
if (typeof window !== "undefined" && window?.localStorage) {
window.localStorage?.removeItem(key);
}
// delete from fallback
delete this.storageFallback[key];
}
/**
* Updates the current store state on localStorage change.
*/
private _bindStorageEvent() {
if (
typeof window === "undefined" ||
!window?.localStorage ||
!window.addEventListener
) {
return;
}
window.addEventListener("storage", (e) => {
if (e.key != this.storageKey) {
return;
}
const data = this._storageGet(this.storageKey) || {};
super.save(data.token || "", data.record || data.model || null);
});
}
}