Redirect to Localhost?

I might be missing something simple but doesn't hurt to ask. When I login to my next app, I get sent to localhost:6927 instead of my root URL. Same thing when I log out. I moved from over from Vercel and didn't have that problem over there. I can provide more context if needed but didn't see anything in the docs or search here at first pass. Cheers!
17 Replies
Percy
Percy12mo ago
Project ID: d46c2eae-5314-40fd-9a01-8b3167e2d2a9
trippester
trippester12mo ago
4caf4458-3b73-4ec2-b116-35df358f1cd1 - Project ID - UPDATED Wait, thats the wrong one...
Brody
Brody12mo ago
vercel would have been monkey patching that localhost redirect for you, Railway in turn runs your code as is
trippester
trippester12mo ago
It's just a redirect to "./" not sure how I can mess that up...
Brody
Brody12mo ago
you need to find a way to specify the correct url for use in redirects
trippester
trippester12mo ago
I did have export const dynamic = 'force-dynamic'; due for vercel to work... import { redirect } from 'next/navigation'; import Header from '@/components/Header'; import NavBar from '@/components/Navbar'; import ClassBanner from '@/components/ClassBanner'; import { getCurrentUser } from '@/lib/utils/getCurrentUser'; // Vercel fails to build if this is not exported export const dynamic = 'force-dynamic'; export default async function MainLayout({ children }: { children: React.ReactNode }) { const currentUser = await getCurrentUser(); if (!currentUser) { redirect('/login'); } return ( <div className="w-full h-full bg-[#1B2026]"> <div className=""> <ClassBanner /> <Header user={currentUser} /> <NavBar /> </div> <div className="w-full h-full pt-[95px] md:pt-[105px]">{children}</div> </div> ); }
Brody
Brody12mo ago
I think next may have an environment variable you can set to the correct url so the redirects use the proper domain
trippester
trippester12mo ago
Cool. Let me check that. Why would it ever go to localhost? I can't figure this out. I've updated the code but to no avail. import { createSupabaseRouteHandlerClient } from '@/lib/utils/getServerSupabaseClient'; import { NextResponse } from 'next/server'; export const dynamic = 'force-dynamic'; export async function POST(request: Request) { const requestUrl = new URL(request.url); const formData = await request.formData(); const email = String(formData.get('email')); const password = String(formData.get('password')); const supabase = createSupabaseRouteHandlerClient(); const { error } = await supabase.auth.signInWithPassword({ email, password, }); // Use BASE_URL if set, otherwise fallback to requestUrl.origin const baseUrl = process.env.BASE_URL || requestUrl.origin; if (error) { return NextResponse.redirect(${baseUrl}/login?error=Could not authenticate user, { // a 301 status is required to redirect from a POST to a GET route status: 301, }); } return NextResponse.redirect(baseUrl, { // a 301 status is required to redirect from a POST to a GET route status: 301, }); } Set the env on railway. Will not correctly direct the user. I know there is a workaround I am missing but I like the idea of the server having this protection built in as a patch if needed.
Brody
Brody12mo ago
I dislike how vercel and other similar services patch away code issues. your app sits behind a proxy, so as far as your next app is aware the requests are coming from a local address, you need to find a way to tell next the correct domain to redirect to
trippester
trippester12mo ago
I'm not sure its a user error if the server is the local host serving to a url. The server knows both of these things.
Brody
Brody12mo ago
user error was the wrong term, code issue is more correct
trippester
trippester12mo ago
To each their own. I look for something else.
Brody
Brody12mo ago
you'd need to find a way to do these redirects with a url you specify I don't think you can access environment variables in next if they aren't prefixed with NEXT_ so that baseUrl variable will always default to the requestUrl.origin, aka localhost
trippester
trippester12mo ago
That wasn't the issue. It's not hitting the API to execute the login/logout function. It gets the url from the env fine. Also, my page loads are 4x longer now on railway. 5.9seconds vs 1.2 before. I wanted to go server-based since the next feature requires sockets...
Brody
Brody12mo ago
not hitting the api
can you explain a little bit more about this? what errors are you getting?
4x page load times
are you running next in production mode? are you located far away from railways default region of us-west1?
trippester
trippester12mo ago
I'm East Coast (NC). It's fine. I ultimately need to move this to AWS GovCloud but I hate working with AWS directly in dev. Full day spent trying to get this fixed. localhost on a server should never return "localhost" I'm sure I'll have issues over GC as well but I'll deal with it on the server.
Brody
Brody12mo ago
railway runs your code as is, it's up to your code to trust the proxy headers or up to you to set the correct environment variables
Want results from more Discord servers?
Add your server