marmelab / ra-auth-msal

An auth provider for react-admin which handles authentication using the MSAL
MIT License
4 stars 2 forks source link

Documentation on getIdentityFromAccount #8

Open dnk8n opened 1 year ago

dnk8n commented 1 year ago

From what I understand, passing a function as getIdentityFromAccount to msalAuthProvider, from this library, sets an identity object which can later be referenced. Am I right in that assessment? If so, how do we reference what we set downstream?

For example, in getIdentityFromAccount function, could I call my API once to get extra database attributes, so that these don't have to be managed separately or repeatedly called, e.g. employee ID (in my database, employee IDs are different to user IDs, which are the same as in Active Directory).

My intended use case is to pass this value to dropdowns, so that logged in user's preferences are prioritized to the top (this I know how to do, and am currently needing to save details to localStorage, which I think by using this feature I can avoid).

dnk8n commented 1 year ago

As usual, I have an Aha moment right after posting the question.

I think I understand, but maybe to less experienced React users like me, it could be made a bit more explicit (maybe just a mention/link to the appropriate docs for react-admin).

What I understand is that instead of passing, as I have done:

const baseAuthProvider = msalAuthProvider({
  msalInstance,
  loginRequest,
  tokenRequest,
  redirectOnCheckAuth: true,
});

export const authProvider = {
  ...baseAuthProvider,
  getPermissions: async () => { ... },
  getIdentity: async () => {
    return await getEmployeeProfile();
  },
};

I can do this, and it results in the same effect:

export const authProvider = msalAuthProvider({
  msalInstance,
  loginRequest,
  tokenRequest,
  getPermissionsFromAccount: async () => { ... },
  getIdentityFromAccount: async () => {
    return await getEmployeeProfile();
  }
  redirectOnCheckAuth: true,
});

I therefore retrieve these attributes via React-Admin's useGetIdentity.

If there is nothing extra to add, I guess this can be closed. A very experienced user would have made the connection immediately.

slax57 commented 1 year ago

Once again thank you for your feedback, and sorry for the delay of our answer (holidays... :palm_tree: :innocent: )

I guess we could add a line in the doc explaining that the result of the getIdentityFromAccount function can later be accessed with RA's useGetIdentity.

Same goes for the getPermissionsFromAccount function, for which the provided example does not really show how to use the different permissions.

I'll mark this as a documentation issue.