A full-featured CRM built with React, shadcn/ui, and Supabase.
Add SSO support to login/sign-up pages
Gildasfa208145fe04
diff --git a/src/components/atomic-crm/login/LoginPage.tsx b/src/components/atomic-crm/login/LoginPage.tsxnew file mode 100644index 00000000..15b5d31f--- /dev/null+++ b/src/components/atomic-crm/login/LoginPage.tsx@@ -0,0 +1,116 @@+import { useState } from "react";+import { Form, required, useLogin, useNotify } from "ra-core";+import type { SubmitHandler, FieldValues } from "react-hook-form";+import { Link } from "react-router";+import { Button } from "@/components/ui/button";+import { TextInput } from "@/components/admin/text-input";+import { Notification } from "@/components/admin/notification";+import { useConfigurationContext } from "@/components/atomic-crm/root/ConfigurationContext.tsx";+import { SSOAuthButton } from "./SSOAuthButton";++/**+ * Login page displayed when authentication is enabled and the user is not authenticated.+ *+ * Automatically shown when an unauthenticated user tries to access a protected route.+ * Handles login via authProvider.login() and displays error notifications on failure.+ *+ * @see {@link https://marmelab.com/shadcn-admin-kit/docs/loginpage LoginPage documentation}+ * @see {@link https://marmelab.com/shadcn-admin-kit/docs/security Security documentation}+ */+export const LoginPage = (props: { redirectTo?: string }) => {+ const { darkModeLogo, title, googleWorkplaceDomain } =+ useConfigurationContext();+ const { redirectTo } = props;+ const [loading, setLoading] = useState(false);+ const login = useLogin();+ const notify = useNotify();++ const handleSubmit: SubmitHandler<FieldValues> = (values) => {+ setLoading(true);+ login(values, redirectTo)+ .then(() => {+ setLoading(false);+ })+ .catch((error) => {+ setLoading(false);+ notify(+ typeof error === "string"+ ? error+ : typeof error === "undefined" || !error.message+ ? "ra.auth.sign_in_error"+ : error.message,+ {+ type: "error",+ messageArgs: {+ _:+ typeof error === "string"+ ? error+ : error && error.message+ ? error.message+ : undefined,+ },+ },+ );+ });+ };++ 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]">+ <div className="flex flex-col space-y-2 text-center">+ <h1 className="text-2xl font-semibold tracking-tight">Sign in</h1>+ </div>+ <Form className="space-y-8" onSubmit={handleSubmit}>+ <TextInput+ label="Email"+ source="email"+ type="email"+ validate={required()}+ />+ <TextInput+ label="Password"+ source="password"+ type="password"+ validate={required()}+ />+ <div className="flex flex-col gap-4">+ <Button+ type="submit"+ className="cursor-pointer"+ disabled={loading}+ >+ Sign in+ </Button>+ {googleWorkplaceDomain ? (+ <SSOAuthButton+ className="w-full"+ domain={googleWorkplaceDomain}+ >+ Sign in with Google Workplace+ </SSOAuthButton>+ ) : null}+ </div>+ </Form>++ <Link+ to={"/forgot-password"}+ className="text-sm text-center hover:underline"+ >+ Forgot your password?+ </Link>+ </div>+ </div>+ </div>+ <Notification />+ </div>+ );+};diff --git a/src/components/atomic-crm/login/SSOAuthButton.tsx b/src/components/atomic-crm/login/SSOAuthButton.tsxnew file mode 100644index 00000000..dff8a5fe--- /dev/null+++ b/src/components/atomic-crm/login/SSOAuthButton.tsx@@ -0,0 +1,67 @@+import { useState, type MouseEvent, type ComponentProps } from "react";+import { useLogin, useNotify } from "ra-core";+import { Button } from "@/components/ui/button";+import { Spinner } from "@/components/ui/spinner";++export const SSOAuthButton = ({+ children,+ domain,+ redirect: redirectTo,+ ...props+}: SSOAuthButtonProps) => {+ const login = useLogin();+ const notify = useNotify();+ const [isPending, setIsPending] = useState(false);++ const handleClick = (event: MouseEvent<HTMLButtonElement>) => {+ event.preventDefault();+ setIsPending(true);+ login(+ { ssoDomain: domain },+ redirectTo ?? window.location.toString(),+ ).catch((error) => {+ setIsPending(false);+ // The authProvide always reject for OAuth login but there will be no error+ // if the call actually succeeds. This is to avoid react-admin redirecting+ // immediately to the provided redirect prop before users are redirected to+ // the OAuth provider.+ if (error) {+ notify(+ typeof error === "string"+ ? error+ : typeof error === "undefined" || !error.message+ ? "ra.auth.sign_in_error"+ : error.message,+ {+ type: "error",+ messageArgs: {+ _:+ typeof error === "string"+ ? error+ : error && error.message+ ? error.message+ : undefined,+ },+ },+ );+ }+ });+ };++ return (+ <Button type="button" onClick={handleClick} disabled={isPending} {...props}>+ {children}+ {isPending ? (+ <Spinner+ className="text-primary-foreground size-4"+ data-icon="inline-start"+ />+ ) : null}+ </Button>+ );+};++export type SSOAuthButtonProps = {+ domain: string;+ redirect?: string;+} & ComponentProps<typeof Button>;diff --git a/src/components/atomic-crm/login/SignupPage.tsx b/src/components/atomic-crm/login/SignupPage.tsxindex a8d68180..d4d9f7ac 100644--- a/src/components/atomic-crm/login/SignupPage.tsx+++ b/src/components/atomic-crm/login/SignupPage.tsx@@ -13,11 +13,16 @@ import type { SignUpData } from "../types";import { LoginSkeleton } from "./LoginSkeleton";import { Notification } from "@/components/admin/notification";import { ConfirmationRequired } from "./ConfirmationRequired";+import { SSOAuthButton } from "./SSOAuthButton";export const SignupPage = () => {const queryClient = useQueryClient();const dataProvider = useDataProvider<CrmDataProvider>();- const { darkModeLogo: logo, title } = useConfigurationContext();+ const {+ darkModeLogo: logo,+ title,+ googleWorkplaceDomain,+ } = useConfigurationContext();const navigate = useNavigate();const { data: isInitialized, isPending } = useQuery({queryKey: ["init"],@@ -139,7 +144,7 @@ export const SignupPage = () => {required/></div>- <div className="flex justify-between items-center mt-8">+ <div className="flex flex-col gap-4 justify-between items-center mt-8"><Buttontype="submit"disabled={!isValid || isSignUpPending}@@ -154,6 +159,14 @@ export const SignupPage = () => {"Create account")}</Button>+ {googleWorkplaceDomain ? (+ <SSOAuthButton+ className="w-full"+ domain={googleWorkplaceDomain}+ >+ Sign in with Google Workplace+ </SSOAuthButton>+ ) : null}</div></form></div>diff --git a/src/components/atomic-crm/login/StartPage.tsx b/src/components/atomic-crm/login/StartPage.tsxindex c4584e5e..96ea37aa 100644--- a/src/components/atomic-crm/login/StartPage.tsx+++ b/src/components/atomic-crm/login/StartPage.tsx@@ -1,10 +1,10 @@import { useQuery } from "@tanstack/react-query";import { useDataProvider } from "ra-core";import { Navigate } from "react-router-dom";-import { LoginPage } from "@/components/admin/login-page";import type { CrmDataProvider } from "../providers/types";import { LoginSkeleton } from "./LoginSkeleton";+import { LoginPage } from "./LoginPage";export const StartPage = () => {const dataProvider = useDataProvider<CrmDataProvider>();diff --git a/src/components/atomic-crm/providers/supabase/authProvider.ts b/src/components/atomic-crm/providers/supabase/authProvider.tsindex e41ec187..7eb4e403 100644--- a/src/components/atomic-crm/providers/supabase/authProvider.ts+++ b/src/components/atomic-crm/providers/supabase/authProvider.ts@@ -44,7 +44,7 @@ export async function getIsInitialized() {const isInitialized = data?.at(0)?.is_initialized > 0;if (isInitialized) {- storage?.setItem(IS_INITIALIZED_CACHE_KEY, 'true');+ storage?.setItem(IS_INITIALIZED_CACHE_KEY, "true");}return isInitialized;@@ -86,6 +86,17 @@ function clearCache() {export const authProvider: AuthProvider = {...baseAuthProvider,+ login: async (params) => {+ if (params.ssoDomain) {+ const { error } = await supabase.auth.signInWithSSO({+ domain: params.ssoDomain,+ });+ if (error) {+ throw error;+ }+ }+ return baseAuthProvider.login(params);+ },logout: async (params) => {clearCache();return baseAuthProvider.logout(params);diff --git a/src/components/atomic-crm/root/CRM.tsx b/src/components/atomic-crm/root/CRM.tsxindex 03c80440..5df8cee5 100644--- a/src/components/atomic-crm/root/CRM.tsx+++ b/src/components/atomic-crm/root/CRM.tsx@@ -110,6 +110,7 @@ export const CRM = ({title = defaultTitle,dataProvider = defaultDataProvider,authProvider = defaultAuthProvider,+ googleWorkplaceDomain = import.meta.env.VITE_GOOGLE_WORKPLACE_DOMAIN,disableTelemetry,...rest}: CRMProps) => {@@ -143,6 +144,7 @@ export const CRM = ({noteStatuses={noteStatuses}taskTypes={taskTypes}title={title}+ googleWorkplaceDomain={googleWorkplaceDomain}><ResponsiveAdmindataProvider={dataProvider}diff --git a/src/components/atomic-crm/root/ConfigurationContext.tsx b/src/components/atomic-crm/root/ConfigurationContext.tsxindex 4d6137b9..fc3107fe 100644--- a/src/components/atomic-crm/root/ConfigurationContext.tsx+++ b/src/components/atomic-crm/root/ConfigurationContext.tsx@@ -26,6 +26,7 @@ export interface ConfigurationContextValue {darkModeLogo: string;lightModeLogo: string;contactGender: ContactGender[];+ googleWorkplaceDomain?: string;}export interface ConfigurationProviderProps extends ConfigurationContextValue {@@ -59,6 +60,7 @@ export const ConfigurationProvider = ({taskTypes,title,contactGender,+ googleWorkplaceDomain,}: ConfigurationProviderProps) => (<ConfigurationContext.Providervalue={{@@ -72,6 +74,7 @@ export const ConfigurationProvider = ({title,taskTypes,contactGender,+ googleWorkplaceDomain,}}>{children}