Skip to content

Commit

Permalink
Merge pull request #10 from marmelab/deep-link-option
Browse files Browse the repository at this point in the history
Provide feature flag to enable deep link redirect
  • Loading branch information
slax57 authored Nov 24, 2023
2 parents 32251be + 3d63045 commit 6ae9763
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
51 changes: 51 additions & 0 deletions packages/ra-auth-msal/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<BrowserRouter>
<Admin
authProvider={authProvider}
dataProvider={dataProvider}
title="Example Admin"
>
<Resource name="posts" {...posts} />
</Admin>
</BrowserRouter>
);
};
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.
Expand Down
31 changes: 21 additions & 10 deletions packages/ra-auth-msal/src/authProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export type MsalAuthProviderParams = {
account: AccountInfo
) => ReturnType<AuthProvider["getIdentity"]>;
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),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
},
};

Expand Down

0 comments on commit 6ae9763

Please sign in to comment.