Skip to content

Commit

Permalink
Merge branch 'dxc-optimize-cold-start'
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed Apr 29, 2024
2 parents b2bc4c8 + 16ea904 commit 46ec39b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
21 changes: 16 additions & 5 deletions addons/dexie-cloud/src/authentication/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ export async function login(
const origUserId = currentUser.userId;
if (currentUser.isLoggedIn && (!hints || (!hints.email && !hints.userId))) {
const licenseStatus = currentUser.license?.status || 'ok';
if (licenseStatus === 'ok' && currentUser.accessToken && (!currentUser.accessTokenExpiration || currentUser.accessTokenExpiration.getTime() > Date.now())) {
if (
licenseStatus === 'ok' &&
currentUser.accessToken &&
(!currentUser.accessTokenExpiration ||
currentUser.accessTokenExpiration.getTime() > Date.now())
) {
// Already authenticated according to given hints. And license is valid.
return false;
}
if (currentUser.refreshToken && (!currentUser.refreshTokenExpiration || currentUser.refreshTokenExpiration.getTime() > Date.now())) {
if (
currentUser.refreshToken &&
(!currentUser.refreshTokenExpiration ||
currentUser.refreshTokenExpiration.getTime() > Date.now())
) {
// Refresh the token
await loadAccessToken(db);
return false;
Expand All @@ -38,7 +47,10 @@ export async function login(
db.cloud.userInteraction,
hints
);
if (origUserId !== UNAUTHORIZED_USER.userId && context.userId !== origUserId) {
if (
origUserId !== UNAUTHORIZED_USER.userId &&
context.userId !== origUserId
) {
// User was logged in before, but now logged in as another user.
await logout(db);
}
Expand All @@ -58,7 +70,6 @@ export async function login(
await setCurrentUser(db, context);
// Make sure to resync as the new login will be authorized
// for new realms.
triggerSync(db, "pull");
triggerSync(db, 'pull');
return context.userId !== origUserId;
}

5 changes: 4 additions & 1 deletion addons/dexie-cloud/src/dexie-cloud-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@ export function dexieCloud(dexie: Dexie) {
// HERE: If requireAuth, do athentication now.
let changedUser = false;
if (db.cloud.options?.requireAuth) {
changedUser = await login(db);
const user = await db.getCurrentUser();
if (!user.isLoggedIn) {
changedUser = await login(db);
}
}

if (localSyncWorker) localSyncWorker.stop();
Expand Down

0 comments on commit 46ec39b

Please sign in to comment.