Skip to content

Commit

Permalink
requireAuth with options (#2006)
Browse files Browse the repository at this point in the history
* Allow options in requireauth property

* Release testversion 4.0.8-rc.1 with login options

* Avoid "-rc.1" in version number as it updated the lock file in a strange way
  • Loading branch information
dfahlander authored Jun 6, 2024
1 parent d8d8527 commit 795f60c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion addons/dexie-cloud/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions addons/dexie-cloud/src/DexieCloudOptions.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions addons/dexie-cloud/src/dexie-cloud-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 795f60c

Please sign in to comment.