Skip to content

Commit

Permalink
fix: correct login redirect issues in supabase implemetation x2 (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
0-vortex committed Aug 28, 2022
1 parent 288cc9e commit 0239abd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
23 changes: 23 additions & 0 deletions generate-netlify-env.js
@@ -0,0 +1,23 @@
const fs = require("fs");

// check if script is running in netlify CI
if (process.env.CI === "true") {
console.log("Running in Netlify CI environment");
let base = process.env.URL;

if (process.env.CONTEXT === "deploy-preview") {
console.log("Running in Netlify deploy-preview environment");

base = `${process.env.DEPLOY_PRIME_URL}/`;
} else {
if (process.env.CHANNEL !== undefined && ["alpha", "beta"].includes(process.env.CHANNEL)) {
console.log(`Running in Netlify ${process.env.CHANNEL} environment`);

const {protocol, hostname} = new URL(process.env.URL);
base = `${protocol}//${process.env.CHANNEL}.${hostname}/`;
}
}

// write BASE_URL env variable to .env file
fs.writeFileSync(".env.local", `NEXT_PUBLIC_BASE_URL=${base}`);
}
15 changes: 1 addition & 14 deletions lib/hooks/useSupabaseAuth.ts
Expand Up @@ -3,19 +3,6 @@ import { supabase } from "../utils/supabase";
import { User } from "@supabase/supabase-js";
import { UserCredentials } from "@supabase/gotrue-js/src/lib/types";

let base = "http://localhost:3000";

if (process.env.CONTEXT === "deploy-preview") {
base = `${process.env.DEPLOY_PRIME_URL}/`;
} else {
if (process.env.CHANNEL !== undefined && ["alpha", "beta"].includes(process.env.CHANNEL)) {
const {protocol, hostname} = new URL(process.env.URL ?? "/");
base = `${protocol}//${process.env.CHANNEL}.${hostname}/`;
}
}

console.log(base);

const useSupabaseAuth = () => {
const [user, setUser] = useState<User | null>(null);

Expand All @@ -34,7 +21,7 @@ const useSupabaseAuth = () => {

return {
signIn: (data: UserCredentials) => supabase.auth.signIn(data, {
redirectTo: base
redirectTo: process.env.NEXT_PUBLIC_BASE_URL ?? "/"
}),
signOut: () => supabase.auth.signOut(),
user
Expand Down
3 changes: 3 additions & 0 deletions netlify.toml
@@ -1,6 +1,9 @@
[build.environment]
NODE_VERSION = "16"

[context.alpha]
environment = { CHANNEL = "alpha" }

[context.beta]
environment = { CHANNEL = "beta" }

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -28,6 +28,7 @@
"prestart": "npx @open-sauced/check-engines",
"push": "npx @open-sauced/conventional-commit",
"dev": "next dev",
"prebuild": "node generate-netlify-env.js",
"build": "next build",
"start": "next start",
"test": "jest",
Expand Down

0 comments on commit 0239abd

Please sign in to comment.