To fetch the current user's email using AWS Amplify, you can use the fetchUserAttributes method. This approach retrieves all user attributes, allowing you to access the email and other details if needed. Here’s a basic example:
const getUserData = async () => {
try {
const data = await Auth.fetchUserAttributes();
if (data) {
const email = data.email;
const username= data.['custom:FullName'];
console.log("User attributes:", email, username);
}
} catch (error) {
console.error("Error fetching user attributes:", error);
return null;
}
};
Note:
- User Must Be Authenticated: The Authenticator ensures that only
authenticated users can access the page, so calling getUserData will
only succeed if the user is logged in.
- Custom Attributes: Replace custom:FullName with any attribute set up
in your AWS Cognito user pool.
To fetch the current user's email using AWS Amplify, you can use the fetchUserAttributes method. This approach retrieves all user attributes, allowing you to access the email and other details if needed. Here’s a basic example:
const getUserData = async () => {
try {
const data = await Auth.fetchUserAttributes();
if (data) {
const email = data.email;
const username= data.username;
console.log("User attributes:", email, username);
}
} catch (error) {
console.error("Error fetching user attributes:", error);
return null;
}
};
**Note:**
- User Must Be Authenticated: The Authenticator ensures that only
authenticated users can access the page, so calling getUserData will
only succeed if the user is logged in.