update login error message

This commit is contained in:
Fran Jurmanović
2025-08-27 21:33:28 +02:00
parent 9ce5802dc6
commit 55e0370004
5 changed files with 51 additions and 10 deletions

View File

@@ -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');