fix logout issues

This commit is contained in:
Fran Jurmanović
2025-09-22 22:49:15 +02:00
parent c005090ab1
commit 9a25b5a28c
4 changed files with 23 additions and 10 deletions

View File

@@ -1,7 +1,8 @@
'use client'; 'use client';
import { loginAction, LoginResult } from '@/lib/actions/auth'; import { loginAction, LoginResult, clearExpiredSessionAction } from '@/lib/actions/auth';
import { useActionState } from 'react'; import { useActionState, useEffect } from 'react';
import { useSearchParams } from 'next/navigation';
const initialState: LoginResult = { const initialState: LoginResult = {
message: '', message: '',
@@ -9,7 +10,15 @@ const initialState: LoginResult = {
}; };
export default function LoginPage() { export default function LoginPage() {
const searchParams = useSearchParams();
const expired = searchParams.get('expired') === 'true';
const [state, formAction] = useActionState(loginAction, initialState); const [state, formAction] = useActionState(loginAction, initialState);
useEffect(() => {
if (expired) {
clearExpiredSessionAction();
}
}, [expired]);
return ( return (
<div className="flex min-h-screen items-center justify-center bg-gray-900 px-4"> <div className="flex min-h-screen items-center justify-center bg-gray-900 px-4">
<div className="w-full max-w-md space-y-8 rounded-lg bg-gray-800 p-8 shadow-lg"> <div className="w-full max-w-md space-y-8 rounded-lg bg-gray-800 p-8 shadow-lg">
@@ -17,6 +26,11 @@ export default function LoginPage() {
<h1 className="text-3xl font-bold text-white">ACC Server Manager</h1> <h1 className="text-3xl font-bold text-white">ACC Server Manager</h1>
<p className="mt-2 text-gray-400">Sign in to manage your servers</p> <p className="mt-2 text-gray-400">Sign in to manage your servers</p>
</div> </div>
{expired && (
<div className="rounded-md border border-yellow-700 bg-yellow-900/50 p-3 text-sm text-yellow-200">
Your session has expired. Please sign in again.
</div>
)}
{state?.success ? null : ( {state?.success ? null : (
<div className="rounded-md border border-red-700 bg-red-900/50 p-3 text-sm text-red-200"> <div className="rounded-md border border-red-700 bg-red-900/50 p-3 text-sm text-red-200">
{state?.message} {state?.message}

View File

@@ -4,7 +4,7 @@ import { logoutAction } from '@/lib/actions/auth';
import { useActionState } from 'react'; import { useActionState } from 'react';
export default function LogoutButton() { export default function LogoutButton() {
const [_, formAction] = useActionState(logoutAction, null); const [, formAction] = useActionState(logoutAction, null);
return ( return (
<form action={formAction}> <form action={formAction}>
<button type="submit" className="flex items-center text-gray-300 hover:text-white"> <button type="submit" className="flex items-center text-gray-300 hover:text-white">

View File

@@ -45,3 +45,7 @@ export async function loginAction(prevState: LoginResult, formData: FormData) {
export async function logoutAction() { export async function logoutAction() {
await logout(); await logout();
} }
export async function clearExpiredSessionAction() {
await logout();
}

View File

@@ -6,11 +6,7 @@ type ApiResponse<T> = {
message?: string; message?: string;
}; };
import { logout } from '@/lib/auth/server'; import { redirect } from 'next/navigation';
const destroySession = async (): Promise<void> => {
await logout();
};
export async function fetchServerAPI<T>( export async function fetchServerAPI<T>(
endpoint: string, endpoint: string,
@@ -32,8 +28,7 @@ export async function fetchServerAPI<T>(
if (!response.ok) { if (!response.ok) {
if (response.status == 401) { if (response.status == 401) {
await destroySession(); redirect('/login?expired=true');
return { error: 'unauthorized' };
} }
throw new Error( throw new Error(
`API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint} - ${token}` `API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint} - ${token}`