feat(mobile): improve notify for login failed

This commit is contained in:
DarkSky
2026-04-09 11:35:15 +08:00
parent eb953c0565
commit 77657a697b
2 changed files with 75 additions and 11 deletions
+36 -5
View File
@@ -1,3 +1,4 @@
import { notify } from '@affine/component';
import { getStoreManager } from '@affine/core/blocksuite/manager/store'; import { getStoreManager } from '@affine/core/blocksuite/manager/store';
import { AffineContext } from '@affine/core/components/context'; import { AffineContext } from '@affine/core/components/context';
import { AppFallback } from '@affine/core/mobile/components/app-fallback'; import { AppFallback } from '@affine/core/mobile/components/app-fallback';
@@ -295,6 +296,24 @@ window.addEventListener('focus', () => {
}); });
frameworkProvider.get(LifecycleService).applicationStart(); 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 }) => { CapacitorApp.addListener('appUrlOpen', ({ url }) => {
// try to close browser if it's open // try to close browser if it's open
InAppBrowser.close().catch(e => console.error('Failed to close browser', e)); InAppBrowser.close().catch(e => console.error('Failed to close browser', e));
@@ -311,7 +330,10 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => {
(method !== 'magic-link' && method !== 'oauth') || (method !== 'magic-link' && method !== 'oauth') ||
!payload !payload
) { ) {
console.error('Invalid authentication url', url); notifyAuthenticationError(
new Error('Invalid authentication url'),
'Invalid authentication url'
);
return; return;
} }
@@ -323,7 +345,12 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => {
const serversService = frameworkProvider.get(ServersService); const serversService = frameworkProvider.get(ServersService);
const server = serversService.getServerByBaseUrl(serverBaseUrl); const server = serversService.getServerByBaseUrl(serverBaseUrl);
if (!server) { 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; return;
} }
authService = server.scope.get(AuthService); authService = server.scope.get(AuthService);
@@ -332,15 +359,19 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => {
if (method === 'oauth') { if (method === 'oauth') {
authService authService
.signInOauth(payload.code, payload.state, payload.provider) .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') { } else if (method === 'magic-link') {
authService authService
.signInMagicLink(payload.email, payload.token) .signInMagicLink(payload.email, payload.token)
.catch(console.error); .catch(error =>
notifyAuthenticationError(error, 'Failed to sign in with magic link')
);
} }
} }
}).catch(e => { }).catch(e => {
console.error(e); notifyAuthenticationError(e, 'Failed to handle authentication callback');
}); });
const ThemeProvider = () => { const ThemeProvider = () => {
+39 -6
View File
@@ -1,3 +1,4 @@
import { notify } from '@affine/component';
import { getStoreManager } from '@affine/core/blocksuite/manager/store'; import { getStoreManager } from '@affine/core/blocksuite/manager/store';
import { AffineContext } from '@affine/core/components/context'; import { AffineContext } from '@affine/core/components/context';
import { AppFallback } from '@affine/core/mobile/components/app-fallback'; import { AppFallback } from '@affine/core/mobile/components/app-fallback';
@@ -408,6 +409,24 @@ window.addEventListener('focus', () => {
}); });
frameworkProvider.get(LifecycleService).applicationStart(); 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 }) => { CapacitorApp.addListener('appUrlOpen', ({ url }) => {
// try to close browser if it's open // try to close browser if it's open
Browser.close().catch(e => console.error('Failed to close browser', e)); Browser.close().catch(e => console.error('Failed to close browser', e));
@@ -424,7 +443,10 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => {
(method !== 'magic-link' && method !== 'oauth') || (method !== 'magic-link' && method !== 'oauth') ||
!payload !payload
) { ) {
console.error('Invalid authentication url', url); notifyAuthenticationError(
new Error('Invalid authentication url'),
'Invalid authentication url'
);
return; return;
} }
@@ -435,23 +457,34 @@ CapacitorApp.addListener('appUrlOpen', ({ url }) => {
if (serverBaseUrl) { if (serverBaseUrl) {
const serversService = frameworkProvider.get(ServersService); const serversService = frameworkProvider.get(ServersService);
const server = serversService.getServerByBaseUrl(serverBaseUrl); const server = serversService.getServerByBaseUrl(serverBaseUrl);
if (server) { if (!server) {
authService = server.scope.get(AuthService); notifyAuthenticationError(
new Error(
`Authentication callback server not found: ${serverBaseUrl}`
),
'Authentication callback server not found'
);
return;
} }
authService = server.scope.get(AuthService);
} }
if (method === 'oauth') { if (method === 'oauth') {
authService authService
.signInOauth(payload.code, payload.state, payload.provider) .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') { } else if (method === 'magic-link') {
authService authService
.signInMagicLink(payload.email, payload.token) .signInMagicLink(payload.email, payload.token)
.catch(console.error); .catch(error =>
notifyAuthenticationError(error, 'Failed to sign in with magic link')
);
} }
} }
}).catch(e => { }).catch(e => {
console.error(e); notifyAuthenticationError(e, 'Failed to handle authentication callback');
}); });
AppTrackingTransparency.requestPermission().catch(e => { AppTrackingTransparency.requestPermission().catch(e => {