Skip to content

Commit

Permalink
fix: login 兼容不同的 request 版本 (#242)
Browse files Browse the repository at this point in the history
* fix: login 兼容不同的 request 版本

* chore: upgrade request
  • Loading branch information
winixt authored May 6, 2024
1 parent 1b0d340 commit c7e7efa
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
81 changes: 55 additions & 26 deletions packages/fes-plugin-login/src/runtime/runtime.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { request } from '@@/core/pluginExports';
import { ApplyPluginsType, getRouter, plugin } from '@fesjs/fes';

let config;
function getLoginConfig() {
if (config) return config;
if (config) {
return config;
}

config = plugin.applyPlugins({
key: 'login',
type: ApplyPluginsType.modify,
Expand All @@ -14,38 +18,63 @@ function getLoginConfig() {
return config;
}

const defaultExport = {
onRouterCreated({ router }) {
const { hasLogin, loginPath } = getLoginConfig();
if (hasLogin && loginPath) {
let isAuthenticated;
router.beforeEach(async (to, from, next) => {
if (to.path !== loginPath && !isAuthenticated) {
isAuthenticated = await hasLogin();
if (!isAuthenticated) {
return next({ path: loginPath });
}
}
next();
});
}
},
};

// ACCESS

export function request(memo) {
if (!memo.responseInterceptors) {
memo.responseInterceptors = [];
}
memo.responseInterceptors.push([
(response) => response,
(error) => {
if (request.version) {
defaultExport.request = (memo) => {
const config = getRunTimeConfig();
if (config.ignore401Redirect) {
return memo;
}

const errorHandler = memo.errorHandler;
memo.errorHandler = (error) => {
if (error?.response?.status === 401) {
const router = getRouter();
const { loginPath } = getLoginConfig();
router.push({ path: loginPath });
}
throw error;
},
]);
return memo;
errorHandler && errorHandler(error);
};
return memo;
};
}

export function onRouterCreated({ router }) {
const { hasLogin, loginPath } = getLoginConfig();
if (hasLogin && loginPath) {
let isAuthenticated;
router.beforeEach(async (to, from, next) => {
if (to.path !== loginPath && !isAuthenticated) {
isAuthenticated = await hasLogin();
if (!isAuthenticated) {
return next({ path: loginPath });
else {
defaultExport.request = (memo) => {
if (!memo.responseInterceptors) {
memo.responseInterceptors = [];
}
memo.responseInterceptors.push([
response => response,
(error) => {
if (error?.response?.status === 401) {
const router = getRouter();
const { loginPath } = getLoginConfig();
router.push({ path: loginPath });
}
}
next();
});
}
throw error;
},
]);
return memo;
};
}

export default defaultExport;
2 changes: 1 addition & 1 deletion packages/fes-plugin-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"dependencies": {
"@fesjs/utils": "^3.0.1",
"@qlin/request": "^0.2.0"
"@qlin/request": "^0.2.3"
},
"typings": "./types.d.ts"
}
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c7e7efa

Please sign in to comment.