Brijesh's Git Server — argus-web @ 08dd7d27ed3795849d48eafe5c50fcde3128e2b2

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
 54
 55
 56
 57
 58
 59
 60
"use server";

export async function login(formData) {
  try {
    const response = await fetch(
      "http://localhost:8080/twirp/auth.AuthService/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) {
    console.error("Login error:", error);
    return { success: false, error: "Invalid credentials" };
  }
}

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

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

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