Closed jmarshall9120 closed 5 months ago
Hey, @jmarshall9120 👋 and thank you for opening this issue. I'll mark this as a feature request for now because we are intentionally throwing the UserUnAuthenticatedException
error in v6 for the getCurrentUser()
API when there is no authenticated user. This was done to optimize bundle size because the solution to cast and throw a specific instance of this error (similar to how the SDK does it) creates more code.
I'll be sure to review this with the team internally and follow up with any additional questions we have, but feel free to add any additional context or questions! Thanks.
Thanks @cwomack. I have no idea how tight your bundling requirements/goals are, but maybe you could at least update the documentation I referenced to show this error as a potential result getCurrentUser()
.
@jmarshall9120, we're already tracking improvements to the error types that are given from the Cognito Service Team in issue #9104 and will consolidate this issue into that one. Feel free to add any additional context, comments, +1/thumbs up on that issue to help it gain traction... but we've already noted this issues comments/details as part of the acceptance criteria for that issue's resolution.
In the meantime, we also created a docs issue to track improvements to the API documentation as you suggested.
Before opening, please confirm:
JavaScript Framework
Not applicable
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
auth
Backend
Amplify Gen 2 (Preview)
Environment information
Describe the bug
Using Gen2 Amplify if you call amplify.Auth.getCurrentUser() with no user set it returns this error:
'UserUnAuthenticatedException'
Despite Gen2 being built for typescript the error is actually an undocumented output of type AuthError. It is returned from here: https://github.com/aws-amplify/amplify-js/blob/d7d33da192ff610c01fcdd417460ddd79d3dcb8d/packages/auth/src/providers/cognito/apis/internal/getCurrentUser.ts#L22
And defined here: https://github.com/aws-amplify/amplify-js/blob/d7d33da192ff610c01fcdd417460ddd79d3dcb8d/packages/auth/src/providers/cognito/utils/types.ts#L33
Documentation does not list this as a possible type of error to be returned from this call: https://aws-amplify.github.io/amplify-js/api/functions/aws_amplify.auth.getCurrentUser.html
This seems to be one of those bug, meets design philosophy issues. JS errors can be very confusing, as giving an error a 'name' instead of a unique type can make them hard to trap. There is very little control over how some string writer will output your error, etc. It is my opinion that if we're going to be using typescript, errors should use unique types, or at least not have different names for a single type. I find this approach to be more consistent with strongly typed languages.
Also - the use case here should be pretty common. As far as I can see there is no way to do a login without first calling
getCurrentUser()
to test if a login is needed. If this is not an intended use case, and there is another way to establish a users auth status when loading the site please advise. Otherwise, I can't see pretty much every user of amplify tripping across this error.Expected behavior
This code should work:
Right now it's necessary to catch it by string compare. Which is more JS than TS.
Reproduction steps
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response