I am new to react and so this question might sound silly. I am developing a react application with back end as asp.net core. I want to have two roles in the application as role A and role B which I am setting up in AAD. After authentication I want the role of the user to be available to react component. What is the best way of passing this information or reading this information in front end may be inside a react component. DOM is getting rendered before I fetch the user information from controller.
I tried using mobx storage to store the data from controller. But the issue I am facing now is the DOM gets rendered before the call gets completed which I don't want. How can I wait for the api call to get completed before DOM is rendered?
Here ReactDOM.render does not wait for stores[USERSTORE].setRole() to complete. If I don't use async await I get promise pending and the response is undefined.
boot.tsx
stores[USERSTORE].setRole();
ReactDOM.render(
<div>"Dome content"</div>,
root
);
service.ts
const client = AxiosAuthClient.createClient('/api/auth');
export const authService =
{
GetUserRole: async() => {
const x = await client.get(`userinfo`);
return x;
},
};
userstore.ts
import { observable, action } from 'mobx';
import { authService } from '../api/AuthService';
import { AxiosResponse, AxiosError } from 'axios';
export class UserStore {
public constructor() {
this.isApprover = false;
}
@observable public isApprover: boolean = false;
@action
public setRole = () => {
authService.GetUserRole()
.then((res: AxiosResponse) => {
this.setUserRole(res.data);
})
.catch((err: AxiosError) => {
const response = err.response;
console.log(response.data.errors)
});
}
@action
public setUserRole = (data : boolean) => {
this.isApprover = data;
}
}
controller
[Route("api/auth")]
public class AuthController : Controller
{
[HttpGet]
[Route("userinfo")]
public async Task<IActionResult> Index()
{
var result = await getrole();
return Ok(result);
}
public async Task<bool> getrole()
{
return true;
}
}