resolve logout issues
This commit is contained in:
@@ -43,5 +43,5 @@ export async function loginAction(prevState: LoginResult, formData: FormData) {
|
||||
}
|
||||
|
||||
export async function logoutAction() {
|
||||
logout();
|
||||
await logout();
|
||||
}
|
||||
|
||||
@@ -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' };
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user