Issue: I am encountering an error when attempting to utilize Auth
from AWS-Amplify in separate functions within a componentized structure, specifically in a helper.ts
file.
Usage:
employerID: Auth.user.attributes["custom:id"],
Error Message:
Property 'user' is private and can only be accessed within the 'AuthClass' class. ts(2341)
As a solution, I pass Auth
as a prop to the function in the main file that renders the page. For example:
await listEmployerImagesDetails(Auth);
, but Auth
is currently declared as any
within the function.
Complete Code Example: helpers.ts
export const listEmployerImagesDetails = async(Auth: any, nextToken: string) => {
try {
const images = await API.graphql({
employerID: Auth.user.attributes["custom:id"],
...
},
});
return {...}
} catch (error){
...
}
};
Inquiries:
- How can I effectively use Auth in separated functions? (preferred method)
- If direct usage of Auth isn't feasible, how can I incorporate
AuthClass
or a similar approach to correctly assign Auth to the appropriate prop location while adhering to TypeScript's type safety?