fix(core): avoid infinite sign in with selfhost (#13169)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* The 404 page now reflects user session state across multiple servers,
showing the appropriate user context when multiple accounts are logged
in.

* **Improvements**
* Enhanced user experience on the 404 page by accurately displaying
information based on the first active logged-in account across all
servers.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
EYHN
2025-07-11 17:46:11 +08:00
committed by GitHub
parent 9cda655c9e
commit 0e8ffce126
2 changed files with 38 additions and 16 deletions

View File

@@ -5,6 +5,7 @@ import {
import { useSignOut } from '@affine/core/components/hooks/affine/use-sign-out';
import { DesktopApiService } from '@affine/core/modules/desktop-api';
import {
FrameworkScope,
useLiveData,
useService,
useServiceOptional,
@@ -16,7 +17,7 @@ import {
RouteLogic,
useNavigateHelper,
} from '../../../components/hooks/use-navigate-helper';
import { AuthService } from '../../../modules/cloud';
import { ServersService } from '../../../modules/cloud';
import { SignIn } from '../auth/sign-in';
/**
@@ -27,9 +28,15 @@ export const PageNotFound = ({
}: {
noPermission?: boolean;
}): ReactElement => {
const authService = useService(AuthService);
const serversService = useService(ServersService);
const serversWithAccount = useLiveData(serversService.serversWithAccount$);
const desktopApi = useServiceOptional(DesktopApiService);
const account = useLiveData(authService.session.account$);
// Check all servers for any logged in accounts to avoid showing sign-in page if user has an active session on any server
const firstLogged = serversWithAccount.find(
({ account }) => account !== null
);
const { jumpToIndex } = useNavigateHelper();
const openSignOutModal = useSignOut();
@@ -46,19 +53,23 @@ export const PageNotFound = ({
// strip the origin
const currentUrl = window.location.href.replace(window.location.origin, '');
return noPermission ? (
<NoPermissionOrNotFound
user={account}
onBack={handleBackButtonClick}
onSignOut={openSignOutModal}
signInComponent={<SignIn redirectUrl={currentUrl} />}
/>
) : (
<NotFoundPage
user={account}
onBack={handleBackButtonClick}
onSignOut={openSignOutModal}
/>
return (
<FrameworkScope scope={firstLogged?.server.scope}>
{noPermission ? (
<NoPermissionOrNotFound
user={firstLogged?.account}
onBack={handleBackButtonClick}
onSignOut={openSignOutModal}
signInComponent={<SignIn redirectUrl={currentUrl} />}
/>
) : (
<NotFoundPage
user={firstLogged?.account}
onBack={handleBackButtonClick}
onSignOut={openSignOutModal}
/>
)}
</FrameworkScope>
);
};

View File

@@ -47,6 +47,17 @@ export class ServersService extends Service {
[] as any
);
serversWithAccount$ = this.servers$
.map(servers =>
servers.map(server =>
server.account$.map(account => ({
server,
account,
}))
)
)
.flat();
server$(id: string) {
return this.servers$.map(servers =>
servers.find(server => server.id === id)