Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct login redirect issues in supabase implemetation x2 #275

Merged
merged 5 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions generate-netlify-env.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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