AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): password reset via server callback (#10303)
Ubbe01950ccc424a
diff --git a/autogpt_platform/frontend/src/app/(platform)/reset-password/actions.ts b/autogpt_platform/frontend/src/app/(platform)/reset-password/actions.tsindex 327b4f86de..e0d0463917 100644--- a/autogpt_platform/frontend/src/app/(platform)/reset-password/actions.ts+++ b/autogpt_platform/frontend/src/app/(platform)/reset-password/actions.ts@@ -27,7 +27,7 @@ export async function sendResetEmail(email: string, turnstileToken: string) {}const { error } = await supabase.auth.resetPasswordForEmail(email, {- redirectTo: `${origin}/reset-password`,+ redirectTo: `${origin}/api/auth/callback/reset-password`,});if (error) {diff --git a/autogpt_platform/frontend/src/app/(platform)/reset-password/page.tsx b/autogpt_platform/frontend/src/app/(platform)/reset-password/page.tsxindex 304d3d452c..fb794d86c2 100644--- a/autogpt_platform/frontend/src/app/(platform)/reset-password/page.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/reset-password/page.tsx@@ -18,18 +18,23 @@ import {} from "@/components/ui/form";import { Input } from "@/components/ui/input";import LoadingBox from "@/components/ui/loading";+import { useToast } from "@/components/ui/use-toast";import { useTurnstile } from "@/hooks/useTurnstile";import { useSupabase } from "@/lib/supabase/hooks/useSupabase";import { getBehaveAs } from "@/lib/utils";import { changePasswordFormSchema, sendEmailFormSchema } from "@/types/auth";import { zodResolver } from "@hookform/resolvers/zod";-import { useCallback, useState } from "react";+import { useRouter, useSearchParams } from "next/navigation";+import { useCallback, useEffect, useState } from "react";import { useForm } from "react-hook-form";import { z } from "zod";import { changePassword, sendResetEmail } from "./actions";export default function ResetPasswordPage() {const { supabase, user, isUserLoading } = useSupabase();+ const { toast } = useToast();+ const searchParams = useSearchParams();+ const router = useRouter();const [isLoading, setIsLoading] = useState(false);const [feedback, setFeedback] = useState<string | null>(null);const [isError, setIsError] = useState(false);@@ -37,6 +42,21 @@ export default function ResetPasswordPage() {const [sendEmailCaptchaKey, setSendEmailCaptchaKey] = useState(0);const [changePasswordCaptchaKey, setChangePasswordCaptchaKey] = useState(0);+ useEffect(() => {+ const error = searchParams.get("error");+ if (error) {+ toast({+ title: "Password Reset Failed",+ description: error,+ variant: "destructive",+ });++ const newUrl = new URL(window.location.href);+ newUrl.searchParams.delete("error");+ router.replace(newUrl.pathname + newUrl.search);+ }+ }, [searchParams, toast, router]);+const sendEmailTurnstile = useTurnstile({action: "reset_password",autoVerify: false,diff --git a/autogpt_platform/frontend/src/app/api/auth/callback/reset-password/route.ts b/autogpt_platform/frontend/src/app/api/auth/callback/reset-password/route.tsnew file mode 100644index 0000000000..c88b14e06d--- /dev/null+++ b/autogpt_platform/frontend/src/app/api/auth/callback/reset-password/route.ts@@ -0,0 +1,41 @@+import { exchangePasswordResetCode } from "@/lib/supabase/helpers";+import { getServerSupabase } from "@/lib/supabase/server/getServerSupabase";+import { NextRequest, NextResponse } from "next/server";++export async function GET(request: NextRequest) {+ const { searchParams } = new URL(request.url);+ const code = searchParams.get("code");+ const origin =+ process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || "http://localhost:3000";++ if (!code) {+ return NextResponse.redirect(+ `${origin}/reset-password?error=Missing verification code`,+ );+ }++ try {+ const supabase = await getServerSupabase();++ if (!supabase) {+ return NextResponse.redirect(+ `${origin}/reset-password?error=no-auth-client`,+ );+ }++ const result = await exchangePasswordResetCode(supabase, code);++ if (!result.success) {+ return NextResponse.redirect(+ `${origin}/reset-password?error=${encodeURIComponent(result.error || "Password reset failed")}`,+ );+ }++ return NextResponse.redirect(`${origin}/reset-password`);+ } catch (error) {+ console.error("Password reset callback error:", error);+ return NextResponse.redirect(+ `${origin}/reset-password?error=Password reset failed`,+ );+ }+}diff --git a/autogpt_platform/frontend/src/lib/supabase/helpers.ts b/autogpt_platform/frontend/src/lib/supabase/helpers.tsindex 2118b0e474..3d26e2228e 100644--- a/autogpt_platform/frontend/src/lib/supabase/helpers.ts+++ b/autogpt_platform/frontend/src/lib/supabase/helpers.ts@@ -1,4 +1,5 @@import { type CookieOptions } from "@supabase/ssr";+import { SupabaseClient } from "@supabase/supabase-js";// Detect if we're in a Playwright test environmentconst isTest = process.env.NEXT_PUBLIC_PW_TEST === "true";@@ -98,3 +99,38 @@ export function setupSessionEventListeners(},};}++export interface CodeExchangeResult {+ success: boolean;+ error?: string;+}++export async function exchangePasswordResetCode(+ supabase: SupabaseClient<any, "public", any>,+ code: string,+): Promise<CodeExchangeResult> {+ try {+ const { data, error } = await supabase.auth.exchangeCodeForSession(code);++ if (error) {+ return {+ success: false,+ error: error.message,+ };+ }++ if (!data.session) {+ return {+ success: false,+ error: "Failed to create session",+ };+ }++ return { success: true };+ } catch (error) {+ return {+ success: false,+ error: error instanceof Error ? error.message : "Unknown error",+ };+ }+}diff --git a/autogpt_platform/frontend/src/lib/supabase/middleware.ts b/autogpt_platform/frontend/src/lib/supabase/middleware.tsindex 0d5607abb2..d2d32e389f 100644--- a/autogpt_platform/frontend/src/lib/supabase/middleware.ts+++ b/autogpt_platform/frontend/src/lib/supabase/middleware.ts@@ -43,18 +43,17 @@ export async function updateSession(request: NextRequest) {},);- // IMPORTANT: Avoid writing any logic between createServerClient and- // supabase.auth.getUser(). A simple mistake could make it very hard to debug- // issues with users being randomly logged out.-- const {- data: { user },- } = await supabase.auth.getUser();-+ const userResponse = await supabase.auth.getUser();+ const user = userResponse.data.user;const userRole = user?.role;+const url = request.nextUrl.clone();const pathname = request.nextUrl.pathname;+ // IMPORTANT: Avoid writing any logic between createServerClient and+ // supabase.auth.getUser(). A simple mistake could make it very hard to debug+ // issues with users being randomly logged out.+// AUTH REDIRECTS// 1. Check if user is not authenticated but trying to access protected contentif (!user) {