diff --git a/packages/ra-auth-msal/Readme.md b/packages/ra-auth-msal/Readme.md
index e68658d..1327a92 100644
--- a/packages/ra-auth-msal/Readme.md
+++ b/packages/ra-auth-msal/Readme.md
@@ -378,6 +378,57 @@ const App = () => {
export default App;
```
+
+### `enableDeepLinkRedirect`
+
+You can choose whether the authProvider should redirect to the page the user was visiting once the user has been authenticated (this allows easier URL sharing between users). By default, it is set to `true`.
+
+Note: This features relies on `sessionStorage` and is not available with Server-Side Rendering.
+
+You can disable this feature like this:
+
+```jsx
+// in src/authConfig.js
+export const msalConfig = {
+ // ...
+};
+```
+
+```jsx
+// in src/App.jsx
+import React from 'react';
+import { Admin, Resource } from 'react-admin';
+import { BrowserRouter } from "react-router-dom";
+import { PublicClientApplication } from "@azure/msal-browser";
+import { msalAuthProvider } from "ra-auth-msal";
+import { CustomLoginPage } from "./CustomLoginPage";
+import dataProvider from './dataProvider';
+import posts from './posts';
+import { msalConfig } from "./authConfig";
+
+const myMSALObj = new PublicClientApplication(msalConfig);
+
+const App = () => {
+ const authProvider = msalAuthProvider({
+ msalInstance: myMSALObj,
+ enableDeepLinkRedirect: false,
+ });
+
+ return (
+
+
+
+
+
+ );
+};
+export default App;
+```
+
### `msalHttpClient`
`ra-auth-msal` includes an `msalHttpClient` that can be used to make authenticated requests to your API. This helper automatically adds the `accessToken` to the request headers.
diff --git a/packages/ra-auth-msal/src/authProvider.ts b/packages/ra-auth-msal/src/authProvider.ts
index 7992e9f..b26d9fb 100644
--- a/packages/ra-auth-msal/src/authProvider.ts
+++ b/packages/ra-auth-msal/src/authProvider.ts
@@ -36,9 +36,10 @@ export type MsalAuthProviderParams = {
account: AccountInfo
) => ReturnType;
redirectOnCheckAuth?: boolean;
+ enableDeepLinkRedirect?: boolean;
};
-const MSAL_REDIRECT_KEY = "_ra_msal_key";
+const MSAL_REDIRECT_KEY = "_ra_msal_redirect_key";
/**
* Function that returns an authProvider using the Microsoft Authentication Library (MSAL),
@@ -87,17 +88,25 @@ export const msalAuthProvider = ({
getPermissionsFromAccount = defaultGetPermissionsFromAccount,
getIdentityFromAccount = defaultGetIdentityFromAccount,
redirectOnCheckAuth = true,
+ enableDeepLinkRedirect = true,
}: MsalAuthProviderParams): AuthProvider => {
// We need to set up the redirect handler at a global scope to make sure all redirects are handled,
// otherwise the lib can lock up because a redirect is still marked as pending and has not been handled.
// Besides, we can call this handler again later and still gather the response because it is cached internally.
msalInstance.handleRedirectPromise();
+ const canDeepLinkRedirect =
+ enableDeepLinkRedirect &&
+ typeof window != undefined &&
+ typeof sessionStorage != undefined;
+
const authProvider = {
async login() {
- // We cannot use react-router location here, as we are not in a router context,
- // So we need to fallback to native browser APIs.
- sessionStorage.setItem(MSAL_REDIRECT_KEY, window.location.href);
+ if (canDeepLinkRedirect) {
+ // We cannot use react-router location here, as we are not in a router context,
+ // So we need to fallback to native browser APIs.
+ sessionStorage.setItem(MSAL_REDIRECT_KEY, window.location.href);
+ }
// Used when the redirection to the MS login form is done from a custom login page
msalInstance.loginRedirect(loginRequest);
@@ -160,12 +169,14 @@ export const msalAuthProvider = ({
}
msalInstance.setActiveAccount(account);
- // We cannot use react-router redirect here, as we are not in a router context,
- // So we need to fallback to native browser APIs.
- const redirectUrl =
- sessionStorage.getItem(MSAL_REDIRECT_KEY) ?? window.location.origin;
- window.location.replace(redirectUrl);
- sessionStorage.removeItem(MSAL_REDIRECT_KEY);
+ if (canDeepLinkRedirect) {
+ // We cannot use react-router redirect here, as we are not in a router context,
+ // So we need to fallback to native browser APIs.
+ const redirectUrl =
+ sessionStorage.getItem(MSAL_REDIRECT_KEY) ?? window.location.origin;
+ window.location.replace(redirectUrl);
+ sessionStorage.removeItem(MSAL_REDIRECT_KEY);
+ }
},
};