refactor: handle 401 errors in staff and user data loading with logout headers
This commit is contained in:
parent
e84c05d0a0
commit
f291290f55
@ -1,9 +1,11 @@
|
|||||||
import { Outlet, redirect } from 'react-router'
|
import { Outlet, redirect } from 'react-router'
|
||||||
|
import { XiorError } from 'xior'
|
||||||
|
|
||||||
import { getStaff } from '~/apis/admin/get-staff'
|
import { getStaff } from '~/apis/admin/get-staff'
|
||||||
import { AUTH_PAGES } from '~/configs/pages'
|
import { AUTH_PAGES } from '~/configs/pages'
|
||||||
import { AdminDefaultLayout } from '~/layouts/admin/default'
|
import { AdminDefaultLayout } from '~/layouts/admin/default'
|
||||||
import { handleCookie } from '~/libs/cookies'
|
import { handleCookie } from '~/libs/cookies'
|
||||||
|
import { setStaffLogoutHeaders } from '~/libs/logout-header.server'
|
||||||
|
|
||||||
import type { Route } from './+types/_admin.lg-admin'
|
import type { Route } from './+types/_admin.lg-admin'
|
||||||
|
|
||||||
@ -13,6 +15,19 @@ export const loader = async ({ request }: Route.LoaderArgs) => {
|
|||||||
const isAuthPage = AUTH_PAGES.includes(pathname)
|
const isAuthPage = AUTH_PAGES.includes(pathname)
|
||||||
let staffData
|
let staffData
|
||||||
|
|
||||||
|
if (staffToken) {
|
||||||
|
try {
|
||||||
|
const { data } = await getStaff({
|
||||||
|
accessToken: staffToken,
|
||||||
|
})
|
||||||
|
staffData = data
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof XiorError && error.response?.status === 401) {
|
||||||
|
setStaffLogoutHeaders()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!isAuthPage && !staffToken) {
|
if (!isAuthPage && !staffToken) {
|
||||||
throw redirect('/lg-admin/login')
|
throw redirect('/lg-admin/login')
|
||||||
}
|
}
|
||||||
@ -21,13 +36,6 @@ export const loader = async ({ request }: Route.LoaderArgs) => {
|
|||||||
throw redirect('/lg-admin')
|
throw redirect('/lg-admin')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (staffToken) {
|
|
||||||
const { data } = await getStaff({
|
|
||||||
accessToken: staffToken,
|
|
||||||
})
|
|
||||||
staffData = data
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
staffData,
|
staffData,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { Outlet } from 'react-router'
|
import { Outlet } from 'react-router'
|
||||||
|
import { XiorError } from 'xior'
|
||||||
|
|
||||||
import { getCategories } from '~/apis/common/get-categories'
|
import { getCategories } from '~/apis/common/get-categories'
|
||||||
import { getSubscriptions } from '~/apis/common/get-subscriptions'
|
import { getSubscriptions } from '~/apis/common/get-subscriptions'
|
||||||
@ -6,6 +7,7 @@ import { getUser } from '~/apis/news/get-user'
|
|||||||
import { NewsProvider } from '~/contexts/news'
|
import { NewsProvider } from '~/contexts/news'
|
||||||
import { NewsDefaultLayout } from '~/layouts/news/default'
|
import { NewsDefaultLayout } from '~/layouts/news/default'
|
||||||
import { handleCookie } from '~/libs/cookies'
|
import { handleCookie } from '~/libs/cookies'
|
||||||
|
import { setUserLogoutHeaders } from '~/libs/logout-header.server'
|
||||||
|
|
||||||
import type { Route } from './+types/_news'
|
import type { Route } from './+types/_news'
|
||||||
|
|
||||||
@ -13,10 +15,16 @@ export const loader = async ({ request }: Route.LoaderArgs) => {
|
|||||||
const { userToken } = await handleCookie(request)
|
const { userToken } = await handleCookie(request)
|
||||||
let userData
|
let userData
|
||||||
if (userToken) {
|
if (userToken) {
|
||||||
const { data } = await getUser({
|
try {
|
||||||
accessToken: userToken,
|
const { data } = await getUser({
|
||||||
})
|
accessToken: userToken,
|
||||||
userData = data
|
})
|
||||||
|
userData = data
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof XiorError && error.response?.status === 401) {
|
||||||
|
setUserLogoutHeaders()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const { data: subscriptionsData } = await getSubscriptions()
|
const { data: subscriptionsData } = await getSubscriptions()
|
||||||
const { data: categoriesData } = await getCategories()
|
const { data: categoriesData } = await getCategories()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user