Skip to content
Docs
Usage guide
Redirect Helper Functions

Redirect Helper Functions

This library provides a set of helper functions to simplify handling common redirect scenarios.

These redirect functions make it easier to create a redirect response in Next.js Middleware (opens in a new tab).

For example, here’s how the redirectToPath helper function works:

export function redirectToPath(
  request: NextRequest,
  path: string,
  options: RedirectToPathOptions = {shouldClearSearchParams: false}
) {
  const url = request.nextUrl.clone();
  url.pathname = path;
 
  if (options.shouldClearSearchParams) {
    url.search = '';
  }
 
  return NextResponse.redirect(url);
}

It’s a straightforward function that builds a NextResponse object for handling redirects.

Note: You don’t have to use the library's redirect functions—you can always implement your own redirect logic if it better suits your needs.

redirectToPath

Example usage

import {redirectToPath} from 'next-firebase-auth-edge';
redirectToPath(request, '/dashboard', {shouldClearSearchParams: true});

redirectToHome

redirectToHome is a simplified version of redirectToPath that redirects the user to the home page (/).

import {redirectToHome} from 'next-firebase-auth-edge';
redirectToHome(request);

redirectToLogin

redirectToLogin redirects unauthenticated users to a public login page. It will skip the redirect if the request matches one of the paths listed in publicPaths.

import {redirectToLogin} from 'next-firebase-auth-edge';
redirectToLogin(request, {
  path: '/sign-in',
  publicPaths: ['/sign-in', '/register', /^\/post\/(\w+)/]
});