mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-08 04:36:13 +08:00
fix: revalidate user token with no refresh page (#1842)
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
import {
|
||||
workspaceDetailSchema,
|
||||
workspaceSchema,
|
||||
} from '@affine/workspace/affine/api';
|
||||
import { WebsocketClient } from '@affine/workspace/affine/channel';
|
||||
import { storageChangeSlot } from '@affine/workspace/affine/login';
|
||||
import { jotaiStore, jotaiWorkspacesAtom } from '@affine/workspace/atom';
|
||||
import type { WorkspaceCRUD } from '@affine/workspace/type';
|
||||
import type { WorkspaceFlavour } from '@affine/workspace/type';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import type { Disposable } from '@blocksuite/store';
|
||||
import { z } from 'zod';
|
||||
|
||||
const logger = new DebugLogger('affine-sync');
|
||||
|
||||
const channelMessageSchema = z.object({
|
||||
ws_list: z.array(workspaceSchema),
|
||||
ws_details: z.record(workspaceDetailSchema),
|
||||
@@ -28,7 +33,7 @@ export function createAffineGlobalChannel(
|
||||
let client: WebsocketClient | null;
|
||||
|
||||
async function handleMessage(channelMessage: ChannelMessage) {
|
||||
console.log('channelMessage', channelMessage);
|
||||
logger.debug('channelMessage', channelMessage);
|
||||
const parseResult = channelMessageSchema.safeParse(channelMessage);
|
||||
if (!parseResult.success) {
|
||||
console.error(
|
||||
@@ -53,8 +58,8 @@ export function createAffineGlobalChannel(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
let dispose: Disposable | undefined = undefined;
|
||||
const apis = {
|
||||
connect: () => {
|
||||
client = new WebsocketClient(
|
||||
`${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${
|
||||
@@ -62,11 +67,18 @@ export function createAffineGlobalChannel(
|
||||
}/api/global/sync`
|
||||
);
|
||||
client.connect(handleMessage);
|
||||
dispose = storageChangeSlot.on(() => {
|
||||
apis.disconnect();
|
||||
apis.connect();
|
||||
});
|
||||
},
|
||||
disconnect: () => {
|
||||
assertExists(client, 'client is null');
|
||||
client.disconnect();
|
||||
dispose?.dispose();
|
||||
client = null;
|
||||
},
|
||||
};
|
||||
|
||||
return apis;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user