Skip to content

Commit

Permalink
chore(app): add dinamyc routes for env redirection auth
Browse files Browse the repository at this point in the history
  • Loading branch information
GeovannyGil committed Oct 2, 2024
1 parent bcd6ee8 commit a02f7df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
20 changes: 8 additions & 12 deletions apps/app/src/app/api/auth/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { getURL } from "@/lib/url";
import { createClient } from "@v1/supabase/server";
import { NextResponse } from "next/server";

export async function GET(request: Request) {
const { searchParams, origin } = new URL(request.url);
const { searchParams } = new URL(request.url);
const code = searchParams.get("code");
const next = searchParams.get("next") ?? "/";

if (code) {
const supabase = createClient();
const { error } = await supabase.auth.exchangeCodeForSession(code);
if (!error) {
const forwardedHost = request.headers.get("x-forwarded-host");
const isLocalEnv = process.env.NODE_ENV === "development";

if (isLocalEnv) {
return NextResponse.redirect(`${origin}${next}`);
}
if (forwardedHost) {
return NextResponse.redirect(`https://${forwardedHost}${next}`);
}
return NextResponse.redirect(`${origin}${next}`);
const baseUrl = getURL();
return NextResponse.redirect(
`${baseUrl}${next.startsWith("/") ? next.slice(1) : next}`,
);
}
}

return NextResponse.redirect(`${origin}?error=auth-code-error`);
const baseUrl = getURL();
return NextResponse.redirect(`${baseUrl}?error=auth-code-error`);
}
11 changes: 11 additions & 0 deletions apps/app/src/lib/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const getURL = () => {
let url =
process?.env?.NEXT_PUBLIC_SITE_URL ?? // Set this to your site URL in production env.
process?.env?.NEXT_PUBLIC_VERCEL_URL ?? // Automatically set by Vercel.
"http://localhost:3000/";
// Make sure to include `https://` when not localhost.
url = url.startsWith("http") ? url : `https://${url}`;
// Make sure to include a trailing `/`.
url = url.endsWith("/") ? url : `${url}/`;
return url;
};

0 comments on commit a02f7df

Please sign in to comment.