Skip to content

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

Feat(crm): Replace browserRouter with HashRouter and handle reset callback

Anthony Rimetb7c739eff1af
diff --git a/.env.development b/.env.development
index 293f7aff..1252dd73 100644
--- a/.env.development
+++ b/.env.development
@@ -1,7 +1,6 @@
SKIP_PREFLIGHT_CHECK=true
VITE_SUPABASE_URL=http://127.0.0.1:54321
-VITE_APPLICATION_BASENAME=
VITE_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0
VITE_IS_DEMO=false
SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU
diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
index 1b46f492..63533946 100644
--- a/.github/workflows/deploy.yml
+++ b/.github/workflows/deploy.yml
@@ -24,7 +24,6 @@ jobs:
SUPABASE_PROJECT_ID: ${{ secrets.SUPABASE_PROJECT_ID }}
VITE_SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
VITE_SUPABASE_ANON_KEY: ${{ secrets.SUPABASE_ANON_KEY }}
- VITE_APPLICATION_BASENAME: ${{ vars.VITE_APPLICATION_BASENAME }}
VITE_IS_DEMO: ${{ vars.VITE_IS_DEMO }}
diff --git a/README.md b/README.md
index 3930fb76..761b772c 100644
--- a/README.md
+++ b/README.md
@@ -104,7 +104,6 @@ SUPABASE_ANON_KEY: Your supabase project anonymous key
Also, you will need to configure the some variables:
```bash
-VITE_APPLICATION_BASENAME: The base URL of your application, optional
VITE_IS_DEMO: Set to `true` if you want to display the demo banner
```
diff --git a/public/auth-callback/index.html b/public/auth-callback/index.html
new file mode 100644
index 00000000..ef7d2924
--- /dev/null
+++ b/public/auth-callback/index.html
@@ -0,0 +1,140 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <meta
+ name="viewport"
+ content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no"
+ />
+ <meta name="theme-color" content="#000000" />
+ <link rel="manifest" href="./manifest.json" />
+ <link rel="shortcut icon" href="./favicon.ico" />
+ <title>Atomic CRM</title>
+ <style>
+ body {
+ margin: 0;
+ padding: 0;
+ font-family: sans-serif;
+ }
+
+ .loader-container {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ flex-direction: column;
+ position: absolute;
+ top: 0;
+ bottom: 0;
+ left: 0;
+ right: 0;
+ background-color: #fafafa;
+ }
+
+ /* CSS Spinner from https://projects.lukehaas.me/css-loaders/ */
+
+ .loader,
+ .loader:before,
+ .loader:after {
+ border-radius: 50%;
+ }
+
+ .loader {
+ color: #283593;
+ font-size: 11px;
+ text-indent: -99999em;
+ margin: 55px auto;
+ position: relative;
+ width: 10em;
+ height: 10em;
+ box-shadow: inset 0 0 0 1em;
+ -webkit-transform: translateZ(0);
+ -ms-transform: translateZ(0);
+ transform: translateZ(0);
+ }
+
+ .loader:before,
+ .loader:after {
+ position: absolute;
+ content: '';
+ }
+
+ .loader:before {
+ width: 5.2em;
+ height: 10.2em;
+ background: #fafafa;
+ border-radius: 10.2em 0 0 10.2em;
+ top: -0.1em;
+ left: -0.1em;
+ -webkit-transform-origin: 5.2em 5.1em;
+ transform-origin: 5.2em 5.1em;
+ -webkit-animation: load2 2s infinite ease 1.5s;
+ animation: load2 2s infinite ease 1.5s;
+ }
+
+ .loader:after {
+ width: 5.2em;
+ height: 10.2em;
+ background: #fafafa;
+ border-radius: 0 10.2em 10.2em 0;
+ top: -0.1em;
+ left: 5.1em;
+ -webkit-transform-origin: 0px 5.1em;
+ transform-origin: 0px 5.1em;
+ -webkit-animation: load2 2s infinite ease;
+ animation: load2 2s infinite ease;
+ }
+
+ @-webkit-keyframes load2 {
+ 0% {
+ -webkit-transform: rotate(0deg);
+ transform: rotate(0deg);
+ }
+ 100% {
+ -webkit-transform: rotate(360deg);
+ transform: rotate(360deg);
+ }
+ }
+
+ @keyframes load2 {
+ 0% {
+ -webkit-transform: rotate(0deg);
+ transform: rotate(0deg);
+ }
+ 100% {
+ -webkit-transform: rotate(360deg);
+ transform: rotate(360deg);
+ }
+ }
+ </style>
+ </head>
+
+ <body>
+ <noscript> You need to enable JavaScript to run this app. </noscript>
+ <div id="root">
+ <div class="loader-container">
+ <div class="loader">Loading...</div>
+ </div>
+ </div>
+ </body>
+ <script>
+ const getUrlParams = () => {
+ const urlSearchParams = new URLSearchParams(
+ window.location.hash.substring(1)
+ );
+ const access_token = urlSearchParams.get('access_token');
+ const refresh_token = urlSearchParams.get('refresh_token');
+ const type = urlSearchParams.get('type');
+
+ return { access_token, refresh_token, type };
+ };
+
+ function interceptAuthCallback() {
+ const hash = window.location.hash;
+ const {access_token, refresh_token, type} = getUrlParams();
+ window.location.href = `../?access_token=${access_token}&refresh_token=${refresh_token}&type=${type}#/auth-callback`;
+ }
+
+ // Call the function to intercept the auth callback
+ interceptAuthCallback();
+ </script>
+</html>
diff --git a/src/authProvider.ts b/src/authProvider.ts
index 56276ea3..b8234a4f 100644
--- a/src/authProvider.ts
+++ b/src/authProvider.ts
@@ -58,4 +58,30 @@ export const authProvider: AuthProvider = {
return baseAuthProvider.checkAuth(params);
},
+ handleCallback: async () => {
+ const { access_token, refresh_token, type } = getUrlParams();
+ // Users have reset their password or have just been invited and must set a new password
+ if (type === 'recovery' || type === 'invite') {
+ if (access_token && refresh_token) {
+ return {
+ redirectTo: `/set-password?access_token=${access_token}&refresh_token=${refresh_token}&type=${type}`,
+ };
+ }
+
+ if (process.env.NODE_ENV === 'development') {
+ console.error(
+ 'Missing access_token or refresh_token for an invite or recovery'
+ );
+ }
+ }
+ },
+};
+
+const getUrlParams = () => {
+ const urlSearchParams = new URLSearchParams(window.location.search);
+ const access_token = urlSearchParams.get('access_token');
+ const refresh_token = urlSearchParams.get('refresh_token');
+ const type = urlSearchParams.get('type');
+
+ return { access_token, refresh_token, type };
};
diff --git a/src/root/CRM.tsx b/src/root/CRM.tsx
index 57889d0c..8ccf3b50 100644
--- a/src/root/CRM.tsx
+++ b/src/root/CRM.tsx
@@ -45,7 +45,6 @@ import {
SetPasswordPage,
raSupabaseEnglishMessages,
} from 'ra-supabase';
-import { BrowserRouter } from 'react-router-dom';
// Define the interface for the CRM component props
type CRMProps = {
@@ -136,53 +135,49 @@ export const CRM = ({
taskTypes={taskTypes}
title={title}
>
- <BrowserRouter
- basename={import.meta.env.VITE_APPLICATION_BASENAME || undefined}
+ <Admin
+ dataProvider={dataProvider}
+ authProvider={authProvider}
+ store={localStorageStore(undefined, 'CRM')}
+ layout={Layout}
+ loginPage={LoginPage}
+ dashboard={Dashboard}
+ theme={lightTheme}
+ darkTheme={darkTheme || null}
+ i18nProvider={i18nProvider}
>
- <Admin
- dataProvider={dataProvider}
- authProvider={authProvider}
- store={localStorageStore(undefined, 'CRM')}
- layout={Layout}
- loginPage={LoginPage}
- dashboard={Dashboard}
- theme={lightTheme}
- darkTheme={darkTheme || null}
- i18nProvider={i18nProvider}
- >
- <CustomRoutes noLayout>
- <Route path={SignupPage.path} element={<SignupPage />} />
- <Route
- path={SetPasswordPage.path}
- element={<SetPasswordPage />}
- />
- <Route
- path={ForgotPasswordPage.path}
- element={<ForgotPasswordPage />}
- />
- </CustomRoutes>
- {permissions => (
- <>
- <CustomRoutes>
- <Route
- path={SettingsPage.path}
- element={<SettingsPage />}
- />
- </CustomRoutes>
- <Resource name="deals" {...deals} />
- <Resource name="contacts" {...contacts} />
- <Resource name="companies" {...companies} />
- <Resource name="contactNotes" />
- <Resource name="dealNotes" />
- <Resource name="tasks" list={ListGuesser} />
- {permissions === 'admin' ? (
- <Resource name="sales" {...sales} />
- ) : null}
- <Resource name="tags" list={ListGuesser} />
- </>
- )}
- </Admin>
- </BrowserRouter>
+ <CustomRoutes noLayout>
+ <Route path={SignupPage.path} element={<SignupPage />} />
+ <Route
+ path={SetPasswordPage.path}
+ element={<SetPasswordPage />}
+ />
+ <Route
+ path={ForgotPasswordPage.path}
+ element={<ForgotPasswordPage />}
+ />
+ </CustomRoutes>
+ {permissions => (
+ <>
+ <CustomRoutes>
+ <Route
+ path={SettingsPage.path}
+ element={<SettingsPage />}
+ />
+ </CustomRoutes>
+ <Resource name="deals" {...deals} />
+ <Resource name="contacts" {...contacts} />
+ <Resource name="companies" {...companies} />
+ <Resource name="contactNotes" />
+ <Resource name="dealNotes" />
+ <Resource name="tasks" list={ListGuesser} />
+ {permissions === 'admin' ? (
+ <Resource name="sales" {...sales} />
+ ) : null}
+ <Resource name="tags" list={ListGuesser} />
+ </>
+ )}
+ </Admin>
</ConfigurationProvider>
);
diff --git a/supabase/config.toml b/supabase/config.toml
index 4f8f1153..df13d88e 100644
--- a/supabase/config.toml
+++ b/supabase/config.toml
@@ -71,7 +71,7 @@ enabled = true
# in emails.
site_url = "http://localhost:5173/"
# A list of *exact* URLs that auth providers are permitted to redirect to post authentication.
-additional_redirect_urls = ["https://localhost:5173/auth-callback"]
+additional_redirect_urls = ["https://localhost:5173/auth-callback/index.html"]
# How long tokens are valid for, in seconds. Defaults to 3600 (1 hour), maximum 604,800 (1 week).
jwt_expiry = 3600
# If disabled, the refresh token will never expire.
diff --git a/supabase/templates/recovery.html b/supabase/templates/recovery.html
index 8e9c72ac..e6d73077 100644
--- a/supabase/templates/recovery.html
+++ b/supabase/templates/recovery.html
@@ -60,7 +60,7 @@
<div class="content">
<p>Hello,</p>
<p>To reset your password, follow the link below:</p>
- <a href="{{ .ConfirmationURL }}auth-callback" class="button"
+ <a href="{{ .ConfirmationURL }}auth-callback/index.html" class="button"
>Reset my password</a
>
<p>