Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(mobile): close the remaining account-switch and sign-in races

Tind91745eabd79
diff --git a/apps/mobile/src/app/sign-in.tsx b/apps/mobile/src/app/sign-in.tsx
index 726c1b168..533a0fd5a 100644
--- a/apps/mobile/src/app/sign-in.tsx
+++ b/apps/mobile/src/app/sign-in.tsx
@@ -88,9 +88,14 @@ export default function SignInScreen() {
const connect = (raw: string) =>
run(async () => {
const probe = ++probeRef.current;
+ const stale = () =>
+ probeRef.current !== probe || modeRef.current !== "self";
const url = normalizeServerUrl(raw);
- const cfg = await auth.probe(url);
- if (probeRef.current !== probe || modeRef.current !== "self") return;
+ const cfg = await auth.probe(url).catch((e: unknown) => {
+ if (stale()) return null;
+ throw e;
+ });
+ if (!cfg || stale()) return;
setServerUrl(url);
setConfig(cfg);
});
@@ -695,7 +700,12 @@ function DeviceStep({
if (cancelled) return;
if (result.kind === "approved") {
if (Platform.OS === "ios") WebBrowser.dismissAuthSession();
- await auth.completeWithToken(serverUrl, result.token);
+ await auth.completeWithToken(
+ serverUrl,
+ result.token,
+ "bearer",
+ () => !cancelled,
+ );
return;
}
if (result.kind === "slowDown") interval += 5;
diff --git a/apps/mobile/src/lib/auth.ts b/apps/mobile/src/lib/auth.ts
index b3d62688e..a52ddf12c 100644
--- a/apps/mobile/src/lib/auth.ts
+++ b/apps/mobile/src/lib/auth.ts
@@ -164,6 +164,8 @@ export async function completeWithToken(
serverUrl: string,
token: string,
type: CredentialType = "bearer",
+ /** Checked before saving, so a sign-in the person backed out of doesn't complete. */
+ stillWanted: () => boolean = () => true,
) {
let user: CurrentUser;
try {
@@ -182,6 +184,7 @@ export async function completeWithToken(
}
throw e;
}
+ if (!stillWanted()) return;
await sessionStore.save({
serverUrl,
credentialType: type,
diff --git a/apps/mobile/src/lib/background.ts b/apps/mobile/src/lib/background.ts
index e2ad88e89..e4b6a8ca9 100644
--- a/apps/mobile/src/lib/background.ts
+++ b/apps/mobile/src/lib/background.ts
@@ -28,7 +28,15 @@ export async function checkNotifications(): Promise<void> {
const since = Number((await AsyncStorage.getItem(SINCE_KEY)) ?? 0);
const notifications = await api.notifications();
// Signing out or switching accounts mid-check must not surface the old account's alerts.
- if (sessionStore.get().session?.token !== session.token) return;
+ const sameAccount = () => sessionStore.get().session?.token === session.token;
+ if (!sameAccount()) return;
+ const post = async (content: Notifications.NotificationContentInput) => {
+ if (!sameAccount()) return;
+ await Notifications.scheduleNotificationAsync({
+ content,
+ trigger: Platform.OS === "android" ? { channelId: CHANNEL_ID } : null,
+ });
+ };
const unread = notifications
.filter(
(n) =>
@@ -42,26 +50,20 @@ export async function checkNotifications(): Promise<void> {
const hidden = Math.max(0, unread.length - (MAX_POSTED - 1));
const shown = hidden > 1 ? unread.slice(-(MAX_POSTED - 1)) : unread;
if (hidden > 1)
- await Notifications.scheduleNotificationAsync({
- content: {
- title: "Kaneo",
- body: i18n.t("mobile:background.summary", { count: hidden }),
- data: {},
- },
- trigger: Platform.OS === "android" ? { channelId: CHANNEL_ID } : null,
+ await post({
+ title: "Kaneo",
+ body: i18n.t("mobile:background.summary", { count: hidden }),
+ data: {},
});
for (const n of shown) {
const text = describeNotification(n);
- await Notifications.scheduleNotificationAsync({
- content: {
- title: text.title,
- body: text.body ?? undefined,
- data:
- n.resourceType === "task" && n.resourceId
- ? { taskId: n.resourceId }
- : {},
- },
- trigger: Platform.OS === "android" ? { channelId: CHANNEL_ID } : null,
+ await post({
+ title: text.title,
+ body: text.body ?? undefined,
+ data:
+ n.resourceType === "task" && n.resourceId
+ ? { taskId: n.resourceId }
+ : {},
});
}
}