Issue with regex matcher middleware
Answered
Saint Hubert Jura Hound posted this in #help-forum
Saint Hubert Jura HoundOP
I'm having some trouble creating a regex for a matcher in my middleware.
I'm using regexr to test and it seems to work as expected. (screenshot)
My goal is to implement a way for users to be redirected to the original page they were trying to visit while unauthenticated after being directed to the login page.
I tried doing this with query params until i realized theres no way to get the current url in a server component so i cant pass it to the login page or i'd have to convert multiple server components to client components which i dont wanna do.
So now i'm trying to use middleware. I want the user to be redirected to the login page if they have no valid session and are trying to access one of the routes not excluded by the matcher.
This is the regex for the matcher that im trying to use:
The only difference between this one regex the one im testing using regexr is the starting slash /. This causes it to not match on any route.
Any help would be appreciated
Or if you know of an easier way to redirect users back to the page they were trying to access after successful login please do share, though im using supabase auth which uses a callback route so i do think i need query params:
I'm using regexr to test and it seems to work as expected. (screenshot)
My goal is to implement a way for users to be redirected to the original page they were trying to visit while unauthenticated after being directed to the login page.
I tried doing this with query params until i realized theres no way to get the current url in a server component so i cant pass it to the login page or i'd have to convert multiple server components to client components which i dont wanna do.
So now i'm trying to use middleware. I want the user to be redirected to the login page if they have no valid session and are trying to access one of the routes not excluded by the matcher.
This is the regex for the matcher that im trying to use:
export const config = {
matcher: [
{
source: "/(^(?!/plans(?:/|$))(?!/(?:login|api|favicon.ico))(?!/$)/.*)",
},
],
}The only difference between this one regex the one im testing using regexr is the starting slash /. This causes it to not match on any route.
Any help would be appreciated
Or if you know of an easier way to redirect users back to the page they were trying to access after successful login please do share, though im using supabase auth which uses a callback route so i do think i need query params:
import { createSupabaseServerClient } from "@/utils/supabase/server"
import { NextRequest, NextResponse } from "next/server"
export async function GET(request: NextRequest) {
const { searchParams, origin } = new URL(request.url)
const code = searchParams.get("code")
const next = searchParams.get("next") ?? "/"
...
// URL to redirect to after sign in process completes
return NextResponse.redirect(origin) // next
}Answered by Saint Hubert Jura Hound
okay figured out how to do it, i set a matcher that excludes the login page on the middleware, set a cookie to request.url, then on the login page get the cookie and prop drill it down to the login action
3 Replies
American
'use server';
import { headers } from 'next/headers';
// inside auth
const referer = headers().get('referer');
// your auth logic
return redirect(referer || '/dashboard/overview');note that if you also did redirect on error, original referer value will be lost.
@American
'use server';
import { headers } from 'next/headers';
// inside auth
const referer = headers().get('referer');
// your auth logic
return redirect(referer || '/dashboard/overview');
note that if you also did redirect on error, original referer value will be lost.
Saint Hubert Jura HoundOP
hi thank you for helping! i tried getting the referer in my auth callback but its always just
i assume its because im using supabase and using the signInWithOAuth function redirects to a callback api, which is the code snippet i posted above, and sets the
http://localhost:3000/.i assume its because im using supabase and using the signInWithOAuth function redirects to a callback api, which is the code snippet i posted above, and sets the
next search param and referer to the default redirect url configured in the supabase dashboardSaint Hubert Jura HoundOP
okay figured out how to do it, i set a matcher that excludes the login page on the middleware, set a cookie to request.url, then on the login page get the cookie and prop drill it down to the login action
Answer