DEV Community

hirooka kazuya
hirooka kazuya

Posted on

dev diary 20251122

getting user information after sign in.

with AWS amplify gen2 and next.js, how to get user information after sign in ? in previous version of amplify gen1, Authenticator's user object has all the user information and can be get ,for example, by 'user.attribute.nickname'. but now actually i can't get with this user object.

user information from 'fetchUserAttributes'

instead, i use 'fetchUserAttributes' to get user profile. the code is following;

import { fetchUserAttributes } from 'aws-amplify/auth';

interface UserAttributes {
  [key: string]: string;
}

const [userAttributes, setUserAttributes] = useState<UserAttributes | null>(null);

const getUserAttributes = useCallback(async () => {
  try {
    const attributes = await fetchUserAttributes();
    setUserAttributes(attributes as UserAttributes); 
  } catch (error) {
    console.error('Error fetching user attributes:', error);
    setUserAttributes(null);
  }
}, []); 
Enter fullscreen mode Exit fullscreen mode

this userAttributes has the information. and we get it with 'userAttributes.nickname' by each property.

Top comments (0)