Skip to content

In "Adding Parameterized Routes to a Next.js App" middleware not redirecting #3

@cmchenry

Description

@cmchenry

Had a problem in "Adding Parameterized Routes to a Next.js App" where accessing the /chats/:chatid route, the login was not redirecting to the callbackUrl (it's not adding the callbackUrl to the path at all).

This was the solution I came up with:

import { NextResponse } from "next/server";
import { auth } from "@/auth";

export const config = {
  matcher: ["/chats/:chatid*"],
};

export default auth((req) => {
  if (!req.auth) {
    const loginUrl = new URL("/api/auth/signin", req.url);
    loginUrl.searchParams.set('callbackUrl', req.url);
    return NextResponse.redirect(loginUrl);
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions