MrHertal / react-admin-amplify

AWS Amplify data provider for react-admin.
MIT License
159 stars 42 forks source link

Getting access to Amplify / Cognito Auth.currentAuthenticatedUser()? #78

Closed weisisheng closed 1 year ago

weisisheng commented 2 years ago

AuthProvider is an awesome wrapper. Yet, I am unsure how to get the details of the current logged -in user for further manipulation (triggering in-app messages, emails, etc)?

I noted the graphql query for 'demo' in the '17 patterns example', but that already has a 'demo user' to reference.

Would appreciate any advice.

halas commented 1 year ago

Mixing a bit examples from react-admin and from demo app, I managed to make it work like this:

...
import { buildAuthProvider } from "react-admin-amplify";

import dataProvider from "./dataProvider";

Amplify.configure({
 ...
  },
});

const authProvider = buildAuthProvider();

authProvider.getIdentity = async () => {
  const userInfo = await Auth.currentUserInfo();

  return Promise.resolve({
    id: userInfo.username,
    fullName: userInfo.attributes.name,
  });
};

function App() {
  return (
    <Admin
      authProvider={authProvider}
      dataProvider={dataProvider}
...