Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(frontend): Auth pages update #9124

Merged
merged 13 commits into from
Dec 30, 2024
51 changes: 19 additions & 32 deletions autogpt_platform/frontend/src/app/login/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { z } from "zod";
import * as Sentry from "@sentry/nextjs";
import getServerSupabase from "@/lib/supabase/getServerSupabase";
import BackendAPI from "@/lib/autogpt-server-api";

const loginFormSchema = z.object({
email: z.string().email().min(2).max(64),
password: z.string().min(6).max(64),
});
import { loginFormSchema, LoginProvider } from "@/types/auth";

export async function logout() {
return await Sentry.withServerActionInstrumentation(
Expand All @@ -25,7 +21,7 @@ export async function logout() {
const { error } = await supabase.auth.signOut();

if (error) {
console.log("Error logging out", error);
console.error("Error logging out", error);
return error.message;
}

Expand All @@ -47,18 +43,13 @@ export async function login(values: z.infer<typeof loginFormSchema>) {
// We are sure that the values are of the correct type because zod validates the form
const { data, error } = await supabase.auth.signInWithPassword(values);

await api.createUser();

if (error) {
console.log("Error logging in", error);
if (error.status == 400) {
// Hence User is not present
redirect("/login");
}

console.error("Error logging in", error);
return error.message;
}

await api.createUser();

if (data.session) {
await supabase.auth.setSession(data.session);
}
Expand All @@ -68,38 +59,34 @@ export async function login(values: z.infer<typeof loginFormSchema>) {
});
}

export async function signup(values: z.infer<typeof loginFormSchema>) {
"use server";
export async function providerLogin(provider: LoginProvider) {
return await Sentry.withServerActionInstrumentation(
"signup",
"providerLogin",
{},
async () => {
const supabase = getServerSupabase();
const api = new BackendAPI();

if (!supabase) {
redirect("/error");
}

// We are sure that the values are of the correct type because zod validates the form
const { data, error } = await supabase.auth.signUp(values);
const { error } = await supabase!.auth.signInWithOAuth({
provider: provider,
options: {
redirectTo:
process.env.AUTH_CALLBACK_URL ??
`http://localhost:3000/auth/callback`,
},
});

if (error) {
console.log("Error signing up", error);
if (error.message.includes("P0001")) {
return "Please join our waitlist for your turn: https://agpt.co/waitlist";
}
if (error.code?.includes("user_already_exists")) {
redirect("/login");
}
console.error("Error logging in", error);
return error.message;
}

if (data.session) {
await supabase.auth.setSession(data.session);
}
console.log("Signed up");
revalidatePath("/", "layout");
redirect("/store/profile");
await api.createUser();
console.log("Logged in");
},
);
}
Loading
Loading