Copilot commented on code in PR #1373:
URL: https://github.com/apache/dubbo-admin/pull/1373#discussion_r2637139508
##########
ui-vue3/src/base/http/request.ts:
##########
@@ -82,8 +81,30 @@ response.use(
(error) => {
NProgress.done()
// Handle error response with data
- if (error.response?.data) {
- const errorMsg =
`${error.response.data.code}:${error.response.data.message}`
+ const response = error.response
+
+ // Handle 401 unauthorized
+ if (response?.status === 401) {
+ removeAuthState()
+ try {
+ const current = router.currentRoute?.value
+ const redirectPath = current?.fullPath || current?.path || '/'
+
+ if (!redirectPath.startsWith('/login')) {
+ router.push({ path:
`/login?redirect=${encodeURIComponent(redirectPath)}` })
+ }
Review Comment:
Potential infinite redirect loop issue. The code checks if the current path
starts with '/login' to avoid redirecting to login when already on the login
page. However, this doesn't prevent the scenario where a user on the login page
makes an API call that returns 401, which could still trigger redirect logic.
Consider also checking if the target redirect path is different from the
current path before calling router.push to prevent unnecessary navigation
attempts.
##########
ui-vue3/src/base/http/request.ts:
##########
@@ -82,8 +81,30 @@ response.use(
(error) => {
NProgress.done()
// Handle error response with data
- if (error.response?.data) {
- const errorMsg =
`${error.response.data.code}:${error.response.data.message}`
+ const response = error.response
+
+ // Handle 401 unauthorized
+ if (response?.status === 401) {
+ removeAuthState()
+ try {
+ const current = router.currentRoute?.value
+ const redirectPath = current?.fullPath || current?.path || '/'
+
+ if (!redirectPath.startsWith('/login')) {
+ router.push({ path:
`/login?redirect=${encodeURIComponent(redirectPath)}` })
+ }
+ } catch (e) {
+ console.error('Router push failed during 401 redirect:', e)
+ if (!window.location.pathname.startsWith('/login')) {
+ window.location.href =
`/login?redirect=${encodeURIComponent(window.location.pathname)}`
+ }
+ }
+ }
+ if (response?.status === 401) {
+ return Promise.reject(error.response?.data)
+ }
Review Comment:
Duplicate 401 status check detected. This condition at lines 103-105 is
redundant since the same check is already performed at lines 87-102. The second
check will never be reached for 401 errors because the first check already
handles it and rejects the promise. Remove this duplicate conditional block to
avoid confusion and dead code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]