fix: fix logout + fix protected route

This commit is contained in:
2026-05-10 18:52:24 +03:00
parent 01685e3811
commit 574e27a379
3 changed files with 26 additions and 6 deletions

View File

@@ -0,0 +1,14 @@
import { Navigate, Outlet, useLocation } from 'react-router-dom'
import { useIsAuthenticated } from '@features/auth'
import { ROUTES } from '@shared/config/routes'
export function ProtectedRoute() {
const isAuthenticated = useIsAuthenticated()
const location = useLocation()
if (!isAuthenticated) {
return <Navigate to={ROUTES.LOGIN} state={{ from: location }} replace />
}
return <Outlet />
}

View File

@@ -8,6 +8,7 @@ import { RegisterPage } from '@pages/register'
import { SeedPhrasePage } from '@pages/seed-phrase'
import { ROUTES } from '@shared/config/routes'
import { ScrollToTop } from './ScrollToTop'
import { ProtectedRoute } from './ProtectedRoute'
export function RouterProvider() {
return (
@@ -15,12 +16,15 @@ export function RouterProvider() {
<ScrollToTop />
<Routes>
<Route path={ROUTES.HOME} element={<HomePage />} />
<Route path={ROUTES.LOGIN} element={<LoginPage />} />
<Route path={ROUTES.REGISTER} element={<RegisterPage />} />
<Route element={<ProtectedRoute />}>
<Route path={ROUTES.WALLET} element={<WalletPage />} />
<Route path={ROUTES.SWAP} element={<SwapPage />} />
<Route path={ROUTES.PROFILE} element={<ProfilePage />} />
<Route path={ROUTES.LOGIN} element={<LoginPage />} />
<Route path={ROUTES.REGISTER} element={<RegisterPage />} />
<Route path={ROUTES.SEED_PHRASE} element={<SeedPhrasePage />} />
</Route>
</Routes>
</BrowserRouter>
)

View File

@@ -1,4 +1,5 @@
import { api } from '@shared/api/base'
import { getCsrfToken } from '@shared/api/csrf'
export interface RegistrationStartPayload {
email: string
@@ -19,6 +20,7 @@ export function registrationComplete(payload: RegistrationCompletePayload): Prom
return api.post<string>('/auth/registration/complete', payload)
}
export function logout(): Promise<void> {
return api.post<void>('/logout', {})
export async function logout(): Promise<void> {
const csrfToken = await getCsrfToken()
return api.post<void>('/logout', { _csrf: csrfToken })
}