@spacelr/sdk 0.18.3 → 0.19.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/README.md CHANGED
@@ -127,6 +127,48 @@ Notes:
127
127
  consumers performing the ceremony — Node/server usage of the SDK never
128
128
  needs it.
129
129
 
130
+ ## Push subscriptions and the session
131
+
132
+ A push subscription is bound to the session it was registered under. When a
133
+ session is revoked remotely ("sign out this device", "sign out all other
134
+ devices", removing a passkey), the server deactivates that device's push
135
+ subscription, so a lost or stolen device stops showing notifications.
136
+
137
+ The SDK keeps this device's subscription in sync for you (`autoManagePush`,
138
+ default `true`):
139
+
140
+ - After every sign-in, it re-registers the device's last push registration
141
+ for the signed-in user and their new session. Signing in over a live
142
+ session (user B without a logout of user A) first unregisters the device for
143
+ the outgoing session, so its pushes move from A to B.
144
+ - `auth.logout()` first unregisters this device's subscription, while the
145
+ session is still valid, so a signed-out device stops receiving the user's
146
+ pushes. Logout waits at most 3 seconds for this.
147
+ - Calling `notifications.unsubscribe()` / `unsubscribeById()` for this device
148
+ turns push off for good: the SDK no longer re-registers it, even if the
149
+ server call fails.
150
+ - Within a client instance, all register/unregister calls run in call order,
151
+ so a late re-register never lands after a logout's unregister. If that
152
+ unregister hangs, the next sign-in's re-register waits for it (at most the
153
+ client's request `timeout`).
154
+
155
+ Everything here is best-effort and never fails a sign-in or logout. If the
156
+ unregister cannot reach the server (offline logout), the device keeps the
157
+ previous user's subscription. A later registration for another user on that
158
+ device is then refused (`DEVICE_ALREADY_REGISTERED`) until the previous user
159
+ deletes that subscription, e.g. via `getSubscriptions()` + `unsubscribeById()`
160
+ from another device. A remote session revoke only deactivates it.
161
+
162
+ The registration is remembered in `localStorage` (per project), falling back
163
+ to memory. With `createClient({ ..., autoManagePush: false })` nothing changes
164
+ automatically, and `subscribe()` / `unsubscribe()` are entirely up to the app.
165
+ In that case, call `subscribe()` after every sign-in yourself.
166
+
167
+ Apps that sign in or out by setting tokens directly (`setTokens()` /
168
+ `clearTokens()`) bypass the automatic handling. They call
169
+ `notifications.resubscribeAfterLogin()` after `setTokens()`, and
170
+ `notifications.unsubscribeBeforeLogout()` before `clearTokens()`.
171
+
130
172
  ## Requirements
131
173
 
132
174
  - Node.js >= 18
package/dist/index.d.mts CHANGED
@@ -380,6 +380,14 @@ interface SpacelrClientConfig {
380
380
  tokenStorage?: TokenStorage;
381
381
  /** Buffer in seconds before token expiry to trigger auto-refresh (default: 60) */
382
382
  refreshBufferSeconds?: number;
383
+ /**
384
+ * Keep this device's push subscription in sync with the session (default:
385
+ * true). After every login the SDK re-registers the device's last push
386
+ * registration for the signed-in user; `auth.logout()` unregisters it first.
387
+ * Set to false to manage `notifications.subscribe()` / `unsubscribe()`
388
+ * entirely yourself.
389
+ */
390
+ autoManagePush?: boolean;
383
391
  }
384
392
 
385
393
  interface FileInfo {
@@ -1080,7 +1088,19 @@ declare class AuthModule {
1080
1088
  private stateListeners;
1081
1089
  private unsubscribeAuthLost;
1082
1090
  private lastEmittedState;
1083
- constructor(http: HttpClient, tokenManager: TokenManager, config: SpacelrClientConfig);
1091
+ private beforeLogout?;
1092
+ private afterSignIn?;
1093
+ constructor(http: HttpClient, tokenManager: TokenManager, config: SpacelrClientConfig,
1094
+ /**
1095
+ * Wired by `createClient`. `beforeLogout` runs while the session is still
1096
+ * valid during `logout()`; `afterSignIn` runs after EVERY sign-in, even
1097
+ * one that does not change the auth state, because each sign-in starts a
1098
+ * new session.
1099
+ */
1100
+ hooks?: {
1101
+ beforeLogout?: () => Promise<void>;
1102
+ afterSignIn?: () => Promise<void>;
1103
+ });
1084
1104
  /**
1085
1105
  * Returns true if a non-expired access token is currently in storage.
1086
1106
  * Does NOT make a network request — safe for route guards and other
@@ -1122,6 +1142,13 @@ declare class AuthModule {
1122
1142
  * onAuthLost subscription. Idempotent — safe to call more than once.
1123
1143
  */
1124
1144
  dispose(): void;
1145
+ /**
1146
+ * Install the tokens of a completed sign-in. Signing in over a live session
1147
+ * (e.g. user B over user A without a logout) first runs `beforeLogout` with
1148
+ * the outgoing session's still-valid token, so this device's push leaves
1149
+ * the previous user before it is re-registered for the new one.
1150
+ */
1151
+ private installSession;
1125
1152
  private emitState;
1126
1153
  login(params: LoginParams): Promise<LoginResponse>;
1127
1154
  register(params: RegisterParams): Promise<RegisterResponse>;
@@ -2104,11 +2131,27 @@ interface PushSubscriptionInfo {
2104
2131
  interface VapidKeyResponse {
2105
2132
  publicKey: string;
2106
2133
  }
2134
+ type PushSubscriptionInput = {
2135
+ platform: PushPlatform;
2136
+ endpoint?: string;
2137
+ keys?: {
2138
+ p256dh: string;
2139
+ auth: string;
2140
+ };
2141
+ deviceToken?: string;
2142
+ };
2143
+ interface NotificationsModuleOptions {
2144
+ /** Scopes the remembered push registration to one project. */
2145
+ projectId?: string;
2146
+ }
2107
2147
  declare class NotificationsModule {
2108
2148
  private http;
2109
2149
  private customDeviceId;
2110
2150
  private customDeviceName;
2111
- constructor(http: HttpClient);
2151
+ private readonly registrationKey;
2152
+ private memoryRegistration;
2153
+ private pushQueue;
2154
+ constructor(http: HttpClient, options?: NotificationsModuleOptions);
2112
2155
  /** Set a custom device ID (e.g. from Capacitor Preferences for persistence beyond localStorage) */
2113
2156
  setDeviceId(id: string): void;
2114
2157
  /** Set a custom device name (e.g. "iOS App", "macOS - Chrome") */
@@ -2121,17 +2164,20 @@ declare class NotificationsModule {
2121
2164
  private detectDeviceName;
2122
2165
  /** Get the VAPID public key for Web Push setup */
2123
2166
  getVapidPublicKey(): Promise<VapidKeyResponse>;
2124
- /** Register a push subscription (web, android, ios, or web-fcm) */
2125
- subscribe(subscription: {
2126
- platform: PushPlatform;
2127
- endpoint?: string;
2128
- keys?: {
2129
- p256dh: string;
2130
- auth: string;
2131
- };
2132
- deviceToken?: string;
2133
- }, deviceName?: string): Promise<PushSubscriptionInfo>;
2134
- /** Unregister a push subscription */
2167
+ /**
2168
+ * Register a push subscription (web, android, ios, or web-fcm).
2169
+ *
2170
+ * The SDK remembers this device's registration. Unless the client was
2171
+ * created with `autoManagePush: false`, it re-registers it after every
2172
+ * login and unregisters it on `auth.logout()`, so the subscription always
2173
+ * belongs to the signed-in user's current session.
2174
+ */
2175
+ subscribe(subscription: PushSubscriptionInput, deviceName?: string): Promise<PushSubscriptionInfo>;
2176
+ private register;
2177
+ /**
2178
+ * Unregister a push subscription. Unregistering this device's own
2179
+ * subscription also stops the SDK from re-registering it after a login.
2180
+ */
2135
2181
  unsubscribe(platform: PushPlatform, identifier: string): Promise<{
2136
2182
  deleted: boolean;
2137
2183
  }>;
@@ -2144,6 +2190,30 @@ declare class NotificationsModule {
2144
2190
  unsubscribeById(subscriptionId: string): Promise<{
2145
2191
  deleted: boolean;
2146
2192
  }>;
2193
+ /**
2194
+ * Re-register this device's remembered subscription for the user who just
2195
+ * signed in, binding it to their new session (#1003). The client calls this
2196
+ * after every sign-in unless `autoManagePush` is false; call it yourself
2197
+ * after signing in via `setTokens()`. No-op when this device never
2198
+ * subscribed. Best-effort: a failure leaves the device without push until
2199
+ * the app subscribes again.
2200
+ */
2201
+ resubscribeAfterLogin(): Promise<void>;
2202
+ /**
2203
+ * Unregister this device's subscription while the session is still valid,
2204
+ * so a signed-out device stops receiving the user's pushes. The remembered
2205
+ * registration is kept, so the next sign-in re-registers it. `auth.logout()`
2206
+ * calls this first unless `autoManagePush` is false; call it yourself before
2207
+ * clearing tokens via `clearTokens()`. Best-effort.
2208
+ */
2209
+ unsubscribeBeforeLogout(): Promise<void>;
2210
+ private enqueue;
2211
+ private deleteSubscription;
2212
+ /** The value `unsubscribe()` identifies a subscription by: endpoint for web, token otherwise. */
2213
+ private static identifierOf;
2214
+ private static isStoredRegistration;
2215
+ private loadRegistration;
2216
+ private saveRegistration;
2147
2217
  /** Get all subscriptions for the current user */
2148
2218
  getSubscriptions(): Promise<PushSubscriptionInfo[]>;
2149
2219
  /**
package/dist/index.d.ts CHANGED
@@ -380,6 +380,14 @@ interface SpacelrClientConfig {
380
380
  tokenStorage?: TokenStorage;
381
381
  /** Buffer in seconds before token expiry to trigger auto-refresh (default: 60) */
382
382
  refreshBufferSeconds?: number;
383
+ /**
384
+ * Keep this device's push subscription in sync with the session (default:
385
+ * true). After every login the SDK re-registers the device's last push
386
+ * registration for the signed-in user; `auth.logout()` unregisters it first.
387
+ * Set to false to manage `notifications.subscribe()` / `unsubscribe()`
388
+ * entirely yourself.
389
+ */
390
+ autoManagePush?: boolean;
383
391
  }
384
392
 
385
393
  interface FileInfo {
@@ -1080,7 +1088,19 @@ declare class AuthModule {
1080
1088
  private stateListeners;
1081
1089
  private unsubscribeAuthLost;
1082
1090
  private lastEmittedState;
1083
- constructor(http: HttpClient, tokenManager: TokenManager, config: SpacelrClientConfig);
1091
+ private beforeLogout?;
1092
+ private afterSignIn?;
1093
+ constructor(http: HttpClient, tokenManager: TokenManager, config: SpacelrClientConfig,
1094
+ /**
1095
+ * Wired by `createClient`. `beforeLogout` runs while the session is still
1096
+ * valid during `logout()`; `afterSignIn` runs after EVERY sign-in, even
1097
+ * one that does not change the auth state, because each sign-in starts a
1098
+ * new session.
1099
+ */
1100
+ hooks?: {
1101
+ beforeLogout?: () => Promise<void>;
1102
+ afterSignIn?: () => Promise<void>;
1103
+ });
1084
1104
  /**
1085
1105
  * Returns true if a non-expired access token is currently in storage.
1086
1106
  * Does NOT make a network request — safe for route guards and other
@@ -1122,6 +1142,13 @@ declare class AuthModule {
1122
1142
  * onAuthLost subscription. Idempotent — safe to call more than once.
1123
1143
  */
1124
1144
  dispose(): void;
1145
+ /**
1146
+ * Install the tokens of a completed sign-in. Signing in over a live session
1147
+ * (e.g. user B over user A without a logout) first runs `beforeLogout` with
1148
+ * the outgoing session's still-valid token, so this device's push leaves
1149
+ * the previous user before it is re-registered for the new one.
1150
+ */
1151
+ private installSession;
1125
1152
  private emitState;
1126
1153
  login(params: LoginParams): Promise<LoginResponse>;
1127
1154
  register(params: RegisterParams): Promise<RegisterResponse>;
@@ -2104,11 +2131,27 @@ interface PushSubscriptionInfo {
2104
2131
  interface VapidKeyResponse {
2105
2132
  publicKey: string;
2106
2133
  }
2134
+ type PushSubscriptionInput = {
2135
+ platform: PushPlatform;
2136
+ endpoint?: string;
2137
+ keys?: {
2138
+ p256dh: string;
2139
+ auth: string;
2140
+ };
2141
+ deviceToken?: string;
2142
+ };
2143
+ interface NotificationsModuleOptions {
2144
+ /** Scopes the remembered push registration to one project. */
2145
+ projectId?: string;
2146
+ }
2107
2147
  declare class NotificationsModule {
2108
2148
  private http;
2109
2149
  private customDeviceId;
2110
2150
  private customDeviceName;
2111
- constructor(http: HttpClient);
2151
+ private readonly registrationKey;
2152
+ private memoryRegistration;
2153
+ private pushQueue;
2154
+ constructor(http: HttpClient, options?: NotificationsModuleOptions);
2112
2155
  /** Set a custom device ID (e.g. from Capacitor Preferences for persistence beyond localStorage) */
2113
2156
  setDeviceId(id: string): void;
2114
2157
  /** Set a custom device name (e.g. "iOS App", "macOS - Chrome") */
@@ -2121,17 +2164,20 @@ declare class NotificationsModule {
2121
2164
  private detectDeviceName;
2122
2165
  /** Get the VAPID public key for Web Push setup */
2123
2166
  getVapidPublicKey(): Promise<VapidKeyResponse>;
2124
- /** Register a push subscription (web, android, ios, or web-fcm) */
2125
- subscribe(subscription: {
2126
- platform: PushPlatform;
2127
- endpoint?: string;
2128
- keys?: {
2129
- p256dh: string;
2130
- auth: string;
2131
- };
2132
- deviceToken?: string;
2133
- }, deviceName?: string): Promise<PushSubscriptionInfo>;
2134
- /** Unregister a push subscription */
2167
+ /**
2168
+ * Register a push subscription (web, android, ios, or web-fcm).
2169
+ *
2170
+ * The SDK remembers this device's registration. Unless the client was
2171
+ * created with `autoManagePush: false`, it re-registers it after every
2172
+ * login and unregisters it on `auth.logout()`, so the subscription always
2173
+ * belongs to the signed-in user's current session.
2174
+ */
2175
+ subscribe(subscription: PushSubscriptionInput, deviceName?: string): Promise<PushSubscriptionInfo>;
2176
+ private register;
2177
+ /**
2178
+ * Unregister a push subscription. Unregistering this device's own
2179
+ * subscription also stops the SDK from re-registering it after a login.
2180
+ */
2135
2181
  unsubscribe(platform: PushPlatform, identifier: string): Promise<{
2136
2182
  deleted: boolean;
2137
2183
  }>;
@@ -2144,6 +2190,30 @@ declare class NotificationsModule {
2144
2190
  unsubscribeById(subscriptionId: string): Promise<{
2145
2191
  deleted: boolean;
2146
2192
  }>;
2193
+ /**
2194
+ * Re-register this device's remembered subscription for the user who just
2195
+ * signed in, binding it to their new session (#1003). The client calls this
2196
+ * after every sign-in unless `autoManagePush` is false; call it yourself
2197
+ * after signing in via `setTokens()`. No-op when this device never
2198
+ * subscribed. Best-effort: a failure leaves the device without push until
2199
+ * the app subscribes again.
2200
+ */
2201
+ resubscribeAfterLogin(): Promise<void>;
2202
+ /**
2203
+ * Unregister this device's subscription while the session is still valid,
2204
+ * so a signed-out device stops receiving the user's pushes. The remembered
2205
+ * registration is kept, so the next sign-in re-registers it. `auth.logout()`
2206
+ * calls this first unless `autoManagePush` is false; call it yourself before
2207
+ * clearing tokens via `clearTokens()`. Best-effort.
2208
+ */
2209
+ unsubscribeBeforeLogout(): Promise<void>;
2210
+ private enqueue;
2211
+ private deleteSubscription;
2212
+ /** The value `unsubscribe()` identifies a subscription by: endpoint for web, token otherwise. */
2213
+ private static identifierOf;
2214
+ private static isStoredRegistration;
2215
+ private loadRegistration;
2216
+ private saveRegistration;
2147
2217
  /** Get all subscriptions for the current user */
2148
2218
  getSubscriptions(): Promise<PushSubscriptionInfo[]>;
2149
2219
  /**