All files / src/api auth-api.ts

100% Statements 118/118
80% Branches 16/20
100% Functions 3/3
100% Lines 118/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 1181x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 4x 2x 2x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 3x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 4x 2x 2x 1x 1x 4x 3x 3x 3x 4x 4x
import { AUTH_ENDPOINT } from "./types/endpoints";
 
/**
 * Signs in a user using their email and password credentials.
 *
 * Sends a POST request to the authentication endpoint to validate credentials.
 *
 * @param email - The user's email address.
 * @param password - The user's password.
 * @returns An object containing either the user token on success, or an error message on failure.
 */
export async function signIn(
    email: string,
    password: string
): Promise<{token?: string; error?: string}> {
    try {
        const response = await fetch(`${AUTH_ENDPOINT}/login?provider=email`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                email,
                password
            }),
        });
        const data = await response.json();
    
        if (!response.ok || !data.data) {
            return { error: data.message || `Failed to Sign in: ${response.status} ${response.statusText}` };
        }
 
        return data.data;
    } catch (err) {
        console.error("Error signing in:", err);
        return { error: err instanceof Error ? err.message : "Unknown error occurred" };
    }
 
}
 
 
/**
 * Signs out a user by invalidating their session on the backend.
 *
 * Sends a POST request to the signout endpoint with the user's ID.
 *
 * @param userID - The user's unique ID.
 * @returns An object indicating success or containing an error message.
 */
export async function signOut(userID: string): Promise<{ error?: string }> {
    try {
        const response = await fetch(`${AUTH_ENDPOINT}/signout`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                user_id: userID,
            }),
        });
 
        const data = await response.json();
        
        if (!response.ok) {
            return { error: data.message || `Failed to sign out: ${response.status} ${response.statusText}` };
        }
 
        return {};
    } catch (err) {
        return { error: err instanceof Error ? err.message : "Unknown error occurred" };
    }
}
 
 
/**
 * Registers a new user with the provided email, password, and name information.
 *
 * Sends a POST request to the signup endpoint to create a new user account.
 *
 * @param email - The user's email address.
 * @param password - The user's password.
 * @param firstName - The user's first name.
 * @param lastName - The user's last name.
 * @returns An object containing either the user token on success, or an error message on failure.
 */
export async function signUp(
    email: string,
    password: string,
    firstName: string,
    lastName: string
): Promise<{token?: string, error?: string}> {
    try {
        const response = await fetch(`${AUTH_ENDPOINT}/signup`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                email,
                password,
                first_name: firstName,
                last_name: lastName
            }),
        });
    
        const data = await response.json();
    
        if (!response.ok || !data.data) {
            throw new Error(data.error || "Failed to sign up");
        }
    
        return { token: data.data.token };
    } catch(err) {
        console.error("Error signing up:", err);
        return { error: err instanceof Error ? err.message : "Unknown error occurred" };
    }
    
}