Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Add a set password page.

Matthieu Hochlander129741206c0a
diff --git a/src/components/supabase/layout.tsx b/src/components/supabase/layout.tsx
new file mode 100644
index 00000000..a61cfb3f
--- /dev/null
+++ b/src/components/supabase/layout.tsx
@@ -0,0 +1,27 @@
+import * as React from "react";
+import { Notification } from "@/components/admin";
+import { useConfigurationContext } from "@/atomic-crm/root/ConfigurationContext";
+
+export const Layout = ({ children }: React.PropsWithChildren) => {
+ const { darkModeLogo, title } = useConfigurationContext();
+
+ return (
+ <div className="min-h-screen flex">
+ <div className="container relative grid flex-col items-center justify-center sm:max-w-none lg:grid-cols-2 lg:px-0">
+ <div className="relative hidden h-full flex-col bg-muted p-10 text-white dark:border-r lg:flex">
+ <div className="absolute inset-0 bg-zinc-900" />
+ <div className="relative z-20 flex items-center text-lg font-medium">
+ <img className="h-6 mr-2" src={darkModeLogo} alt={title} />
+ {title}
+ </div>
+ </div>
+ <div className="lg:p-8">
+ <div className="mx-auto flex w-full flex-col justify-center space-y-6 sm:w-[350px]">
+ {children}
+ </div>
+ </div>
+ </div>
+ <Notification />
+ </div>
+ );
+};
diff --git a/src/components/supabase/set-password-page.tsx b/src/components/supabase/set-password-page.tsx
index 61cc2e0f..7a12f950 100644
--- a/src/components/supabase/set-password-page.tsx
+++ b/src/components/supabase/set-password-page.tsx
@@ -1,5 +1,117 @@
-export function SetPasswordPage() {
- return null;
+import { useState } from "react";
+import { Form, required, useNotify, useTranslate, ValidateForm } from "ra-core";
+import { useSetPassword, useSupabaseAccessToken } from "ra-supabase-core";
+import { FieldValues, SubmitHandler } from "react-hook-form";
+import { Button } from "@/components/ui/button";
+import { TextInput } from "@/components/admin/text-input";
+import { Layout } from "@/components/supabase/layout";
+
+interface FormData {
+ password: string;
+ confirmPassword: string;
}
+export const SetPasswordPage = () => {
+ const [loading, setLoading] = useState(false);
+
+ const access_token = useSupabaseAccessToken();
+ const refresh_token = useSupabaseAccessToken({
+ parameterName: "refresh_token",
+ });
+
+ const notify = useNotify();
+ const translate = useTranslate();
+ const [, { mutateAsync: setPassword }] = useSetPassword();
+
+ const validate = (values: FormData) => {
+ if (values.password !== values.confirmPassword) {
+ return {
+ password: "ra-supabase.validation.password_mismatch",
+ confirmPassword: "ra-supabase.validation.password_mismatch",
+ };
+ }
+ return {};
+ };
+
+ if (!access_token || !refresh_token) {
+ if (process.env.NODE_ENV === "development") {
+ console.error("Missing access_token or refresh_token for set password");
+ }
+ return (
+ <Layout>
+ <p>{translate("ra-supabase.auth.missing_tokens")}</p>
+ </Layout>
+ );
+ }
+
+ const submit = async (values: FormData) => {
+ try {
+ setLoading(true);
+ await setPassword({
+ access_token,
+ refresh_token,
+ password: values.password,
+ });
+ setLoading(false);
+ } catch (error: any) {
+ notify(
+ typeof error === "string"
+ ? error
+ : typeof error === "undefined" || !error.message
+ ? "ra.auth.sign_in_error"
+ : error.message,
+ {
+ type: "warning",
+ messageArgs: {
+ _:
+ typeof error === "string"
+ ? error
+ : error && error.message
+ ? error.message
+ : undefined,
+ },
+ },
+ );
+ }
+ };
+
+ return (
+ <Layout>
+ <div className="flex flex-col space-y-2 text-center">
+ <h1 className="text-2xl font-semibold tracking-tight">
+ {translate("ra-supabase.set_password.new_password", {
+ _: "Choose your password",
+ })}
+ </h1>
+ </div>
+ <Form<FormData>
+ className="space-y-8"
+ onSubmit={submit as SubmitHandler<FieldValues>}
+ validate={validate as ValidateForm}
+ >
+ <TextInput
+ label={translate("ra.auth.password", {
+ _: "Password",
+ })}
+ autoComplete="new-password"
+ source="password"
+ type="password"
+ validate={required()}
+ />
+ <TextInput
+ label={translate("ra.auth.confirm_password", {
+ _: "Confirm password",
+ })}
+ source="confirmPassword"
+ type="password"
+ validate={required()}
+ />
+ <Button type="submit" className="cursor-pointer" disabled={loading}>
+ {translate("ra.action.save")}
+ </Button>
+ </Form>
+ </Layout>
+ );
+};
+
SetPasswordPage.path = "set-password";