Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requireAuth with options #2006

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading