# withMiddlewareAuthRequired() not redirecting to custom login route (Next.js)

**URL:** <https://community.auth0.com/t/withmiddlewareauthrequired-not-redirecting-to-custom-login-route-next-js/181481>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [February 5, 2025, 2:10pm UTC](https://community.auth0.com/t/withmiddlewareauthrequired-not-redirecting-to-custom-login-route-next-js/181481 "2025-02-05T14:10:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![solarpathic](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/solarpathic/32/70983_2.png) [@solarpathic](https://community.auth0.com/u/solarpathic)\
**Post date:** [February 5, 2025, 2:10pm UTC](https://community.auth0.com/t/withmiddlewareauthrequired-not-redirecting-to-custom-login-route-next-js/181481/1 "2025-02-05T14:10:17Z")

</div>

```auto
import { withMiddlewareAuthRequired, getSession } from "@auth0/nextjs-auth0/edge";
import { NextRequest, NextResponse } from "next/server";

export default withMiddlewareAuthRequired(async function middleware(req: NextRequest) {
    const res = NextResponse.next();
    const session = await getSession(req, res);

    if (!session) {
        // Redirect user to the login API route if not authenticated
        return NextResponse.redirect(new URL("/api/v1/auth/login", req.url));
    }

    return res;
});

export const config = {
    matcher: ["/topics/:path*", "/chats/:path*", "/profile", "/settings", "/home"],
};

```

Whenever the session expires, I am always redirected to the following URL instead of ‘/api/v1/auth/login’:

{baseURL}/api/auth/login?returnTo=%2Fchats

I’ve tried a bunch of different workarounds, but nothing works.

Any ideas of how to fix this?

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [February 5, 2025, 8:36pm UTC](https://community.auth0.com/t/withmiddlewareauthrequired-not-redirecting-to-custom-login-route-next-js/181481/3 "2025-02-05T20:36:21Z")

</div>

Hi @solarpathic

Welcome to the Auth0 Community!

Please try the following when redirecting the user:

```auto
return NextResponse.redirect(`${baseURL}/api/v1/auth/login`, req.url);

```

or

```auto
return NextResponse.redirect(process.env.loginURL, req.url);

```

Otherwise, if the user is redirected to the `/login?`, are they able to authenticate through that page or not?

Let me know if this helps.

Kind Regards,  
Nik

---

<div class="post-metadata">

**Author:** ![solarpathic](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/solarpathic/32/70983_2.png) [@solarpathic](https://community.auth0.com/u/solarpathic)\
**Post date:** [February 5, 2025, 8:50pm UTC](https://community.auth0.com/t/withmiddlewareauthrequired-not-redirecting-to-custom-login-route-next-js/181481/4 "2025-02-05T20:50:30Z")

</div>

That doesn’t work as it goes right back to ‘/api/auth/login’.

That line is synonymous with what I already have.

‘/login?’ doesn’t work because my API routing is setup as ‘/api/v1/auth/login’, not ‘/api/auth/login’.

I suppose I could just make my auth have the path of ‘/api/auth/’ instead of ‘/api/v1/auth’, but I would like to keep the auth in the v1 directory.

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [February 5, 2025, 9:20pm UTC](https://community.auth0.com/t/withmiddlewareauthrequired-not-redirecting-to-custom-login-route-next-js/181481/5 "2025-02-05T21:20:43Z")

</div>

Hi @solarpathic

I am not quite sure why it redirects to a different path then the one declared in the redirect. Perhaps, somewhere in your implementation the API routing is not set up correctly or your logic overrides the one you set up.

I would recommend to visit the following pages since they might help you in doublechecking your implementation:

- [Github page form `middleware.ts` in NextAuth.JS](https://github.com/nextauthjs/next-auth/blob/8853000fd524b074f82ad38c57671fe1f640172f/packages/next-auth/src/next/middleware.ts#L140)
- [Github page for withMiddlewareAuthRequired() helper](https://github.com/auth0/nextjs-auth0/blob/main/src/helpers/with-middleware-auth-required.ts#L116)
- [Github discussion regarding redirection using NextAuth](https://github.com/nextauthjs/next-auth/discussions/4882)

If by any chance the information above was helpful, feel free to leave a reply to let us know!  
In the meantime, I will be investing the matter as well and come back with an update as soon as possible.

Kind Regards,  
Nik

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [February 19, 2025, 9:21pm UTC](https://community.auth0.com/t/withmiddlewareauthrequired-not-redirecting-to-custom-login-route-next-js/181481/6 "2025-02-19T21:21:12Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
