Reputation: 333
How do I integrate MSAL with React-Admin properly.
I used the code provided by Microsoft and put it in the Constructor in App.js and it works fine using a redirect method, except that I keep getting a default React-Admin login screen for split second before it redirects to MS authentication page.
If I put the MSAL code in my custom login page (empty page), it goes into a loop and authentication does not work.
How do I get rid of the React-Admin login screen?
import { UserAgentApplication } from 'msal';
class App extends Component {
constructor(props) {
super(props);
this.userAgentApplication = new UserAgentApplication({
auth: {
clientId: config.appId,
authority: config.authEndPoint,
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: true
}
});
this.userAgentApplication.handleRedirectCallback(this.authCallback.bind(this));
var user = this.userAgentApplication.getAccount();
if (user != null) {
localStorage.setItem('token', user.idToken);
localStorage.setItem('userName', user.userName);
const userName = user.userName.toString();
fetch('http://localhost:5000/getuserid', {
mode: 'cors',
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'USERNAME': userName
},
}).then(res => res.json())
.then(res => {
if (res.id != null) {
localStorage.setItem('userID', res.id);
} else {
console.log('Failed to retreived id');
}
}).catch(
err => console.log(err))
}
this.state = {
isAuthenticated: (user !== null),
user: {},
error: null
};
if (user) {
// Enhance user object with data from Graph
//this.getUserProfile();
}
else {
const loginRequest = {
scopes: ["https://graph.microsoft.com/User.Read"]
}
this.userAgentApplication.loginRedirect(loginRequest);
}
}
authCallback(error, response) {
//handle redirect response
this.setState({
authenticated: true
});
}
render() {
return (
<Admin title="MyApp" >
...
</Admin>
);
}
}
export default App;
Upvotes: 0
Views: 2168
Reputation: 934
I worked a bit on your example and I have a solution. I have read mostly React-Admin Tutorial https://marmelab.com/react-admin/Tutorial.html but also a custom React AAD MSAL library docs https://www.npmjs.com/package/react-aad-msal#react-aad-msal
Please add such a package to your project via docs example.
My code for App.js:
import React from 'react';
import ReactDOM from 'react-dom';
import {Admin, ListGuesser, Resource} from 'react-admin';
import jsonServerProvider from 'ra-data-json-server';
import { AzureAD } from 'react-aad-msal';
// import App from './App';
import { authProvider } from './authProvider';
const dataProvider = jsonServerProvider('https://jsonplaceholder.typicode.com');
const AdminApp = () => (
<Admin dataProvider={dataProvider}>
<Resource name="users" list={ListGuesser}/>
</Admin>
);
const App = () => (
<AzureAD provider={authProvider} forceLogin={true}>
<AdminApp />
</AzureAD>
);
export default App;
And for authProvider.js
// authProvider.js
import { MsalAuthProvider, LoginType } from 'react-aad-msal';
// Msal Configurations
const config = {
auth: {
authority: 'https://login.microsoftonline.com/MY_TENANT_ID/',
clientId: 'MY_APP_ID',
redirectUri: 'http://localhost:3000/'
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: true
}
};
// Authentication Parameters
const authenticationParameters = {
scopes: [
'User.Read'
]
}
// Options
const options = {
loginType: LoginType.Redirect,
tokenRefreshUri: window.location.origin + '/auth.html'
}
export const authProvider = new MsalAuthProvider(config, authenticationParameters, options)
That solution displays react-admin only when MS Authentication is successful. It's not using the default Login page at all.
Next step probably here will be merging that solution with using react-admin logout button based on the following docs: https://marmelab.com/react-admin/doc/2.8/Authentication.html#customizing-the-login-and-logout-components and how to call the following MSAL logout function: https://www.npmjs.com/package/react-aad-msal#azuread-component
Upvotes: 2