Skip to main content

Overview

Authenticates an existing user with their email and password. On success, returns a JWT token along with basic user information.

Endpoint

POST /api/auth/login

Request

Headers

HeaderValue
Content-Typeapplication/json

Body parameters

email
string
required
The email address associated with the account.
password
string
required
The account password.

Response

Success

token
string
A JWT token to use for authenticated requests.
user.user_id
string
The unique identifier for the authenticated user.
user.username
string
The display name of the authenticated user.
user.profile_picture
string
URL of the user’s profile picture.

Error

error
string
Human-readable error message describing why login failed.

Example

TypeScript
type LoginResponse = {
  token: string;
  user: {
    user_id: string;
    username: string;
    profile_picture: string;
  };
};

import { SERVER_ENDPOINT } from "@env";

const API = SERVER_ENDPOINT ?? "";

const login = async (
  email: string,
  password: string,
): Promise<LoginResponse> => {
  const res = await fetch(`${API}/api/auth/login`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      email,
      password,
    }),
  });

  const data = await res.json();

  if (!res.ok) {
    throw new Error(data.error || "Login failed");
  }

  return data;
};