Skip to main content
The authService.login method is the frontend service layer that handles user authentication. This method calls the backend /api/login endpoint and manages the authentication flow.
This documentation covers the frontend service layer from the RespondeIA frontend repository. The backend API endpoints are hosted separately.

Source Code

From modules/auth/services/auth.service.ts:
export const authService = {
  login: async (data: any) => {
    const res = await fetch('/api/login', {
      method: 'POST',
      body: JSON.stringify(data),
      headers: { 'Content-Type': 'application/json' },
    });
    if (!res.ok) throw new Error('Credenciales incorrectas');
    return res.json();
  },
  // ...
};

Parameters

data
object
required
Login credentials object containing email and password.

Returns

Returns a Promise that resolves to the parsed JSON response from the backend API.

Error Handling

Throws an Error with message 'Credenciales incorrectas' when the API response is not ok (!res.ok).

Usage Example

From modules/auth/hooks/useLogin.ts:
import { useMutation } from '@tanstack/react-query';
import { authService } from '../services/auth.service';

export const useLogin = () => {
  return useMutation({
    mutationFn: authService.login,
    onSuccess: (data) => {
      // Handle successful login
      console.log('Login successful', data);
    },
    onError: (error) => {
      // Handle login error
      console.error('Login failed', error);
    }
  });
};
Used in LoginForm.tsx:
const LoginForm = () => {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const { mutate, isPending, error } = useLogin();

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    mutate({ email, password });
  };

  return (
    <form onSubmit={handleSubmit}>
      {/* Form fields */}
    </form>
  );
};

authService.register

Register new users

useLogin Hook

React Query hook wrapping the login service

LoginForm Component

UI component using the login service

useAuthStore

Zustand store for authentication state

Build docs developers (and LLMs) love