How redirect in route component ?
Answered
Pacific herring posted this in #help-forum
Pacific herringOP
I have a small question, do
route.tsx can retrieve the public domain ? Because I have a route which is supposed to redirect the user, but is returning to http://localhost:3000 everytime (in dev and in prod). There is my route :import { createRouteHandlerClient } from '@/lib/supabase/route';
import { NextRequest, NextResponse } from 'next/server';
export const dynamic = 'force-dynamic';
export async function GET(request: NextRequest) {
const requestUrl = new URL(request.url);
try {
const code = requestUrl.searchParams.get('code');
const next = requestUrl.searchParams.get('next') ?? '/';
if (!code) throw new Error('No code provided');
const supabase = createRouteHandlerClient();
const { error } = await supabase.auth.exchangeCodeForSession(code);
if (error) throw error;
// URL to redirect to after sign in process completes
return NextResponse.redirect(new URL(next, requestUrl));
} catch (error) {
console.error(`error [${new Date().toISOString()}]:`, error);
// return the user to the error page
return NextResponse.redirect(new URL('/auth/error', requestUrl));
}
}Answered by Ray
import { NextResponse } from "next/server";
export function GET(req: Request) {
const proto = new URL(req.url).protocol;
const host = req.headers.get("host") ?? "";
const path = "/";
return NextResponse.redirect(`${proto}//${host}${path}`);
}37 Replies
Pacific herringOP
any idea ?
@linesofcode this is the issue mentioned in #discussions, could you continue the discussion here?
@joulev <@414145877335080960> this is the issue mentioned in <#752647196419031042>, could you continue the discussion here?
Pacific herringOP
yeah sry Ive delete the msg in the channel #discussions
Pacific herringOP
maybe the problem come from AWS ?
arrh Idk what can I try now
@Ray what does `new URL(request.url).host` give you?
Pacific herringOP
Like that ?
export async function GET(request: NextRequest) {
const requestUrl = new URL(request.url);
try {
const code = requestUrl.searchParams.get('code');
const next = requestUrl.searchParams.get('next') ?? '/';
if (!code) throw new Error('No code provided');
const supabase = createRouteHandlerClient();
const { error } = await supabase.auth.exchangeCodeForSession(code);
if (error) throw error;
// URL to redirect to after sign in process completes
return NextResponse.redirect(new URL(next, requestUrl.host));
} catch (error) {
console.error(`error [${new Date().toISOString()}]:`, error);
// return the user to the error page
return NextResponse.redirect(new URL('/auth/error', requestUrl.host));
}
}@Ray what does `new URL(request.url).host` give you?
Pacific herringOP
Doing this I have an error
code: 'ERR_INVALID_URL',@Pacific herring Doing this I have an error `code: 'ERR_INVALID_URL',`
yeah it is expected, was going to ask you try to console.log and see how it look like
@Ray yeah it is expected, was going to ask you try to console.log and see how it look like
Pacific herringOP
Its so hard to fix it because I have to push my code to test in AWS each time, its a hell 

@Pacific herring Its so hard to fix it because I have to push my code to test in AWS each time, its a hell <:lolsob:753870958489632819>
what tool do you use to deploy it to aws?
sst?
@Ray what tool do you use to deploy it to aws?
Pacific herringOP
Im new in AWS (just leave Vercel because I have problem with redirect idk why) but Ive just link hte github repo and when I push is automatically update like Vercel
@Ray oh ok, it should be amplify
Pacific herringOP
When accessing to
domain.com/auth/callback in the Vercel hosted version, its working (redirect to domain.com/auth/error) but in AWS version is redirecting to localhost:3000/auth/callback@Pacific herring When accessing to `domain.com/auth/callback` in the Vercel hosted version, its working (redirect to `domain.com/auth/error`) but in AWS version is redirecting to `localhost:3000/auth/callback`
have you tried
NextResponse.redirect(new URL('/auth/error', request.url)?Im trying to deploy a project to it
are you using cloudfront?
@Ray are you using cloudfront?
Pacific herringOP
Heuuu, I guess no ahah
@Ray have you tried `NextResponse.redirect(new URL('/auth/error', request.url)`?
Pacific herringOP
Its what I have in my code yes :
export async function GET(request: NextRequest) {
const requestUrl = new URL(request.url);
try {
const code = requestUrl.searchParams.get('code');
const next = requestUrl.searchParams.get('next') ?? '/';
if (!code) throw new Error('No code provided');
const supabase = createServerClient();
const { error } = await supabase.auth.exchangeCodeForSession(code);
if (error) throw error;
// URL to redirect to after sign in process completes
return NextResponse.redirect(new URL(next, requestUrl));
} catch (error) {
// return the user to the error page
const errorUrl = new URL('/auth/error', requestUrl);
errorUrl.searchParams.set('error', `${error}`);
return NextResponse.redirect(errorUrl);
}
}But actually I dont like AWS, I would love to leave it. But Vercel have weird issue with it too
@Ray no, just
ts
NextResponse.redirect(new URL('/auth/error', request.url)
Pacific herringOP
Liek this ?
export async function GET(request: NextRequest) {
const requestUrl = new URL(request.url);
try {
const code = requestUrl.searchParams.get('code');
const next = requestUrl.searchParams.get('next') ?? '/';
if (!code) throw new Error('No code provided');
const supabase = createServerClient();
const { error } = await supabase.auth.exchangeCodeForSession(code);
if (error) throw error;
// URL to redirect to after sign in process completes
return NextResponse.redirect(new URL(next, request.url));
} catch (error) {
// return the user to the error page
const errorUrl = new URL('/auth/error', request.url);
errorUrl.searchParams.set('error', `${error}`);
return NextResponse.redirect(errorUrl);
}
}Pacific herringOP
Im gonna try !
@Ray yes
Pacific herringOP
still redirect to localhost...
@Ray yes
Pacific herringOP
But only with the AWS version, not with the Vercel version... that so weird ahah
@Pacific herring But only with the AWS version, not with the Vercel version... that so weird ahah
oh yeah, it redirect to localhost:3000 too lol
import { NextResponse } from "next/server";
export function GET(req: Request) {
const proto = new URL(req.url).protocol;
const host = req.headers.get("host") ?? "";
const path = "/";
return NextResponse.redirect(`${proto}//${host}${path}`);
}Answer
But its gonna work for everything hoster right ? Not only AWS ?
@Pacific herring But its gonna work for everything hoster right ? Not only AWS ?
it depends your provider but should work on most of it
@Ray it depends your provider but should work on most of it
Pacific herringOP
Its working !