1

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;
    }
}
0

2 Answers 2

2

You have varioius choices, some of the most popular:

  1. Use a global store like Redux (https://redux.js.org/)
  2. Store credentials in upper component (lift state up https://reactjs.org/docs/lifting-state-up.html)
  3. Store credential in localStorage (https://developer.mozilla.org/it/docs/Web/API/Storage/setItem)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. I am using Mobx store for storing user information. But my question basically is how should I get the user claims in the front end application. Is it a good idea to have a Auth controller and then get the information by calling this API in react.
If you do SSR (server side rendering) you can actually print those credentials in a global constant object. If you do not do SSR you can fetch credentials after auth (or aggregated with auth credentials) and store it before bootstrapping the react app.
0

Store your critical data in localStorage rather than in redux. User credentials is critical data in our app. Upon browser refresh, our app's state gets initialized.


It's maybe required to you throughout in your app. So don't lose it in any case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.