Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/lib/password-reset.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,40 @@ export const requestPasswordReset = createServerFn({ method: "POST" })
const verified = await verifyTurnstile(data.turnstileToken, ip, "password_reset");
if (!verified) throw new Error("Human verification failed. Please try again.");

const { createClient } = await import("@supabase/supabase-js");
// Resolve the administrator email server-side from the role assignment.
// Never trust the submitted address or expose service-role credentials.
const { supabaseAdmin } = await import("@/integrations/supabase/client.server");
const { data: adminRoles, error: roleError } = await supabaseAdmin
.from("user_roles")
.select("user_id")
.eq("role", "admin");

if (roleError) {
console.error("[password-reset] admin role lookup failed", roleError);
throw new Error("Unable to process the request.");
}

let adminEmailMatches = false;
for (const role of adminRoles ?? []) {
const { data: userData, error: userError } = await supabaseAdmin.auth.admin.getUserById(role.user_id);
if (userError) {
console.error("[password-reset] admin user lookup failed", userError);
throw new Error("Unable to process the request.");
}
if (userData.user?.email?.trim().toLowerCase() === data.email) {
adminEmailMatches = true;
break;
}
}

if (!adminEmailMatches) return { success: false as const, reason: "invalid_email" as const };

const supabaseUrl = process.env.VITE_SUPABASE_URL || process.env.SUPABASE_URL;
const publishableKey =
process.env.VITE_SUPABASE_PUBLISHABLE_KEY || process.env.SUPABASE_PUBLISHABLE_KEY;
if (!supabaseUrl || !publishableKey) throw new Error("Supabase server configuration is incomplete.");

const { createClient } = await import("@supabase/supabase-js");
const userClient = createClient(supabaseUrl, publishableKey, {
auth: { autoRefreshToken: false, persistSession: false, detectSessionInUrl: false },
});
Expand All @@ -34,6 +62,5 @@ export const requestPasswordReset = createServerFn({ method: "POST" })

if (error) throw new Error("Unable to process the request.");

// Deliberately do not reveal whether the submitted address exists.
return { success: true };
return { success: true as const };
});
18 changes: 14 additions & 4 deletions src/routes/forgot-password.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* /forgot-password — Admin password recovery request.
*
* Supabase sends the recovery email. The page deliberately returns the same
* success message regardless of whether the address exists to avoid account
* enumeration.
* Supabase sends the recovery email. The server validates the address against
* the administrator role before allowing the reset request.
*/
import { createFileRoute, Link, useNavigate } from "@tanstack/react-router";
import { useState } from "react";
Expand Down Expand Up @@ -46,20 +45,31 @@ function ForgotPasswordPage() {

setBusy(true);
try {
await requestPasswordReset({
const result = await requestPasswordReset({
data: {
email: normalizedEmail,
turnstileToken,
},
});

if (!result.success) {
toast.error("Invalid email address", {
icon: <Info className="h-4 w-4" />,
});
setTurnstileToken("");
setResetKey((key) => key + 1);
return;
}

setSent(true);
setTurnstileToken("");
setResetKey((key) => key + 1);
} catch {
toast.error("Unable to process the request. Please try again.", {
icon: <Info className="h-4 w-4" />,
});
setTurnstileToken("");
setResetKey((key) => key + 1);
} finally {
setBusy(false);
}
Expand Down
Loading