Private
Public Access
1
0

fix: persist auth state across page refreshes using onRehydrateStorage
Some checks failed
CI Pipeline / Rust Format Check (push) Failing after 5s
CI Pipeline / Clippy Lints (push) Successful in 49s
CI Pipeline / Rust Unit Tests (push) Successful in 1m2s
CI Pipeline / Security Audit (push) Successful in 4s
CI Pipeline / Frontend Lint & Type Check (push) Failing after 10s
CI Pipeline / Build .deb & Release (push) Has been skipped

This commit is contained in:
2026-05-07 02:59:09 +00:00
parent 5e63245f65
commit 73df591cd3
3 changed files with 51 additions and 8 deletions

14
Cargo.lock generated
View File

@ -2206,7 +2206,7 @@ dependencies = [
[[package]] [[package]]
name = "pm-agent-client" name = "pm-agent-client"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",
@ -2223,7 +2223,7 @@ dependencies = [
[[package]] [[package]]
name = "pm-auth" name = "pm-auth"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"argon2", "argon2",
@ -2250,7 +2250,7 @@ dependencies = [
[[package]] [[package]]
name = "pm-ca" name = "pm-ca"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",
@ -2273,7 +2273,7 @@ dependencies = [
[[package]] [[package]]
name = "pm-core" name = "pm-core"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"aes-gcm", "aes-gcm",
"anyhow", "anyhow",
@ -2297,7 +2297,7 @@ dependencies = [
[[package]] [[package]]
name = "pm-reports" name = "pm-reports"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",
@ -2318,7 +2318,7 @@ dependencies = [
[[package]] [[package]]
name = "pm-web" name = "pm-web"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"axum", "axum",
@ -2354,7 +2354,7 @@ dependencies = [
[[package]] [[package]]
name = "pm-worker" name = "pm-worker"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",

View File

@ -2,6 +2,7 @@ import { Routes, Route, Navigate } from 'react-router-dom'
import { CssBaseline, ThemeProvider } from '@mui/material' import { CssBaseline, ThemeProvider } from '@mui/material'
import { darkTheme } from './theme/theme' import { darkTheme } from './theme/theme'
import { useAuthStore } from './store/authStore' import { useAuthStore } from './store/authStore'
import { CircularProgress, Box } from '@mui/material'
import AppLayout from './components/AppLayout' import AppLayout from './components/AppLayout'
import LoginPage from './pages/LoginPage' import LoginPage from './pages/LoginPage'
import MfaSetupPage from './pages/MfaSetupPage' import MfaSetupPage from './pages/MfaSetupPage'
@ -19,6 +20,16 @@ import SettingsPage from './pages/SettingsPage'
function RequireAuth({ children }: { children: React.ReactNode }) { function RequireAuth({ children }: { children: React.ReactNode }) {
const isAuthenticated = useAuthStore((s) => s.isAuthenticated) const isAuthenticated = useAuthStore((s) => s.isAuthenticated)
const isRestoring = useAuthStore((s) => s.isRestoring)
if (isRestoring) {
return (
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}>
<CircularProgress />
</Box>
)
}
return isAuthenticated ? <>{children}</> : <Navigate to="/login" replace /> return isAuthenticated ? <>{children}</> : <Navigate to="/login" replace />
} }

View File

@ -1,3 +1,4 @@
import axios from 'axios'
import { create } from 'zustand' import { create } from 'zustand'
import { persist } from 'zustand/middleware' import { persist } from 'zustand/middleware'
import type { User } from '../types' import type { User } from '../types'
@ -7,6 +8,7 @@ interface AuthState {
refreshToken: string | null refreshToken: string | null
user: User | null user: User | null
isAuthenticated: boolean isAuthenticated: boolean
isRestoring: boolean
setTokens: (access: string, refresh: string) => void setTokens: (access: string, refresh: string) => void
setUser: (user: User) => void setUser: (user: User) => void
logout: () => void logout: () => void
@ -19,6 +21,7 @@ export const useAuthStore = create<AuthState>()(
refreshToken: null, refreshToken: null,
user: null, user: null,
isAuthenticated: false, isAuthenticated: false,
isRestoring: true,
setTokens: (access, refresh) => setTokens: (access, refresh) =>
set({ accessToken: access, refreshToken: refresh, isAuthenticated: true }), set({ accessToken: access, refreshToken: refresh, isAuthenticated: true }),
@ -26,12 +29,41 @@ export const useAuthStore = create<AuthState>()(
setUser: (user) => set({ user }), setUser: (user) => set({ user }),
logout: () => logout: () =>
set({ accessToken: null, refreshToken: null, user: null, isAuthenticated: false }), set({ accessToken: null, refreshToken: null, user: null, isAuthenticated: false, isRestoring: false }),
}), }),
{ {
name: 'pm-auth', name: 'pm-auth',
// Only persist refresh token; access token regenerated on load // Only persist refresh token; access token regenerated on load
partialize: (state) => ({ refreshToken: state.refreshToken, user: state.user }), partialize: (state) => ({ refreshToken: state.refreshToken, user: state.user }),
onRehydrateStorage: () => {
return (state) => {
if (state?.refreshToken) {
// Proactively refresh the access token using the persisted refresh token
axios.post('/api/v1/auth/refresh', { refresh_token: state.refreshToken })
.then(({ data }) => {
useAuthStore.setState({
accessToken: data.access_token,
refreshToken: data.refresh_token,
isAuthenticated: true,
isRestoring: false,
})
})
.catch(() => {
// Refresh token expired or invalid — clear all auth state
useAuthStore.setState({
accessToken: null,
refreshToken: null,
user: null,
isAuthenticated: false,
isRestoring: false,
})
})
} else {
// No refresh token — not logged in, skip restoration
useAuthStore.setState({ isRestoring: false })
}
}
},
} }
) )
) )