From 77657a697b83dbccf7ea3b29a620db4ebe91bed9 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Thu, 9 Apr 2026 11:35:15 +0800 Subject: [PATCH] feat(mobile): improve notify for login failed --- packages/frontend/apps/android/src/app.tsx | 41 +++++++++++++++++--- packages/frontend/apps/ios/src/app.tsx | 45 +++++++++++++++++++--- 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/packages/frontend/apps/android/src/app.tsx b/packages/frontend/apps/android/src/app.tsx index 2ea7b22344..b0f05cfd66 100644 --- a/packages/frontend/apps/android/src/app.tsx +++ b/packages/frontend/apps/android/src/app.tsx @@ -1,3 +1,4 @@ +import { notify } from '@affine/component'; import { getStoreManager } from '@affine/core/blocksuite/manager/store'; import { AffineContext } from '@affine/core/components/context'; import { AppFallback } from '@affine/core/mobile/components/app-fallback'; @@ -295,6 +296,24 @@ window.addEventListener('focus', () => { }); frameworkProvider.get(LifecycleService).applicationStart(); +const getErrorMessage = (error: unknown, fallback: string) => { + if (typeof error === 'string' && error) { + return error; + } + if (error instanceof Error && error.message) { + return error.message; + } + return fallback; +}; + +const notifyAuthenticationError = (error: unknown, fallback: string) => { + console.error(fallback, error); + notify.error({ + title: I18n['com.affine.auth.toast.title.failed'](), + message: getErrorMessage(error, fallback), + }); +}; + CapacitorApp.addListener('appUrlOpen', ({ url }) => { // try to close browser if it's open InAppBrowser.close().catch(e => console.error('Failed to close browser', e)); @@ -311,7 +330,10 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => { (method !== 'magic-link' && method !== 'oauth') || !payload ) { - console.error('Invalid authentication url', url); + notifyAuthenticationError( + new Error('Invalid authentication url'), + 'Invalid authentication url' + ); return; } @@ -323,7 +345,12 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => { const serversService = frameworkProvider.get(ServersService); const server = serversService.getServerByBaseUrl(serverBaseUrl); if (!server) { - console.error('Authentication callback server not found', serverBaseUrl); + notifyAuthenticationError( + new Error( + `Authentication callback server not found: ${serverBaseUrl}` + ), + 'Authentication callback server not found' + ); return; } authService = server.scope.get(AuthService); @@ -332,15 +359,19 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => { if (method === 'oauth') { authService .signInOauth(payload.code, payload.state, payload.provider) - .catch(console.error); + .catch(error => + notifyAuthenticationError(error, 'Failed to sign in with OAuth') + ); } else if (method === 'magic-link') { authService .signInMagicLink(payload.email, payload.token) - .catch(console.error); + .catch(error => + notifyAuthenticationError(error, 'Failed to sign in with magic link') + ); } } }).catch(e => { - console.error(e); + notifyAuthenticationError(e, 'Failed to handle authentication callback'); }); const ThemeProvider = () => { diff --git a/packages/frontend/apps/ios/src/app.tsx b/packages/frontend/apps/ios/src/app.tsx index 5a6e4068b5..eabcb42953 100644 --- a/packages/frontend/apps/ios/src/app.tsx +++ b/packages/frontend/apps/ios/src/app.tsx @@ -1,3 +1,4 @@ +import { notify } from '@affine/component'; import { getStoreManager } from '@affine/core/blocksuite/manager/store'; import { AffineContext } from '@affine/core/components/context'; import { AppFallback } from '@affine/core/mobile/components/app-fallback'; @@ -408,6 +409,24 @@ window.addEventListener('focus', () => { }); frameworkProvider.get(LifecycleService).applicationStart(); +const getErrorMessage = (error: unknown, fallback: string) => { + if (typeof error === 'string' && error) { + return error; + } + if (error instanceof Error && error.message) { + return error.message; + } + return fallback; +}; + +const notifyAuthenticationError = (error: unknown, fallback: string) => { + console.error(fallback, error); + notify.error({ + title: I18n['com.affine.auth.toast.title.failed'](), + message: getErrorMessage(error, fallback), + }); +}; + CapacitorApp.addListener('appUrlOpen', ({ url }) => { // try to close browser if it's open Browser.close().catch(e => console.error('Failed to close browser', e)); @@ -424,7 +443,10 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => { (method !== 'magic-link' && method !== 'oauth') || !payload ) { - console.error('Invalid authentication url', url); + notifyAuthenticationError( + new Error('Invalid authentication url'), + 'Invalid authentication url' + ); return; } @@ -435,23 +457,34 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => { if (serverBaseUrl) { const serversService = frameworkProvider.get(ServersService); const server = serversService.getServerByBaseUrl(serverBaseUrl); - if (server) { - authService = server.scope.get(AuthService); + if (!server) { + notifyAuthenticationError( + new Error( + `Authentication callback server not found: ${serverBaseUrl}` + ), + 'Authentication callback server not found' + ); + return; } + authService = server.scope.get(AuthService); } if (method === 'oauth') { authService .signInOauth(payload.code, payload.state, payload.provider) - .catch(console.error); + .catch(error => + notifyAuthenticationError(error, 'Failed to sign in with OAuth') + ); } else if (method === 'magic-link') { authService .signInMagicLink(payload.email, payload.token) - .catch(console.error); + .catch(error => + notifyAuthenticationError(error, 'Failed to sign in with magic link') + ); } } }).catch(e => { - console.error(e); + notifyAuthenticationError(e, 'Failed to handle authentication callback'); }); AppTrackingTransparency.requestPermission().catch(e => {