resolve logout issues

This commit is contained in:
Fran Jurmanović
2025-09-22 20:45:02 +02:00
parent 6aeb654abf
commit c005090ab1
7 changed files with 36 additions and 39 deletions

View File

@@ -43,5 +43,5 @@ export async function loginAction(prevState: LoginResult, formData: FormData) {
}
export async function logoutAction() {
logout();
await logout();
}

View File

@@ -17,9 +17,6 @@ const getSession = async (): Promise<SessionData | null> => {
}
return null;
};
const destroySession = async (): Promise<void> => {
await fetch('/api/session', { method: 'DELETE' });
};
export async function fetchClientAPI<T>(
endpoint: string,
@@ -52,7 +49,6 @@ export async function fetchClientAPI<T>(
if (!response.ok) {
if (response.status === 401) {
await destroySession();
window.location.href = '/login';
return { error: 'unauthorized' };
}

View File

@@ -6,8 +6,10 @@ type ApiResponse<T> = {
message?: string;
};
import { logout } from '@/lib/auth/server';
const destroySession = async (): Promise<void> => {
await fetch('/api/session', { method: 'DELETE' });
await logout();
};
export async function fetchServerAPI<T>(
@@ -31,7 +33,6 @@ export async function fetchServerAPI<T>(
if (!response.ok) {
if (response.status == 401) {
await destroySession();
window.location.href = '/login';
return { error: 'unauthorized' };
}
throw new Error(

View File

@@ -12,7 +12,6 @@ export async function requireAuth(skipRedirect?: boolean) {
const session = await getSession();
if (!skipRedirect && (!session.token || !session.user)) {
session.destroy();
redirect('/login');
}
@@ -28,6 +27,7 @@ export async function login(token: string, user: SessionData['user'], openToken?
}
export async function logout() {
'use server';
const session = await getSession();
session.destroy();
}