Azure / Azure/static-web-apps

.referrer in responseOverrides redirect is not working for custom urls

Open
#1,398 2 comments 4 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
346
Forks
67
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**

The responseOverrides only replaces the .referrer keyword when the base path is in `/.auth`, if we use any other path, the `.referrer` is not replaced.

I have created a custom page for letting the user select the login provider (Microsoft, GitHub, Google,...). When running the page in the local environment the responseOverrides replaces the .referrer keyword by the referrer page, but when deploying it to the static web app this stops working.

**To Reproduce**
Steps to reproduce the behavior:
Use a response override with a custom page (out of the /.auth path) and add the .referrer keyword to get the referrer in the query:
```json
"responseOverrides": {
"401": {
"statusCode": 302,
"redirect": "/sso?post_login_redirect_uri=.referrer"
}
},
```

**Expected behavior**
Replace the `.referrer` keyword in any responseOverrides configuration with redirect.

**Screenshots**
When using any /.auth path the .referrer keyword is correctly replaced:
![redirect to .auth/login/aad works](https://github.com/Azure/static-web-apps/assets/8036360/d47d42a5-8169-4dd2-ba29-50320db7c8cf)

When using custom path the .referrer keyword is left as is:
![Custom path redirect does not work](https://github.com/Azure/static-web-apps/assets/8036360/358b6818-575e-40b0-854e-24b11c674a31)

When using the local swa emulator, it works as expected replacing the .referrer keyword even for custom pages:
![Custom path redirect in local](https://github.com/Azure/static-web-apps/assets/8036360/09cc7fee-3c6c-48f4-a882-f6494f950fc6)

**Why do I need this?**

I've configured a few different auth providers (aad, google & github) and I want a bridge page to let the user choose its preferred authentication method, just like this:

image

But when using this intermediate page that redirects to the /.auth/login/[provider] , the `.referrer` is not replaced.

Is there any other way to achieve this?

**Partial workaround**

I can store the link using localStorage during the `beforeunload` event:

```js
window.onbeforeunload = function () {
localStorage.setItem('lastPage', document.activeElement.href);
};
```

And then check if the `.referrer` has been replaced or not and use this value, but this only works if the user comes from a link in the same website, it won't work for external links.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.