Lesson 123 of 158 – Login Response Interface
78%

Login Response Interface

When a React Native application sends login information to a REST API, the server returns a response. TypeScript interfaces help us define the exact structure of this login response.

Note: In this lesson, we will create a complete TypeScript structure for login responses containing success status, messages, JWT tokens, user details, roles, and authentication information.

1. What is a Login Response?

A login response is the data returned by the server after the user submits login credentials.

A typical flow is:

React Native
     ↓
Email + Password
     ↓
PHP Login API
     ↓
Verify User
     ↓
Login Response
     ↓
React Native

2. Example Login Response

{
    "success": true,
    "message": "Login successful",
    "token": "eyJhbGciOiJIUzI1NiJ9...",
    "user": {
        "id": 101,
        "name": "Rahul Kumar",
        "email": "rahul@example.com"
    }
}

3. Why Use an Interface?

Without TypeScript, we may not know exactly which properties are available in the response.

An interface gives the response a clear structure.

interface LoginResponse {
    success: boolean;
    message: string;
    token: string;
}

4. Basic LoginResponse Interface

interface LoginResponse {
    success: boolean;
    message: string;
    token: string;
}

Here, success is a boolean, while message and token are strings.

5. Creating a User Interface

interface User {
    id: number;
    name: string;
    email: string;
}

The user object can then be included inside the login response.

6. LoginResponse with User

interface User {
    id: number;
    name: string;
    email: string;
}

interface LoginResponse {
    success: boolean;
    message: string;
    token: string;
    user: User;
}

7. Axios Login Request

const response =
    await axios.post<LoginResponse>(
        "/login",
        {
            email: "rahul@example.com",
            password: "123456"
        }
    );

The generic type <LoginResponse> tells TypeScript what structure to expect from the Axios response.

8. Accessing the Token

const token = response.data.token;

console.log(token);

Because the response is typed, TypeScript knows that token exists as a string.

9. Accessing User Information

const userName = response.data.user.name;
const email = response.data.user.email;

console.log(userName);
console.log(email);

10. Login Request Interface

We can also create a separate interface for the data sent to the server.

interface LoginRequest {
    email: string;
    password: string;
}

This separates request data from response data.

11. Typed Login Function

const login = async (
    data: LoginRequest
): Promise<LoginResponse> => {

    const response =
        await axios.post<LoginResponse>(
            "/login",
            data
        );

    return response.data;
};

12. Login Response with Role

Many applications return the user's role after successful login.

interface User {
    id: number;
    name: string;
    email: string;
    role: string;
}

The role can later be used by the application for navigation and display.

13. Literal Role Types

Instead of allowing any string, TypeScript can restrict the possible roles.

interface User {
    id: number;
    name: string;
    email: string;
    role: "admin" | "teacher" | "student";
}

This provides better type safety.

14. Login Response with Role

interface LoginResponse {
    success: boolean;
    message: string;
    token: string;
    user: {
        id: number;
        name: string;
        email: string;
        role: "admin" | "teacher" | "student";
    };
}

15. Optional User Information

Sometimes an API may not return user information when login fails. TypeScript can represent optional properties.

interface LoginResponse {
    success: boolean;
    message: string;
    token?: string;
    user?: User;
}

The question mark means the property may be absent.

16. Successful Login Response

{
    "success": true,
    "message": "Login successful",
    "token": "abc123",
    "user": {
        "id": 10,
        "name": "Amit",
        "email": "amit@example.com",
        "role": "student"
    }
}

17. Failed Login Response

{
    "success": false,
    "message": "Invalid email or password"
}

A failed response may not contain a token or user object.

18. Handling Successful Login

const response =
    await login(data);

if (response.success) {
    console.log(response.token);
    console.log(response.user.name);
}

19. Authentication State Interface

interface AuthState {
    token: string | null;
    user: User | null;
    isAuthenticated: boolean;
}

This interface can be used with React Native state or an authentication context.

20. Updating Authentication State

setAuth({
    token: response.token,
    user: response.user,
    isAuthenticated: true
});

After successful login, the application can update the authentication state.

21. Login Response and JWT

When JWT authentication is used, the token returned by the login API can be sent with future protected requests.

Authorization: Bearer <token>

The server verifies the JWT before returning protected information.

22. Typed Axios Protected Request

interface ProfileResponse {
    success: boolean;
    user: User;
}

const response =
    await axios.get<ProfileResponse>(
        "/profile",
        {
            headers: {
                Authorization:
                    `Bearer ${token}`
            }
        }
    );

console.log(response.data.user.name);

23. Login Error Response

interface LoginErrorResponse {
    success: false;
    message: string;
    code?: string;
}

A typed error structure makes authentication error handling easier.

{
    "success": false,
    "message": "Invalid credentials",
    "code": "INVALID_CREDENTIALS"
}

24. Login Response with Permissions

Some applications return permissions along with the authenticated user.

interface User {
    id: number;
    name: string;
    email: string;
    role: "admin" | "teacher" | "student";
    permissions: string[];
}

The server should remain responsible for enforcing these permissions.

25. Generic API Response

A reusable generic interface can represent different API responses.

interface ApiResponse<T> {
    success: boolean;
    message: string;
    data: T;
}

A login response can then be represented using ApiResponse.

26. Generic Login Response

interface LoginData {
    token: string;
    user: User;
}

type LoginResponse =
    ApiResponse<LoginData>;

This avoids repeating the same response structure across multiple APIs.

27. Complete Login Interfaces

interface User {
    id: number;
    name: string;
    email: string;
    role: "admin" | "teacher" | "student";
}

interface LoginRequest {
    email: string;
    password: string;
}

interface LoginData {
    token: string;
    user: User;
}

interface LoginResponse {
    success: boolean;
    message: string;
    data: LoginData;
}

28. Complete Typed Login Function

import axios from "axios";

interface User {
    id: number;
    name: string;
    email: string;
    role: "admin" | "teacher" | "student";
}

interface LoginRequest {
    email: string;
    password: string;
}

interface LoginResponse {
    success: boolean;
    message: string;
    token: string;
    user: User;
}

const login = async (
    data: LoginRequest
): Promise<LoginResponse> => {

    const response =
        await axios.post<LoginResponse>(
            "https://example.com/api/login",
            data
        );

    return response.data;
};

29. Using the Login Response in React Native

const handleLogin = async () => {

    const data: LoginRequest = {
        email: email,
        password: password
    };

    try {

        const result =
            await login(data);

        if (result.success) {

            console.log(
                "Token:",
                result.token
            );

            console.log(
                "User:",
                result.user.name
            );

            console.log(
                "Role:",
                result.user.role
            );
        }

    } catch (error) {

        console.log(
            "Login failed"
        );
    }
};

30. Complete Login Response Flow

LoginRequest
     ↓
Axios POST
     ↓
PHP Login API
     ↓
Verify Email + Password
     ↓
LoginResponse
     ↓
TypeScript checks structure
     ↓
Get Token + User
     ↓
Update AuthState
     ↓
Send Bearer Token
     ↓
Protected API

A well-defined LoginResponse interface makes the communication between the React Native application and PHP REST API easier to understand, maintain, and debug.

📌 Key Points

  • A login response contains information returned after authentication.
  • TypeScript interfaces describe the expected response structure.
  • A login response can contain a token and user information.
  • A separate User interface keeps the code reusable.
  • Roles can be represented using TypeScript literal types.
  • Optional properties can represent data that may not exist in failed responses.
  • Axios generics can type the login response.
  • Generic API response interfaces reduce repeated code.
  • Authentication state can contain token, user, and authentication status.
  • The server must still enforce authentication and authorization.

🧠 Quick Quiz

Question: Which Axios syntax correctly tells TypeScript that the API returns a LoginResponse?