@koolbase/js 12.43.0 → 12.44.0

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/CHANGELOG.md CHANGED
@@ -7,6 +7,14 @@ is based on [Keep a Changelog][kac], and this project adheres to
7
7
  [kac]: https://keepachangelog.com/en/1.1.0/
8
8
  [semver]: https://semver.org/
9
9
 
10
+ ## 12.44.0
11
+
12
+ Remember me: a sign-in says whether it outlasts the browser.
13
+
14
+ - **`remember`** on `login`, `signInWithEmailCode`, `verifyOtp`, `signInWithGoogle` and `signInWithApple`. Omitted, a sign-in is remembered, as before. `remember: false` keeps the session in the tab's `sessionStorage`: it ends when the tab or browser closes, and a new tab asks again. A sign-in that goes on to a second step (`verifyMfa`, `verifyRecoveryCode`) keeps its choice. Never sent to the server.
15
+ - **`BrowserAuthStorage`** keeps the session in one place at a time: IndexedDB when remembered, the tab when not. A token refresh saves it where it already lives, so a session that is not remembered never reaches IndexedDB. `clear()` removes both.
16
+ - **`KoolbaseAuthStorage.setRemember`** (optional) is how a storage is told. Storages without it -- React Native's keychain, an app's own -- keep every session as before.
17
+
10
18
  ## 12.39.0
11
19
 
12
20
  A device registers as its signed-in person's (R138). Same contract as koolbase_flutter 12.53.0.
@@ -1,5 +1,12 @@
1
1
  import type { KoolbaseAuthStorage, KoolbaseSession } from '@koolbase/core';
2
2
  export declare class BrowserAuthStorage implements KoolbaseAuthStorage {
3
+ /** Set by a sign-in (setRemember), or found where the session is. */
4
+ private place;
5
+ /** A tab session where the tab has no store of its own. */
6
+ private memory;
7
+ private tabRead;
8
+ private tabWrite;
9
+ setRemember(remember: boolean): Promise<void>;
3
10
  readSession(): Promise<KoolbaseSession | null>;
4
11
  saveSession(session: KoolbaseSession): Promise<void>;
5
12
  clear(): Promise<void>;
@@ -8,6 +8,12 @@ const core_1 = require("@koolbase/core");
8
8
  // KoolbaseConfig.authStorage. It keeps the session in the platform's storage
9
9
  // — IndexedDB — so a page refresh does not sign the user out.
10
10
  //
11
+ // Remember me (12.44.0): a sign-in made with `remember: false` is kept in the
12
+ // tab's sessionStorage instead, so it ends when the tab or the browser closes
13
+ // and a new tab asks again — the browser's usual meaning, and Firebase's
14
+ // "session" persistence. A refresh saves the session where it already lives,
15
+ // so a session that is not remembered never reaches IndexedDB.
16
+ //
11
17
  // What it is not: secure the way a keychain is. IndexedDB, localStorage and
12
18
  // every other store a page can read are readable by any script running on
13
19
  // that page, so an XSS vulnerability in the app exposes the session. That is
@@ -16,23 +22,103 @@ const core_1 = require("@koolbase/core");
16
22
  // storage backend. An app that needs a different trade-off — an httpOnly
17
23
  // cookie set by its own backend, say — injects its own KoolbaseAuthStorage.
18
24
  const KEY = 'koolbase_session_v1';
25
+ /** This tab's session: a sign-in that is not remembered. */
26
+ const TAB_KEY = 'koolbase_session_tab_v1';
27
+ /** The tab's own store, or null where there is none (a server render, a
28
+ * sandboxed frame that refuses it). Then a tab session lives in memory and
29
+ * ends with the page — never in IndexedDB. */
30
+ function tabStore() {
31
+ try {
32
+ const s = globalThis.sessionStorage;
33
+ return s ?? null;
34
+ }
35
+ catch {
36
+ return null;
37
+ }
38
+ }
19
39
  class BrowserAuthStorage {
40
+ constructor() {
41
+ /** Set by a sign-in (setRemember), or found where the session is. */
42
+ this.place = null;
43
+ /** A tab session where the tab has no store of its own. */
44
+ this.memory = null;
45
+ }
46
+ tabRead() {
47
+ const s = tabStore();
48
+ if (!s)
49
+ return this.memory;
50
+ try {
51
+ return s.getItem(TAB_KEY);
52
+ }
53
+ catch {
54
+ return this.memory;
55
+ }
56
+ }
57
+ tabWrite(raw) {
58
+ this.memory = raw;
59
+ const s = tabStore();
60
+ if (!s)
61
+ return;
62
+ try {
63
+ if (raw === null)
64
+ s.removeItem(TAB_KEY);
65
+ else
66
+ s.setItem(TAB_KEY, raw);
67
+ }
68
+ catch {
69
+ // Refused (quota, a sandbox): it lives in memory, for this page.
70
+ }
71
+ }
72
+ async setRemember(remember) {
73
+ this.place = remember ? 'kept' : 'tab';
74
+ // One place at a time: the other's copy belongs to an earlier sign-in.
75
+ if (remember)
76
+ this.tabWrite(null);
77
+ else
78
+ await (0, core_1.getPlatform)().storage.removeItem(KEY);
79
+ }
20
80
  async readSession() {
81
+ // This tab's session first: a sign-in here that is not remembered.
82
+ const tab = this.tabRead();
83
+ if (tab) {
84
+ try {
85
+ const session = JSON.parse(tab);
86
+ this.place = 'tab';
87
+ return session;
88
+ }
89
+ catch {
90
+ this.tabWrite(null);
91
+ }
92
+ }
21
93
  const raw = await (0, core_1.getPlatform)().storage.getItem(KEY);
22
94
  if (!raw)
23
95
  return null;
24
96
  try {
25
- return JSON.parse(raw);
97
+ const session = JSON.parse(raw);
98
+ this.place = 'kept';
99
+ return session;
26
100
  }
27
101
  catch {
28
- await this.clear();
102
+ await (0, core_1.getPlatform)().storage.removeItem(KEY);
29
103
  return null;
30
104
  }
31
105
  }
32
106
  async saveSession(session) {
33
- await (0, core_1.getPlatform)().storage.setItem(KEY, JSON.stringify(session));
107
+ const raw = JSON.stringify(session);
108
+ // Where it already lives: a refresh never moves a tab session to
109
+ // IndexedDB. Unknown (a fresh page that has not read): the tab's copy
110
+ // says so if there is one.
111
+ const place = this.place ?? (this.tabRead() !== null ? 'tab' : 'kept');
112
+ this.place = place;
113
+ if (place === 'tab') {
114
+ this.tabWrite(raw);
115
+ return;
116
+ }
117
+ await (0, core_1.getPlatform)().storage.setItem(KEY, raw);
34
118
  }
35
119
  async clear() {
120
+ this.tabWrite(null);
121
+ this.place = null;
36
122
  await (0, core_1.getPlatform)().storage.removeItem(KEY);
37
123
  }
38
124
  }
@@ -1,5 +1,12 @@
1
1
  import type { KoolbaseAuthStorage, KoolbaseSession } from '@koolbase/core';
2
2
  export declare class BrowserAuthStorage implements KoolbaseAuthStorage {
3
+ /** Set by a sign-in (setRemember), or found where the session is. */
4
+ private place;
5
+ /** A tab session where the tab has no store of its own. */
6
+ private memory;
7
+ private tabRead;
8
+ private tabWrite;
9
+ setRemember(remember: boolean): Promise<void>;
3
10
  readSession(): Promise<KoolbaseSession | null>;
4
11
  saveSession(session: KoolbaseSession): Promise<void>;
5
12
  clear(): Promise<void>;
@@ -5,6 +5,12 @@ import { getPlatform } from '@koolbase/core';
5
5
  // KoolbaseConfig.authStorage. It keeps the session in the platform's storage
6
6
  // — IndexedDB — so a page refresh does not sign the user out.
7
7
  //
8
+ // Remember me (12.44.0): a sign-in made with `remember: false` is kept in the
9
+ // tab's sessionStorage instead, so it ends when the tab or the browser closes
10
+ // and a new tab asks again — the browser's usual meaning, and Firebase's
11
+ // "session" persistence. A refresh saves the session where it already lives,
12
+ // so a session that is not remembered never reaches IndexedDB.
13
+ //
8
14
  // What it is not: secure the way a keychain is. IndexedDB, localStorage and
9
15
  // every other store a page can read are readable by any script running on
10
16
  // that page, so an XSS vulnerability in the app exposes the session. That is
@@ -13,23 +19,103 @@ import { getPlatform } from '@koolbase/core';
13
19
  // storage backend. An app that needs a different trade-off — an httpOnly
14
20
  // cookie set by its own backend, say — injects its own KoolbaseAuthStorage.
15
21
  const KEY = 'koolbase_session_v1';
22
+ /** This tab's session: a sign-in that is not remembered. */
23
+ const TAB_KEY = 'koolbase_session_tab_v1';
24
+ /** The tab's own store, or null where there is none (a server render, a
25
+ * sandboxed frame that refuses it). Then a tab session lives in memory and
26
+ * ends with the page — never in IndexedDB. */
27
+ function tabStore() {
28
+ try {
29
+ const s = globalThis.sessionStorage;
30
+ return s ?? null;
31
+ }
32
+ catch {
33
+ return null;
34
+ }
35
+ }
16
36
  export class BrowserAuthStorage {
37
+ constructor() {
38
+ /** Set by a sign-in (setRemember), or found where the session is. */
39
+ this.place = null;
40
+ /** A tab session where the tab has no store of its own. */
41
+ this.memory = null;
42
+ }
43
+ tabRead() {
44
+ const s = tabStore();
45
+ if (!s)
46
+ return this.memory;
47
+ try {
48
+ return s.getItem(TAB_KEY);
49
+ }
50
+ catch {
51
+ return this.memory;
52
+ }
53
+ }
54
+ tabWrite(raw) {
55
+ this.memory = raw;
56
+ const s = tabStore();
57
+ if (!s)
58
+ return;
59
+ try {
60
+ if (raw === null)
61
+ s.removeItem(TAB_KEY);
62
+ else
63
+ s.setItem(TAB_KEY, raw);
64
+ }
65
+ catch {
66
+ // Refused (quota, a sandbox): it lives in memory, for this page.
67
+ }
68
+ }
69
+ async setRemember(remember) {
70
+ this.place = remember ? 'kept' : 'tab';
71
+ // One place at a time: the other's copy belongs to an earlier sign-in.
72
+ if (remember)
73
+ this.tabWrite(null);
74
+ else
75
+ await getPlatform().storage.removeItem(KEY);
76
+ }
17
77
  async readSession() {
78
+ // This tab's session first: a sign-in here that is not remembered.
79
+ const tab = this.tabRead();
80
+ if (tab) {
81
+ try {
82
+ const session = JSON.parse(tab);
83
+ this.place = 'tab';
84
+ return session;
85
+ }
86
+ catch {
87
+ this.tabWrite(null);
88
+ }
89
+ }
18
90
  const raw = await getPlatform().storage.getItem(KEY);
19
91
  if (!raw)
20
92
  return null;
21
93
  try {
22
- return JSON.parse(raw);
94
+ const session = JSON.parse(raw);
95
+ this.place = 'kept';
96
+ return session;
23
97
  }
24
98
  catch {
25
- await this.clear();
99
+ await getPlatform().storage.removeItem(KEY);
26
100
  return null;
27
101
  }
28
102
  }
29
103
  async saveSession(session) {
30
- await getPlatform().storage.setItem(KEY, JSON.stringify(session));
104
+ const raw = JSON.stringify(session);
105
+ // Where it already lives: a refresh never moves a tab session to
106
+ // IndexedDB. Unknown (a fresh page that has not read): the tab's copy
107
+ // says so if there is one.
108
+ const place = this.place ?? (this.tabRead() !== null ? 'tab' : 'kept');
109
+ this.place = place;
110
+ if (place === 'tab') {
111
+ this.tabWrite(raw);
112
+ return;
113
+ }
114
+ await getPlatform().storage.setItem(KEY, raw);
31
115
  }
32
116
  async clear() {
117
+ this.tabWrite(null);
118
+ this.place = null;
33
119
  await getPlatform().storage.removeItem(KEY);
34
120
  }
35
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koolbase/js",
3
- "version": "12.43.0",
3
+ "version": "12.44.0",
4
4
  "description": "Koolbase SDK for the browser \u2014 auth, database, storage, realtime, functions, flags and offline sync in one package.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/esm/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  "url": "https://github.com/koolbase/koolbase-react-native"
27
27
  },
28
28
  "dependencies": {
29
- "@koolbase/core": "^12.43.0"
29
+ "@koolbase/core": "^12.44.0"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"