@imtbl/auth-next-client 2.12.7-alpha.1 → 2.12.7-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Utility for persisting idToken in localStorage.
3
+ *
4
+ * The idToken is stripped from the NextAuth session cookie (via a custom
5
+ * jwt.encode in @imtbl/auth-next-server) to keep cookie size under CDN header
6
+ * limits (CloudFront 20 KB). Instead, the client stores idToken in
7
+ * localStorage so that wallet operations (e.g., MagicTEESigner) can still
8
+ * access it via getUser().
9
+ *
10
+ * All functions are safe to call during SSR or in restricted environments
11
+ * (e.g., incognito mode with localStorage disabled) -- they silently no-op.
12
+ */
13
+
14
+ const ID_TOKEN_STORAGE_KEY = 'imtbl_id_token';
15
+
16
+ /**
17
+ * Store the idToken in localStorage.
18
+ * @param idToken - The raw ID token JWT string
19
+ */
20
+ export function storeIdToken(idToken: string): void {
21
+ try {
22
+ if (typeof window !== 'undefined' && window.localStorage) {
23
+ window.localStorage.setItem(ID_TOKEN_STORAGE_KEY, idToken);
24
+ }
25
+ } catch {
26
+ // Silently ignore -- localStorage may be unavailable (SSR, incognito, etc.)
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Retrieve the idToken from localStorage.
32
+ * @returns The stored idToken, or undefined if not available.
33
+ */
34
+ export function getStoredIdToken(): string | undefined {
35
+ try {
36
+ if (typeof window !== 'undefined' && window.localStorage) {
37
+ return window.localStorage.getItem(ID_TOKEN_STORAGE_KEY) ?? undefined;
38
+ }
39
+ } catch {
40
+ // Silently ignore
41
+ }
42
+ return undefined;
43
+ }
44
+
45
+ /**
46
+ * Remove the idToken from localStorage (e.g., on logout).
47
+ */
48
+ export function clearStoredIdToken(): void {
49
+ try {
50
+ if (typeof window !== 'undefined' && window.localStorage) {
51
+ window.localStorage.removeItem(ID_TOKEN_STORAGE_KEY);
52
+ }
53
+ } catch {
54
+ // Silently ignore
55
+ }
56
+ }