Brijesh's Git Server — argus-web @ 5a0714cb3aaa37575c5d632988d27f8cbf0e2c89

Web Ul for argus

src/app/actions/auth.js (view raw)

 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
"use server";

export async function login(formData) {
  try {
    const response = await fetch("http://localhost:8080/auth/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        email: formData.get("email"),
        password: formData.get("password"),
      }),
    });

    if (!response.ok) {
      throw new Error("Login failed");
    }

    const data = await response.json();

    // Note: We'll need to handle token storage on the client side
    // as we can't access localStorage from server actions
    return { success: true, data };
  } catch (error) {
    return { success: false, error: "Invalid credentials" };
  }
}

export async function register(formData) {
  try {
    const response = await fetch("http://localhost:8080/auth/register", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        email: formData.get("email"),
        password: formData.get("password"),
      }),
    });

    if (!response.ok) {
      const errorText = await response.text();
      throw new Error("Registration failed");
    }

    const data = await response.json();
    return { success: true, data };
  } catch (error) {
    return { success: false, error: "Registration failed " + error };
  }
}