update login error message
This commit is contained in:
@@ -4,13 +4,21 @@ import { redirect } from 'next/navigation';
|
||||
import { loginUser } from '@/lib/api/server/auth';
|
||||
import { login, logout } from '@/lib/auth/server';
|
||||
|
||||
export async function loginAction(formData: FormData) {
|
||||
export type LoginResult = {
|
||||
success: boolean;
|
||||
message: string;
|
||||
};
|
||||
|
||||
export async function loginAction(prevState: LoginResult, formData: FormData) {
|
||||
try {
|
||||
const username = formData.get('username') as string;
|
||||
const password = formData.get('password') as string;
|
||||
|
||||
if (!username || !password) {
|
||||
throw new Error('Username and password are required');
|
||||
return {
|
||||
success: false,
|
||||
message: 'Username and password are required'
|
||||
};
|
||||
}
|
||||
|
||||
const result = await loginUser(username, password);
|
||||
@@ -18,10 +26,16 @@ export async function loginAction(formData: FormData) {
|
||||
if (result.token && result.user) {
|
||||
await login(result.token, result.user);
|
||||
} else {
|
||||
throw new Error('Invalid credentials');
|
||||
return {
|
||||
success: false,
|
||||
message: 'Invalid credentials'
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(error instanceof Error ? error.message : 'Authentication failed');
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'Authentication failed'
|
||||
};
|
||||
}
|
||||
|
||||
redirect('/dashboard');
|
||||
|
||||
@@ -11,7 +11,10 @@ export async function startServerAction(serverId: string) {
|
||||
revalidatePath('/dashboard');
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
} catch (error) {
|
||||
throw new Error(error instanceof Error ? error.message : 'Failed to start server');
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'Failed to start server'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +25,10 @@ export async function stopServerAction(serverId: string) {
|
||||
revalidatePath('/dashboard');
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
} catch (error) {
|
||||
throw new Error(error instanceof Error ? error.message : 'Failed to stop server');
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'Failed to stop server'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +39,9 @@ export async function restartServerAction(serverId: string) {
|
||||
revalidatePath('/dashboard');
|
||||
revalidatePath(`/dashboard/server/${serverId}`);
|
||||
} catch (error) {
|
||||
throw new Error(error instanceof Error ? error.message : 'Failed to restart server');
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'Failed to restart server'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,11 @@ export async function loginUser(username: string, password: string) {
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Login failed: ${response.statusText} - ${BASE_URL}${authRoute}/login`);
|
||||
if (response.status === 401) {
|
||||
throw new Error(`Invalid credentials`);
|
||||
}
|
||||
|
||||
throw new Error(`Login failed: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const { token } = await response.json();
|
||||
|
||||
Reference in New Issue
Block a user