@nhost/nhost-js 3.3.1 → 4.0.1
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.
- package/README.md +67 -32
- package/dist/fetch-2PHmQBIJ.mjs +61 -0
- package/dist/fetch-2PHmQBIJ.mjs.map +1 -0
- package/dist/fetch-DWXwEnoe.js +2 -0
- package/dist/fetch-DWXwEnoe.js.map +1 -0
- package/dist/nhost-js/auth.cjs.js +2 -0
- package/dist/nhost-js/auth.cjs.js.map +1 -0
- package/dist/nhost-js/auth.es.js +917 -0
- package/dist/nhost-js/auth.es.js.map +1 -0
- package/dist/nhost-js/fetch.cjs.js +2 -0
- package/dist/nhost-js/fetch.cjs.js.map +1 -0
- package/dist/nhost-js/fetch.es.js +90 -0
- package/dist/nhost-js/fetch.es.js.map +1 -0
- package/dist/nhost-js/functions.cjs.js +2 -0
- package/dist/nhost-js/functions.cjs.js.map +1 -0
- package/dist/nhost-js/functions.es.js +45 -0
- package/dist/nhost-js/functions.es.js.map +1 -0
- package/dist/nhost-js/graphql.cjs.js +2 -0
- package/dist/nhost-js/graphql.cjs.js.map +1 -0
- package/dist/nhost-js/graphql.es.js +48 -0
- package/dist/nhost-js/graphql.es.js.map +1 -0
- package/dist/nhost-js/session.cjs.js +2 -0
- package/dist/nhost-js/session.cjs.js.map +1 -0
- package/dist/nhost-js/session.es.js +234 -0
- package/dist/nhost-js/session.es.js.map +1 -0
- package/dist/nhost-js/storage.cjs.js +2 -0
- package/dist/nhost-js/storage.cjs.js.map +1 -0
- package/dist/nhost-js/storage.es.js +324 -0
- package/dist/nhost-js/storage.es.js.map +1 -0
- package/dist/nhost-js.cjs.js +2 -0
- package/dist/nhost-js.cjs.js.map +1 -0
- package/dist/nhost-js.es.js +228 -0
- package/dist/nhost-js.es.js.map +1 -0
- package/dist/nhost-js.umd.js +2 -0
- package/dist/nhost-js.umd.js.map +1 -0
- package/dist/refreshSession-Bw715ZmX.js +2 -0
- package/dist/refreshSession-Bw715ZmX.js.map +1 -0
- package/dist/refreshSession-WwGlzgtM.mjs +95 -0
- package/dist/refreshSession-WwGlzgtM.mjs.map +1 -0
- package/dist/src/auth/client.d.ts +1763 -0
- package/dist/src/auth/client.d.ts.map +1 -0
- package/dist/src/auth/client.js +1043 -0
- package/dist/src/auth/client.js.map +1 -0
- package/dist/src/auth/index.d.ts +35 -0
- package/dist/src/auth/index.d.ts.map +1 -0
- package/dist/src/auth/index.js +35 -0
- package/dist/src/auth/index.js.map +1 -0
- package/dist/src/fetch/fetch.d.ts +88 -0
- package/dist/src/fetch/fetch.d.ts.map +1 -0
- package/dist/src/fetch/fetch.js +98 -0
- package/dist/src/fetch/fetch.js.map +1 -0
- package/dist/src/fetch/index.d.ts +38 -0
- package/dist/src/fetch/index.d.ts.map +1 -0
- package/dist/src/fetch/index.js +38 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/fetch/middlewareAttachAccessToken.d.ts +24 -0
- package/dist/src/fetch/middlewareAttachAccessToken.d.ts.map +1 -0
- package/dist/src/fetch/middlewareAttachAccessToken.js +54 -0
- package/dist/src/fetch/middlewareAttachAccessToken.js.map +1 -0
- package/dist/src/fetch/middlewareSessionRefresh.d.ts +30 -0
- package/dist/src/fetch/middlewareSessionRefresh.d.ts.map +1 -0
- package/dist/src/fetch/middlewareSessionRefresh.js +61 -0
- package/dist/src/fetch/middlewareSessionRefresh.js.map +1 -0
- package/dist/src/fetch/middlewareUpdateSessionFromResponse.d.ts +26 -0
- package/dist/src/fetch/middlewareUpdateSessionFromResponse.d.ts.map +1 -0
- package/dist/src/fetch/middlewareUpdateSessionFromResponse.js +79 -0
- package/dist/src/fetch/middlewareUpdateSessionFromResponse.js.map +1 -0
- package/dist/src/functions/client.d.ts +52 -0
- package/dist/src/functions/client.d.ts.map +1 -0
- package/dist/src/functions/client.js +89 -0
- package/dist/src/functions/client.js.map +1 -0
- package/dist/src/functions/index.d.ts +43 -0
- package/dist/src/functions/index.d.ts.map +1 -0
- package/dist/src/functions/index.js +43 -0
- package/dist/src/functions/index.js.map +1 -0
- package/dist/src/graphql/client.d.ts +93 -0
- package/dist/src/graphql/client.d.ts.map +1 -0
- package/dist/src/graphql/client.js +66 -0
- package/dist/src/graphql/client.js.map +1 -0
- package/dist/src/graphql/index.d.ts +65 -0
- package/dist/src/graphql/index.d.ts.map +1 -0
- package/dist/src/graphql/index.js +65 -0
- package/dist/src/graphql/index.js.map +1 -0
- package/dist/src/index.d.ts +35 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +45 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/nhost.d.ts +301 -0
- package/dist/src/nhost.d.ts.map +1 -0
- package/dist/src/nhost.js +323 -0
- package/dist/src/nhost.js.map +1 -0
- package/dist/src/session/index.d.ts +20 -0
- package/dist/src/session/index.d.ts.map +1 -0
- package/dist/src/session/index.js +19 -0
- package/dist/src/session/index.js.map +1 -0
- package/dist/src/session/refreshSession.d.ts +17 -0
- package/dist/src/session/refreshSession.d.ts.map +1 -0
- package/dist/src/session/refreshSession.js +120 -0
- package/dist/src/session/refreshSession.js.map +1 -0
- package/dist/src/session/session.d.ts +24 -0
- package/dist/src/session/session.d.ts.map +1 -0
- package/dist/src/session/session.js +48 -0
- package/dist/src/session/session.js.map +1 -0
- package/dist/src/session/storage.d.ts +62 -0
- package/dist/src/session/storage.d.ts.map +1 -0
- package/dist/src/session/storage.js +91 -0
- package/dist/src/session/storage.js.map +1 -0
- package/dist/src/session/storageBackend.d.ts +122 -0
- package/dist/src/session/storageBackend.d.ts.map +1 -0
- package/dist/src/session/storageBackend.js +145 -0
- package/dist/src/session/storageBackend.js.map +1 -0
- package/dist/src/storage/client.d.ts +546 -0
- package/dist/src/storage/client.d.ts.map +1 -0
- package/dist/src/storage/client.js +368 -0
- package/dist/src/storage/client.js.map +1 -0
- package/dist/src/storage/index.d.ts +37 -0
- package/dist/src/storage/index.d.ts.map +1 -0
- package/dist/src/storage/index.js +37 -0
- package/dist/src/storage/index.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +103 -59
- package/dist/clients/auth.d.ts +0 -8
- package/dist/clients/auth.d.ts.map +0 -1
- package/dist/clients/functions/index.d.ts +0 -107
- package/dist/clients/functions/index.d.ts.map +0 -1
- package/dist/clients/functions/types.d.ts +0 -28
- package/dist/clients/functions/types.d.ts.map +0 -1
- package/dist/clients/graphql.d.ts +0 -8
- package/dist/clients/graphql.d.ts.map +0 -1
- package/dist/clients/index.d.ts +0 -6
- package/dist/clients/index.d.ts.map +0 -1
- package/dist/clients/nhost.d.ts +0 -83
- package/dist/clients/nhost.d.ts.map +0 -1
- package/dist/clients/storage.d.ts +0 -8
- package/dist/clients/storage.d.ts.map +0 -1
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.d.ts +0 -6
- package/dist/index.d.ts.map +0 -1
- package/dist/index.esm.js +0 -356
- package/dist/index.esm.js.map +0 -1
- package/dist/utils/helpers.d.ts +0 -20
- package/dist/utils/helpers.d.ts.map +0 -1
- package/dist/utils/types.d.ts +0 -57
- package/dist/utils/types.d.ts.map +0 -1
- package/umd/nhost-js.umd.js +0 -22
- package/umd/nhost-js.umd.js.map +0 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage implementations for session persistence in different environments.
|
|
3
|
+
*
|
|
4
|
+
* This module provides different storage adapters for persisting authentication sessions
|
|
5
|
+
* across page reloads and browser sessions.
|
|
6
|
+
*/
|
|
7
|
+
import type { Session as AuthSession } from "../auth";
|
|
8
|
+
import { type Session } from "./session";
|
|
9
|
+
import { type SessionStorageBackend } from "./storageBackend";
|
|
10
|
+
/**
|
|
11
|
+
* Callback function type for session change subscriptions
|
|
12
|
+
*/
|
|
13
|
+
export type SessionChangeCallback = (session: Session | null) => void;
|
|
14
|
+
/**
|
|
15
|
+
* A wrapper around any SessionStorageInterface implementation that adds
|
|
16
|
+
* the ability to subscribe to session changes.
|
|
17
|
+
*/
|
|
18
|
+
export declare class SessionStorage {
|
|
19
|
+
private readonly storage;
|
|
20
|
+
private subscribers;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new SessionStorage instance
|
|
23
|
+
* @param storage - The underlying storage implementation to use
|
|
24
|
+
*/
|
|
25
|
+
constructor(storage: SessionStorageBackend);
|
|
26
|
+
/**
|
|
27
|
+
* Gets the session from the underlying storage
|
|
28
|
+
* @returns The stored session or null if not found
|
|
29
|
+
*/
|
|
30
|
+
get(): Session | null;
|
|
31
|
+
/**
|
|
32
|
+
* Sets the session in the underlying storage and notifies subscribers
|
|
33
|
+
* @param value - The session to store
|
|
34
|
+
*/
|
|
35
|
+
set(value: AuthSession): void;
|
|
36
|
+
/**
|
|
37
|
+
* Removes the session from the underlying storage and notifies subscribers
|
|
38
|
+
*/
|
|
39
|
+
remove(): void;
|
|
40
|
+
/**
|
|
41
|
+
* Subscribe to session changes
|
|
42
|
+
* @param callback - Function that will be called when the session changes
|
|
43
|
+
* @returns An unsubscribe function to remove this subscription
|
|
44
|
+
*/
|
|
45
|
+
onChange(callback: SessionChangeCallback): () => void;
|
|
46
|
+
/**
|
|
47
|
+
* Notify all subscribers of a session change
|
|
48
|
+
* @param session - The new session value or null if removed
|
|
49
|
+
*/
|
|
50
|
+
private notifySubscribers;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Detects the best available storage implementation for the current environment.
|
|
54
|
+
*
|
|
55
|
+
* The detection process follows this order:
|
|
56
|
+
* 1. Try to use localStorage if we're in a browser environment
|
|
57
|
+
* 2. Fall back to in-memory storage if localStorage isn't available
|
|
58
|
+
*
|
|
59
|
+
* @returns The best available storage implementation as a SessionStorageBackend
|
|
60
|
+
*/
|
|
61
|
+
export declare const detectStorage: () => SessionStorageBackend;
|
|
62
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../../src/session/storage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,EAAqB,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AAC5D,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,kBAAkB,CAAC;AAE1B;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,KAAK,IAAI,CAAC;AAEtE;;;GAGG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAwB;IAChD,OAAO,CAAC,WAAW,CAAoC;IAEvD;;;OAGG;gBACS,OAAO,EAAE,qBAAqB;IAI1C;;;OAGG;IACH,GAAG,IAAI,OAAO,GAAG,IAAI;IAIrB;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI;IAW7B;;OAEG;IACH,MAAM,IAAI,IAAI;IAKd;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,qBAAqB;IAQxC;;;OAGG;IACH,OAAO,CAAC,iBAAiB;CAS1B;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,QAAO,qBAKhC,CAAC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage implementations for session persistence in different environments.
|
|
3
|
+
*
|
|
4
|
+
* This module provides different storage adapters for persisting authentication sessions
|
|
5
|
+
* across page reloads and browser sessions.
|
|
6
|
+
*/
|
|
7
|
+
import { decodeUserSession } from "./session";
|
|
8
|
+
import { LocalStorage, MemoryStorage, } from "./storageBackend";
|
|
9
|
+
/**
|
|
10
|
+
* A wrapper around any SessionStorageInterface implementation that adds
|
|
11
|
+
* the ability to subscribe to session changes.
|
|
12
|
+
*/
|
|
13
|
+
export class SessionStorage {
|
|
14
|
+
storage;
|
|
15
|
+
subscribers = new Set();
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new SessionStorage instance
|
|
18
|
+
* @param storage - The underlying storage implementation to use
|
|
19
|
+
*/
|
|
20
|
+
constructor(storage) {
|
|
21
|
+
this.storage = storage;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets the session from the underlying storage
|
|
25
|
+
* @returns The stored session or null if not found
|
|
26
|
+
*/
|
|
27
|
+
get() {
|
|
28
|
+
return this.storage.get();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Sets the session in the underlying storage and notifies subscribers
|
|
32
|
+
* @param value - The session to store
|
|
33
|
+
*/
|
|
34
|
+
set(value) {
|
|
35
|
+
const decodedToken = decodeUserSession(value.accessToken);
|
|
36
|
+
const decodedSession = {
|
|
37
|
+
...value,
|
|
38
|
+
decodedToken: decodedToken,
|
|
39
|
+
};
|
|
40
|
+
this.storage.set(decodedSession);
|
|
41
|
+
this.notifySubscribers(decodedSession);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Removes the session from the underlying storage and notifies subscribers
|
|
45
|
+
*/
|
|
46
|
+
remove() {
|
|
47
|
+
this.storage.remove();
|
|
48
|
+
this.notifySubscribers(null);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Subscribe to session changes
|
|
52
|
+
* @param callback - Function that will be called when the session changes
|
|
53
|
+
* @returns An unsubscribe function to remove this subscription
|
|
54
|
+
*/
|
|
55
|
+
onChange(callback) {
|
|
56
|
+
this.subscribers.add(callback);
|
|
57
|
+
return () => {
|
|
58
|
+
this.subscribers.delete(callback);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Notify all subscribers of a session change
|
|
63
|
+
* @param session - The new session value or null if removed
|
|
64
|
+
*/
|
|
65
|
+
notifySubscribers(session) {
|
|
66
|
+
for (const subscriber of this.subscribers) {
|
|
67
|
+
try {
|
|
68
|
+
subscriber(session);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
console.error("Error notifying subscriber:", error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Detects the best available storage implementation for the current environment.
|
|
78
|
+
*
|
|
79
|
+
* The detection process follows this order:
|
|
80
|
+
* 1. Try to use localStorage if we're in a browser environment
|
|
81
|
+
* 2. Fall back to in-memory storage if localStorage isn't available
|
|
82
|
+
*
|
|
83
|
+
* @returns The best available storage implementation as a SessionStorageBackend
|
|
84
|
+
*/
|
|
85
|
+
export const detectStorage = () => {
|
|
86
|
+
if (typeof window !== "undefined") {
|
|
87
|
+
return new LocalStorage();
|
|
88
|
+
}
|
|
89
|
+
return new MemoryStorage();
|
|
90
|
+
};
|
|
91
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../../src/session/storage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,iBAAiB,EAAgB,MAAM,WAAW,CAAC;AAC5D,OAAO,EACL,YAAY,EACZ,aAAa,GAEd,MAAM,kBAAkB,CAAC;AAO1B;;;GAGG;AACH,MAAM,OAAO,cAAc;IACR,OAAO,CAAwB;IACxC,WAAW,GAAG,IAAI,GAAG,EAAyB,CAAC;IAEvD;;;OAGG;IACH,YAAY,OAA8B;QACxC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,GAAG;QACD,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC;IAC5B,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,KAAkB;QACpB,MAAM,YAAY,GAAG,iBAAiB,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QAC1D,MAAM,cAAc,GAAG;YACrB,GAAG,KAAK;YACR,YAAY,EAAE,YAAY;SAC3B,CAAC;QAEF,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;QACjC,IAAI,CAAC,iBAAiB,CAAC,cAAc,CAAC,CAAC;IACzC,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;QACtB,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IAED;;;;OAIG;IACH,QAAQ,CAAC,QAA+B;QACtC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAE/B,OAAO,GAAG,EAAE;YACV,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACpC,CAAC,CAAC;IACJ,CAAC;IAED;;;OAGG;IACK,iBAAiB,CAAC,OAAuB;QAC/C,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YAC1C,IAAI,CAAC;gBACH,UAAU,CAAC,OAAO,CAAC,CAAC;YACtB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,KAAK,CAAC,CAAC;YACtD,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,GAA0B,EAAE;IACvD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,OAAO,IAAI,YAAY,EAAE,CAAC;IAC5B,CAAC;IACD,OAAO,IAAI,aAAa,EAAE,CAAC;AAC7B,CAAC,CAAC"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage implementations for session persistence in different environments.
|
|
3
|
+
*
|
|
4
|
+
* This module provides different storage adapters for persisting authentication sessions
|
|
5
|
+
* across page reloads and browser sessions.
|
|
6
|
+
*/
|
|
7
|
+
import type { Session } from "./session";
|
|
8
|
+
/**
|
|
9
|
+
* Session storage interface for session persistence.
|
|
10
|
+
* This interface can be implemented to provide custom storage solutions.
|
|
11
|
+
*/
|
|
12
|
+
export interface SessionStorageBackend {
|
|
13
|
+
/**
|
|
14
|
+
* Get the current session from storage
|
|
15
|
+
* @returns The stored session or null if not found
|
|
16
|
+
*/
|
|
17
|
+
get(): Session | null;
|
|
18
|
+
/**
|
|
19
|
+
* Set the session in storage
|
|
20
|
+
* @param value - The session to store
|
|
21
|
+
*/
|
|
22
|
+
set(value: Session): void;
|
|
23
|
+
/**
|
|
24
|
+
* Remove the session from storage
|
|
25
|
+
*/
|
|
26
|
+
remove(): void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Default storage key used for storing the Nhost session
|
|
30
|
+
*/
|
|
31
|
+
export declare const DEFAULT_SESSION_KEY = "nhostSession";
|
|
32
|
+
/**
|
|
33
|
+
* Browser localStorage implementation of StorageInterface.
|
|
34
|
+
* Persists the session across page reloads and browser restarts.
|
|
35
|
+
*/
|
|
36
|
+
export declare class LocalStorage implements SessionStorageBackend {
|
|
37
|
+
private readonly storageKey;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new LocalStorage instance
|
|
40
|
+
* @param options - Configuration options
|
|
41
|
+
* @param options.storageKey - The key to use in localStorage (defaults to "nhostSession")
|
|
42
|
+
*/
|
|
43
|
+
constructor(options?: {
|
|
44
|
+
storageKey?: string;
|
|
45
|
+
});
|
|
46
|
+
/**
|
|
47
|
+
* Gets the session from localStorage
|
|
48
|
+
* @returns The stored session or null if not found
|
|
49
|
+
*/
|
|
50
|
+
get(): Session | null;
|
|
51
|
+
/**
|
|
52
|
+
* Sets the session in localStorage
|
|
53
|
+
* @param value - The session to store
|
|
54
|
+
*/
|
|
55
|
+
set(value: Session): void;
|
|
56
|
+
/**
|
|
57
|
+
* Removes the session from localStorage
|
|
58
|
+
*/
|
|
59
|
+
remove(): void;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* In-memory storage implementation for non-browser environments or when
|
|
63
|
+
* persistent storage is not available or desirable.
|
|
64
|
+
*/
|
|
65
|
+
export declare class MemoryStorage implements SessionStorageBackend {
|
|
66
|
+
private session;
|
|
67
|
+
/**
|
|
68
|
+
* Gets the session from memory
|
|
69
|
+
* @returns The stored session or null if not set
|
|
70
|
+
*/
|
|
71
|
+
get(): Session | null;
|
|
72
|
+
/**
|
|
73
|
+
* Sets the session in memory
|
|
74
|
+
* @param value - The session to store
|
|
75
|
+
*/
|
|
76
|
+
set(value: Session): void;
|
|
77
|
+
/**
|
|
78
|
+
* Clears the session from memory
|
|
79
|
+
*/
|
|
80
|
+
remove(): void;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Cookie-based storage implementation.
|
|
84
|
+
* This storage uses web browser cookies to store the session so it's not
|
|
85
|
+
* available in server-side environments. It is useful though for synchronizing
|
|
86
|
+
* sessions between client and server environments.
|
|
87
|
+
*/
|
|
88
|
+
export declare class CookieStorage implements SessionStorageBackend {
|
|
89
|
+
private readonly cookieName;
|
|
90
|
+
private readonly expirationDays;
|
|
91
|
+
private readonly secure;
|
|
92
|
+
private readonly sameSite;
|
|
93
|
+
/**
|
|
94
|
+
* Creates a new CookieStorage instance
|
|
95
|
+
* @param options - Configuration options
|
|
96
|
+
* @param options.cookieName - Name of the cookie to use (defaults to "nhostSession")
|
|
97
|
+
* @param options.expirationDays - Number of days until the cookie expires (defaults to 30)
|
|
98
|
+
* @param options.secure - Whether to set the Secure flag on the cookie (defaults to true)
|
|
99
|
+
* @param options.sameSite - SameSite policy for the cookie (defaults to "lax")
|
|
100
|
+
*/
|
|
101
|
+
constructor(options?: {
|
|
102
|
+
cookieName?: string;
|
|
103
|
+
expirationDays?: number;
|
|
104
|
+
secure?: boolean;
|
|
105
|
+
sameSite?: "strict" | "lax" | "none";
|
|
106
|
+
});
|
|
107
|
+
/**
|
|
108
|
+
* Gets the session from cookies
|
|
109
|
+
* @returns The stored session or null if not found
|
|
110
|
+
*/
|
|
111
|
+
get(): Session | null;
|
|
112
|
+
/**
|
|
113
|
+
* Sets the session in a cookie
|
|
114
|
+
* @param value - The session to store
|
|
115
|
+
*/
|
|
116
|
+
set(value: Session): void;
|
|
117
|
+
/**
|
|
118
|
+
* Removes the session cookie
|
|
119
|
+
*/
|
|
120
|
+
remove(): void;
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=storageBackend.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storageBackend.d.ts","sourceRoot":"","sources":["../../../src/session/storageBackend.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEzC;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,GAAG,IAAI,OAAO,GAAG,IAAI,CAAC;IAEtB;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IAE1B;;OAEG;IACH,MAAM,IAAI,IAAI,CAAC;CAChB;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,iBAAiB,CAAC;AAElD;;;GAGG;AACH,qBAAa,YAAa,YAAW,qBAAqB;IACxD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IAEpC;;;;OAIG;gBACS,OAAO,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE;IAI7C;;;OAGG;IACH,GAAG,IAAI,OAAO,GAAG,IAAI;IAUrB;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAIzB;;OAEG;IACH,MAAM,IAAI,IAAI;CAGf;AAED;;;GAGG;AACH,qBAAa,aAAc,YAAW,qBAAqB;IACzD,OAAO,CAAC,OAAO,CAAwB;IAEvC;;;OAGG;IACH,GAAG,IAAI,OAAO,GAAG,IAAI;IAIrB;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAIzB;;OAEG;IACH,MAAM,IAAI,IAAI;CAGf;AAED;;;;;GAKG;AACH,qBAAa,aAAc,YAAW,qBAAqB;IACzD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAS;IACxC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IACjC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA4B;IAErD;;;;;;;OAOG;gBACS,OAAO,CAAC,EAAE;QACpB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,QAAQ,CAAC,EAAE,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAC;KACtC;IAOD;;;OAGG;IACH,GAAG,IAAI,OAAO,GAAG,IAAI;IAgBrB;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAazB;;OAEG;IACH,MAAM,IAAI,IAAI;CAIf"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage implementations for session persistence in different environments.
|
|
3
|
+
*
|
|
4
|
+
* This module provides different storage adapters for persisting authentication sessions
|
|
5
|
+
* across page reloads and browser sessions.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Default storage key used for storing the Nhost session
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_SESSION_KEY = "nhostSession";
|
|
11
|
+
/**
|
|
12
|
+
* Browser localStorage implementation of StorageInterface.
|
|
13
|
+
* Persists the session across page reloads and browser restarts.
|
|
14
|
+
*/
|
|
15
|
+
export class LocalStorage {
|
|
16
|
+
storageKey;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new LocalStorage instance
|
|
19
|
+
* @param options - Configuration options
|
|
20
|
+
* @param options.storageKey - The key to use in localStorage (defaults to "nhostSession")
|
|
21
|
+
*/
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.storageKey = options?.storageKey || DEFAULT_SESSION_KEY;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Gets the session from localStorage
|
|
27
|
+
* @returns The stored session or null if not found
|
|
28
|
+
*/
|
|
29
|
+
get() {
|
|
30
|
+
try {
|
|
31
|
+
const value = window.localStorage.getItem(this.storageKey);
|
|
32
|
+
return value ? JSON.parse(value) : null;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
this.remove();
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Sets the session in localStorage
|
|
41
|
+
* @param value - The session to store
|
|
42
|
+
*/
|
|
43
|
+
set(value) {
|
|
44
|
+
window.localStorage.setItem(this.storageKey, JSON.stringify(value));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Removes the session from localStorage
|
|
48
|
+
*/
|
|
49
|
+
remove() {
|
|
50
|
+
window.localStorage.removeItem(this.storageKey);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* In-memory storage implementation for non-browser environments or when
|
|
55
|
+
* persistent storage is not available or desirable.
|
|
56
|
+
*/
|
|
57
|
+
export class MemoryStorage {
|
|
58
|
+
session = null;
|
|
59
|
+
/**
|
|
60
|
+
* Gets the session from memory
|
|
61
|
+
* @returns The stored session or null if not set
|
|
62
|
+
*/
|
|
63
|
+
get() {
|
|
64
|
+
return this.session;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Sets the session in memory
|
|
68
|
+
* @param value - The session to store
|
|
69
|
+
*/
|
|
70
|
+
set(value) {
|
|
71
|
+
this.session = value;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Clears the session from memory
|
|
75
|
+
*/
|
|
76
|
+
remove() {
|
|
77
|
+
this.session = null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Cookie-based storage implementation.
|
|
82
|
+
* This storage uses web browser cookies to store the session so it's not
|
|
83
|
+
* available in server-side environments. It is useful though for synchronizing
|
|
84
|
+
* sessions between client and server environments.
|
|
85
|
+
*/
|
|
86
|
+
export class CookieStorage {
|
|
87
|
+
cookieName;
|
|
88
|
+
expirationDays;
|
|
89
|
+
secure;
|
|
90
|
+
sameSite;
|
|
91
|
+
/**
|
|
92
|
+
* Creates a new CookieStorage instance
|
|
93
|
+
* @param options - Configuration options
|
|
94
|
+
* @param options.cookieName - Name of the cookie to use (defaults to "nhostSession")
|
|
95
|
+
* @param options.expirationDays - Number of days until the cookie expires (defaults to 30)
|
|
96
|
+
* @param options.secure - Whether to set the Secure flag on the cookie (defaults to true)
|
|
97
|
+
* @param options.sameSite - SameSite policy for the cookie (defaults to "lax")
|
|
98
|
+
*/
|
|
99
|
+
constructor(options) {
|
|
100
|
+
this.cookieName = options?.cookieName || DEFAULT_SESSION_KEY;
|
|
101
|
+
this.expirationDays = options?.expirationDays ?? 30;
|
|
102
|
+
this.secure = options?.secure ?? true;
|
|
103
|
+
this.sameSite = options?.sameSite || "lax";
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Gets the session from cookies
|
|
107
|
+
* @returns The stored session or null if not found
|
|
108
|
+
*/
|
|
109
|
+
get() {
|
|
110
|
+
const cookies = document.cookie.split(";");
|
|
111
|
+
for (const cookie of cookies) {
|
|
112
|
+
const [name, value] = cookie.trim().split("=");
|
|
113
|
+
if (name === this.cookieName) {
|
|
114
|
+
try {
|
|
115
|
+
return JSON.parse(decodeURIComponent(value || ""));
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
this.remove();
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Sets the session in a cookie
|
|
127
|
+
* @param value - The session to store
|
|
128
|
+
*/
|
|
129
|
+
set(value) {
|
|
130
|
+
const expires = new Date();
|
|
131
|
+
expires.setTime(expires.getTime() + this.expirationDays * 24 * 60 * 60 * 1000);
|
|
132
|
+
const cookieValue = encodeURIComponent(JSON.stringify(value));
|
|
133
|
+
const cookieString = `${this.cookieName}=${cookieValue}; expires=${expires.toUTCString()}; path=/; ${this.secure ? "secure; " : ""}SameSite=${this.sameSite}`;
|
|
134
|
+
// biome-ignore lint/suspicious/noDocumentCookie: this is unnecessary
|
|
135
|
+
document.cookie = cookieString;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Removes the session cookie
|
|
139
|
+
*/
|
|
140
|
+
remove() {
|
|
141
|
+
// biome-ignore lint/suspicious/noDocumentCookie: this is unnecessary
|
|
142
|
+
document.cookie = `${this.cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; ${this.secure ? "secure; " : ""}SameSite=${this.sameSite}`;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=storageBackend.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storageBackend.js","sourceRoot":"","sources":["../../../src/session/storageBackend.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA2BH;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,cAAc,CAAC;AAElD;;;GAGG;AACH,MAAM,OAAO,YAAY;IACN,UAAU,CAAS;IAEpC;;;;OAIG;IACH,YAAY,OAAiC;QAC3C,IAAI,CAAC,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,mBAAmB,CAAC;IAC/D,CAAC;IAED;;;OAGG;IACH,GAAG;QACD,IAAI,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC3D,OAAO,KAAK,CAAC,CAAC,CAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAa,CAAC,CAAC,CAAC,IAAI,CAAC;QACvD,CAAC;QAAC,MAAM,CAAC;YACP,IAAI,CAAC,MAAM,EAAE,CAAC;YACd,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED;;;OAGG;I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