fix(build): read URL params via window in guards (avoid useSearchParams prerender error)
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
This commit is contained in:
@@ -1,22 +1,19 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
import { authApi } from '@/lib/api';
|
||||
|
||||
export default function AdminGuard({ children }: { children: React.ReactNode }) {
|
||||
const params = useSearchParams();
|
||||
const [state, setState] = useState<'checking' | 'ok' | 'denied'>('checking');
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
const adm = params.get('adm');
|
||||
const url = new URL(window.location.href);
|
||||
const adm = url.searchParams.get('adm');
|
||||
try {
|
||||
if (adm) {
|
||||
await authApi.session({ adm });
|
||||
// strip the param from URL but keep route
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete('adm');
|
||||
window.history.replaceState({}, '', url.toString());
|
||||
}
|
||||
@@ -29,7 +26,7 @@ export default function AdminGuard({ children }: { children: React.ReactNode })
|
||||
}
|
||||
})();
|
||||
return () => { cancelled = true; };
|
||||
}, [params]);
|
||||
}, []);
|
||||
|
||||
if (state === 'checking') return <div className="min-h-screen flex items-center justify-center text-gray-500">Verificando…</div>;
|
||||
if (state === 'denied') return <div className="min-h-screen flex items-center justify-center text-center px-6">
|
||||
|
||||
Reference in New Issue
Block a user