middleware fix wip

This commit is contained in:
2026-01-04 01:45:14 +01:00
parent 99092e2759
commit b8eb8fb005
7 changed files with 170 additions and 15 deletions

View File

@@ -35,13 +35,27 @@ export default function LoginPage() {
const [showPassword, setShowPassword] = useState(false);
const [showErrorDetails, setShowErrorDetails] = useState(false);
const [hasInsufficientPermissions, setHasInsufficientPermissions] = useState(false);
// Check if user is already authenticated
useEffect(() => {
if (session) {
router.replace(returnTo);
// If there's a returnTo parameter (user was redirected here from a protected route),
// they might not have permission. Don't auto-redirect them back.
const returnToParam = searchParams.get('returnTo');
console.log('[LOGIN] returnToParam:', returnToParam);
console.log('[LOGIN] returnTo:', returnTo);
const hasReturnTo = returnToParam !== null;
if (hasReturnTo) {
console.log('[LOGIN] Has returnTo, setting insufficient permissions');
setHasInsufficientPermissions(true);
} else {
// No returnTo means they navigated here directly while authenticated
console.log('[LOGIN] No returnTo, redirecting to dashboard');
router.replace('/dashboard');
}
}
}, [session, router, returnTo]);
}, [session, router, returnTo, searchParams]);
// Use enhanced form hook
const {
@@ -244,6 +258,28 @@ export default function LoginPage() {
</label>
</div>
{/* Insufficient Permissions Message */}
<AnimatePresence>
{hasInsufficientPermissions && (
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
className="p-4 rounded-lg bg-warning-amber/10 border border-warning-amber/30"
>
<div className="flex items-start gap-3">
<AlertCircle className="w-5 h-5 text-warning-amber flex-shrink-0 mt-0.5" />
<div className="text-sm text-gray-300">
<strong className="text-warning-amber">Insufficient Permissions</strong>
<p className="mt-1">
You don't have permission to access that page. Please log in with an account that has the required role.
</p>
</div>
</div>
</motion.div>
)}
</AnimatePresence>
{/* Enhanced Error Display */}
<AnimatePresence>
{formState.submitError && (