diff --git a/addons/dexie-cloud/package.json b/addons/dexie-cloud/package.json index 919dfff22..bf7eb0e0c 100644 --- a/addons/dexie-cloud/package.json +++ b/addons/dexie-cloud/package.json @@ -1,6 +1,6 @@ { "name": "dexie-cloud-addon", - "version": "4.0.7", + "version": "4.0.8", "description": "Dexie addon that syncs with to Dexie Cloud", "main": "dist/umd/dexie-cloud-addon.js", "type": "module", diff --git a/addons/dexie-cloud/src/DexieCloudOptions.ts b/addons/dexie-cloud/src/DexieCloudOptions.ts index 10c797661..370f6f462 100644 --- a/addons/dexie-cloud/src/DexieCloudOptions.ts +++ b/addons/dexie-cloud/src/DexieCloudOptions.ts @@ -1,5 +1,5 @@ -import Dexie, { Collection, Table } from 'dexie'; -import { TokenFinalResponse } from 'dexie-cloud-common'; +import type { TokenFinalResponse } from 'dexie-cloud-common'; +import type { LoginHints } from './DexieCloudAPI'; export interface PeriodicSyncOptions { // The minimum interval time, in milliseconds, at which the service-worker's @@ -11,7 +11,7 @@ export interface DexieCloudOptions { databaseUrl: string; // Whether to require authentication or opt-in to it using db.cloud.login() - requireAuth?: boolean; + requireAuth?: boolean | LoginHints // Whether to use service worker. Combine with registering your own service // worker and import "dexie-cloud-addon/dist/modern/service-worker.min.js" from it. diff --git a/addons/dexie-cloud/src/dexie-cloud-client.ts b/addons/dexie-cloud/src/dexie-cloud-client.ts index 1c75f4612..a7e82aae1 100644 --- a/addons/dexie-cloud/src/dexie-cloud-client.ts +++ b/addons/dexie-cloud/src/dexie-cloud-client.ts @@ -398,8 +398,20 @@ export function dexieCloud(dexie: Dexie) { // HERE: If requireAuth, do athentication now. let changedUser = false; const user = await db.getCurrentUser(); - if (db.cloud.options?.requireAuth) { - if (!user.isLoggedIn) { + const requireAuth = db.cloud.options?.requireAuth; + if (requireAuth) { + if (typeof requireAuth === 'object') { + // requireAuth contains login hints. Check if we already fulfil it: + if ( + !user.isLoggedIn || + (requireAuth.userId && user.userId !== requireAuth.userId) || + (requireAuth.email && user.email !== requireAuth.email) + ) { + // If not, login the configured user: + changedUser = await login(db, requireAuth); + } + } else if (!user.isLoggedIn) { + // requireAuth is true and user is not logged in changedUser = await login(db); } }