AuthError is an unexpected type calling getCurrentUser() with no authenticated user. #13400
Closed
3 tasks done
Labels
Auth
Related to Auth components/category
duplicate
If marked with duplicate, issue will be closed & original will be added for traceability
feature-request
Request a new feature
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:
amplify-js/packages/auth/src/providers/cognito/apis/internal/getCurrentUser.ts
Line 22 in d7d33da
And defined here:
amplify-js/packages/auth/src/providers/cognito/utils/types.ts
Line 33 in d7d33da
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
// Put your code below this line.
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
The text was updated successfully, but these errors were encountered: