Skip to content

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

fix: move Supabase login/logout into the remote init script

erwanMarmelab527f9da86aaf
diff --git a/doc/src/content/docs/index.mdx b/doc/src/content/docs/index.mdx
index e36812e3..bba6e6c7 100644
--- a/doc/src/content/docs/index.mdx
+++ b/doc/src/content/docs/index.mdx
@@ -310,6 +310,8 @@ make supabase-remote-init
The script will ask you for the Supabase instance name, create the database, apply the migrations and deploy the edge functions. Finally, it will create a `.env.production.local` file with your remote Supabase configuration.
+If the script logs you into the Supabase CLI, it creates an access token that never expires: see [Supabase Configuration](./developers/supabase-configuration) to revoke it or to use a token that expires.
+
The frontend of the CRM is a Single-Page App that can be deployed to any CDN. Since you have forked the Atomic CRM repository on GitHub, you can use GitHub Pages to host the frontend.
First, build the frontend bundle with:
diff --git a/makefile b/makefile
index 1b48fc7d..99fbab91 100644
--- a/makefile
+++ b/makefile
@@ -98,11 +98,7 @@ prod-deploy: build supabase-deploy
npm run ghpages:deploy
supabase-remote-init:
- if ! npx supabase projects list >/dev/null 2>&1; then \
- trap 'npx supabase projects list >/dev/null 2>&1 && npx supabase logout --yes && echo "The Supabase CLI login created a personal access token that never expires. Revoke it at https://supabase.com/dashboard/account/tokens"' EXIT; \
- trap 'exit 130' INT TERM; \
- fi; \
- npm run supabase:remote:init && $(MAKE) supabase-deploy
+ npm run supabase:remote:init
supabase-deploy:
npx supabase db push
diff --git a/scripts/supabase-remote-init.mjs b/scripts/supabase-remote-init.mjs
index 83ad4958..ea062b68 100644
--- a/scripts/supabase-remote-init.mjs
+++ b/scripts/supabase-remote-init.mjs
@@ -3,7 +3,50 @@ import { execa } from "execa";
import fs from "node:fs";
(async () => {
- await loginToSupabase();
+ // Only log out if we logged in: keep an existing login (or SUPABASE_ACCESS_TOKEN) untouched
+ const shouldLogout = !(await isLoggedIn());
+ if (shouldLogout) {
+ for (const [signal, code] of [
+ ["SIGHUP", 129],
+ ["SIGINT", 130],
+ ["SIGQUIT", 131],
+ ["SIGTERM", 143],
+ ]) {
+ process.on(signal, () => logout().finally(() => process.exit(code)));
+ }
+ await execa("npx", ["supabase", "login"], { stdio: "inherit" });
+ }
+ try {
+ await initRemoteProject();
+ } finally {
+ if (shouldLogout) await logout();
+ }
+})();
+
+async function isLoggedIn() {
+ const { exitCode } = await execa(
+ "npx",
+ ["--yes", "supabase", "projects", "list"],
+ { stdout: "ignore", stderr: "ignore", reject: false },
+ );
+ return exitCode === 0;
+}
+
+let logoutPromise;
+function logout() {
+ logoutPromise ??= execa("npx", ["supabase", "logout", "--yes"], {
+ stdout: "ignore",
+ reject: false,
+ }).then(() => {
+ // eslint-disable-next-line no-console
+ console.log(
+ "The Supabase CLI login created a personal access token that never expires. Revoke it at https://supabase.com/dashboard/account/tokens",
+ );
+ });
+ return logoutPromise;
+}
+
+async function initRemoteProject() {
const projectName = await input({
message: "Enter the name of the project:",
default: "Atomic CRM",
@@ -48,10 +91,11 @@ import fs from "node:fs";
projectRef,
publishableKey,
});
-})();
-async function loginToSupabase() {
- await execa("npx", ["supabase", "login"], { stdio: "inherit" });
+ // Deploy here rather than in the makefile, so it runs before the logout
+ await execa("npx", ["supabase", "functions", "deploy"], {
+ stdio: "inherit",
+ });
}
async function createProject({