Skip to content

Commit e6c65a4

Browse files
author
Clément VALENTIN
committed
fix: Add location state handling for navigation in ProtectedRoute, Login, and Settings components
1 parent 14db3a2 commit e6c65a4

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

apps/web/src/App.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Routes, Route, Navigate } from 'react-router-dom'
1+
import { Routes, Route, Navigate, useLocation } from 'react-router-dom'
22
import { Suspense, lazy } from 'react'
33
import { useAuth } from './hooks/useAuth'
44
import Layout from './components/Layout'
@@ -50,6 +50,7 @@ function PageLoader() {
5050

5151
function ProtectedRoute({ children }: { children: React.ReactNode }) {
5252
const { isAuthenticated, isLoading } = useAuth()
53+
const location = useLocation()
5354

5455
if (isLoading) {
5556
return (
@@ -59,7 +60,12 @@ function ProtectedRoute({ children }: { children: React.ReactNode }) {
5960
)
6061
}
6162

62-
return isAuthenticated ? <>{children}</> : <Navigate to="/login" replace />
63+
if (!isAuthenticated) {
64+
// Sauvegarder l'URL courante pour y revenir après connexion
65+
return <Navigate to="/login" state={{ from: location.pathname }} replace />
66+
}
67+
68+
return <>{children}</>
6369
}
6470

6571
function PublicRoute({ children }: { children: React.ReactNode }) {

apps/web/src/pages/Login.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import { useState } from 'react'
2-
import { Link, useNavigate } from 'react-router-dom'
2+
import { Link, useNavigate, useLocation } from 'react-router-dom'
33
import { useAuth } from '@/hooks/useAuth'
44
import { LogIn, Eye, EyeOff } from 'lucide-react'
55

6+
interface LocationState {
7+
from?: string
8+
}
9+
610
export default function Login() {
711
const [email, setEmail] = useState('')
812
const [password, setPassword] = useState('')
913
const [showPassword, setShowPassword] = useState(false)
1014
const { login, loginLoading, loginError } = useAuth()
1115
const navigate = useNavigate()
16+
const location = useLocation()
17+
18+
// Récupérer l'URL de redirection depuis le state ou utiliser /dashboard par défaut
19+
const from = (location.state as LocationState)?.from || '/dashboard'
1220

1321
const handleSubmit = async (e: React.FormEvent) => {
1422
e.preventDefault()
1523
login({ email, password }, {
16-
onSuccess: () => navigate('/dashboard'),
24+
onSuccess: () => navigate(from, { replace: true }),
1725
})
1826
}
1927

apps/web/src/pages/Settings.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState } from 'react'
22
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
3-
import { useNavigate } from 'react-router-dom'
3+
import { useNavigate, useLocation } from 'react-router-dom'
44
import toast from 'react-hot-toast'
55
import { authApi } from '@/api/auth'
66
import { useAuth } from '@/hooks/useAuth'
@@ -11,6 +11,7 @@ import { Trash2, TrendingUp, Copy, RefreshCw, Key, Lock, LogOut, Palette, Eye, E
1111
export default function Settings() {
1212
const { user, logout } = useAuth()
1313
const navigate = useNavigate()
14+
const location = useLocation()
1415
const queryClient = useQueryClient()
1516
const { mode, setMode } = useThemeStore()
1617
const isDemo = useIsDemo()
@@ -131,7 +132,7 @@ export default function Settings() {
131132

132133
const handleLogout = () => {
133134
logout()
134-
navigate('/login')
135+
navigate('/login', { state: { from: location.pathname } })
135136
}
136137

137138
const copyNewSecret = () => {

0 commit comments

Comments
 (0)