【发布时间】:2020-05-06 15:21:41
【问题描述】:
如何正确地将 MSAL 与 React-Admin 集成。
我使用了 Microsoft 提供的代码并将其放入 App.js 中的构造函数中,并且使用重定向方法可以正常工作,只是在重定向到 MS 身份验证之前,我会在瞬间获得默认的 React-Admin 登录屏幕页面。
如果我将 MSAL 代码放在我的自定义登录页面(空页面)中,它会进入循环并且身份验证不起作用。
如何摆脱 React-Admin 登录屏幕?
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;
【问题讨论】:
-
你能用代码示例更新你的帖子吗?
-
我已更新帖子以包含我在 App.js 中使用的 MSAL 代码。该应用程序需要在 Azure AD 中注册,但为了工作,配置位于单独的 confg.js 字段中
标签: react-admin msal