跳到主要内容

LogoutManager

根据请求响应执行登出。默认情况下,由 401 (Unauthorized) 状态的响应触发。

实现

LogoutManager 实现了 Manager

用法​

index.tsx
import {
DataProvider,
LogoutManager,
getDefaultManagers,
} from '@data-client/react';
import { createRoot } from 'react-dom/client';
import App from './App';

const managers = [new LogoutManager(), ...getDefaultManagers()];

createRoot(document.body).render(
<DataProvider managers={managers}>
<App />
</DataProvider>,
);

自定义登出处理函数​

import { unAuth } from '../authentication';

const managers = [
new LogoutManager({
handleLogout(controller) {
// call custom unAuth function we defined
unAuth();
// still reset the store
controller.resetEntireStore();
},
}),
...getDefaultManagers(),
];
提示

使用 controller.invalidateAll 只清除部分缓存。

import { unAuth } from '../authentication';

const myDomain = 'http://test.com';
const testKey = (key: string) => key.startsWith(`GET ${myDomain}`);

const managers = [
new LogoutManager({
handleLogout(controller) {
// call custom unAuth function we defined
unAuth();
// still reset the store
controller.invalidateAll({ testKey });
},
}),
...getDefaultManagers(),
];

成员​

handleLogout(controller)​

默认情况下只是调用 controller.resetEntireStore()

如果登录状态由 Reactive Data Client store 中是否存在用户 Entity 决定,这样就足够了。不过,如果需要做更多事情,你可以通过继承来重写这个方法。

shouldLogout(error)​

protected shouldLogout(error: UnknownError) {
// 401 indicates reauthorization is needed
return error.status === 401;
}

Github 示例​

探索 github-app 示例

更多演示