How to redirect to the URL originally tried to access in Static Web Apps?

matsunotsuma 20 Reputation points
2024-09-19T22:24:19.8466667+00:00

I'm developing React Web Site with SWA.

And I use Entra ID for authentication.

My site can redirect to log in by staticwebapp.config.json like below,

    "responseOverrides": {
        "401": {
        "statusCode": 302,
        "redirect": "/.auth/login/aad"
        }
    }

but after logged in, redirect to route address of my site.

But I want to be redirected to the URL originally tried to access.

For example,

current situation:

https://mysite.com/authenticated/address

https://mysite.com/.auth/login/aadhttps://mysite.com

desired outcome:

https://mysite.com/authenticated/address

https://mysite.com/.auth/login/aadhttps://mysite.com/authenticated/address

How can I this?

Best Regard.

Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

1 answer

Sort by: Most helpful
  1. SnehaAgrawal-MSFT 22,721 Reputation points Moderator
    2024-09-23T10:53:07.9+00:00

    @matsunotsuma Thanks for asking question! If you want to use post authentication to redirect the user to where he/she intended to go? If so, you may try this- You can only use it in a response override:

    {
      "routes": [
        {
          "route": "/*",
          "allowedRoles": [
            "authenticated"
          ]
        }
      ],
      "responseOverrides": {
        "401": {
          "statusCode": 302,
          "redirect": "/.auth/login/aad"
        }
      }
    }
    

    Let us know

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.