Open brianreinhold opened 11 months ago
Hello @brianreinhold . You can import AuthError
from aws-amplify/auth
and access the error name, message and recoverySuggestion e.g
import { AuthError, signIn } from 'aws-amplify/auth';
try {
await signIn(signInInput)
} catch (error) {
error instanceof AuthError && console.log(error.name, error.message, error.recoverySuggestion)
}
@brianreinhold, I've also noted this as something to add to our documentation via the docs issue referenced above. Thank you for creating this issue in the event that others run into this! Let us know if @israx's recommendation above resolves the issue for you.
@israx Does the 'AuthError' object cover the UserAlreadyAuthenticatedException
? I did find where to import 'AuthError' from as I used in in v5 and assumed I would need it in v6.
So in resolving the promise with the usual then(()=>{}).catch((error)=>{})
with error instanceof AuthError catch the 'UserAlreadyAuthenticatedException'? When I just print console.log(error)' I get
UserAlreadyAuthenticatedException` but nothing else.
I tried it in the code and using this
if (error.name === 'UserAlreadyAuthenticatedException')
catches the exception.
However, the documentation for the signIn() method only shows the following:
* @throws service: {@link InitiateAuthException }, {@link RespondToAuthChallengeException }
* - Cognito service errors thrown during the sign-in process.
* @throws validation: {@link AuthValidationErrorCode } - Validation errors thrown when either username or password
* are not defined.
* @throws AuthTokenConfigException - Thrown when the token provider config is invalid.
for exceptions. The UserAlreadyAuthenticatedException
is not one of them. How many others are there?
Also, in version 5 I never got this exception even if I never signed out...which I almost never do. I simply do a ctrl-S in VS code for my Angular project as I update the code and the app restarts with no UserAlreadyAuthenticatedException
. In v6 it always happens (unless I specifically logout). Is this a new behavior? Can I get any documentation describing this behavior?
Hey @brianreinhold . Amplify v6 currently maps all service, config, validation and unknown errors into the AuthError
instance. If you want to reference any specific error code, please refer to the error name (error.name === 'InitiateAuthException'
).
When it comes to the UserAlreadyAuthenticatedException
, the library doesn't allow to have more than one user authenticated at the same time. This is to avoid key duplication if 2 or more users sign-in without signing out.
Thank you for bringing these concerns. We will improve our API docs to be more descriptive.
Thanks for the info. What has hit me hard is that the user can easily exit the tab without logging out and a restart gives that the user is already authenticate even it it is not the same user (but on the same device). I want to force the user to log in every time.
What happens if user A doesnt logout and user B comes on the same device and logs in with his account? I get strange behavior in this situation including a 'DONE' challenge. I am really confused by what is happening here.
If you want to force your users to login every time, you can use SessionStorage
to store long live credentials until the browser session is finished, or you can implement your own storage mechanism that suits your needs. We offer documentation where you can see how that can be achieved.
This seems to be an undocumented breaking change from v5 to v6. Where in v5 you could call Auth.federatedSignIn({provider: CognitoHostedUIIdentityProvider.Google,})
and if the user was already signed in then it would just kick off the signed in event in Hub. Now it throws this exception. There also doesn't seem to be a documented way to check if the user is signed in before firing signin method.
@israx Does the 'AuthError' object cover the
UserAlreadyAuthenticatedException
? I did find where to import 'AuthError' from as I used in in v5 and assumed I would need it in v6.So in resolving the promise with the usual
then(()=>{}).catch((error)=>{})
with error instanceof AuthError catch the 'UserAlreadyAuthenticatedException'? When I just printconsole.log(error)' I get
UserAlreadyAuthenticatedException` but nothing else.I tried it in the code and using this
if (error.name === 'UserAlreadyAuthenticatedException')
catches the exception.However, the documentation for the signIn() method only shows the following:
* @throws service: {@link InitiateAuthException }, {@link RespondToAuthChallengeException } * - Cognito service errors thrown during the sign-in process. * @throws validation: {@link AuthValidationErrorCode } - Validation errors thrown when either username or password * are not defined. * @throws AuthTokenConfigException - Thrown when the token provider config is invalid.
for exceptions. The
UserAlreadyAuthenticatedException
is not one of them. How many others are there?Also, in version 5 I never got this exception even if I never signed out...which I almost never do. I simply do a ctrl-S in VS code for my Angular project as I update the code and the app restarts with no
UserAlreadyAuthenticatedException
. In v6 it always happens (unless I specifically logout). Is this a new behavior? Can I get any documentation describing this behavior?
is it error.name === 'UserAlreadyAuthenticatedException'
or error.name === 'UserAlreadyAuthenticated'
Can I get any documentation describing this behavior?
made my day
Just solved it as follows:
import { AuthError } from 'aws-amplify/auth'
// add a catch to your function, check the error type by name and handle the error
.catch((error) => {
if (error instanceof AuthError) {
if (error.name === 'UserAlreadyAuthenticatedException') {
// do what ever you want !!!!
}
}
})
Before opening, please confirm:
JavaScript Framework
Angular
Amplify APIs
Authentication
Amplify Categories
auth
Environment information
Describe the bug
Doing a signIn() operation aws-amplify throws a
UserAlreadyAuthenticatedException
. The problem is not the exception but I cannot find any documentation on it so I cannot even import it in my attempt to handle it. I guessed my way to this:however
UserAlreadyAuthenticatedException
is not recognized. I need to import it. But so far I have not been able find where to import it from.Here is where the documentation fails. This is not the only object that I have found very difficult to find where it is declared. This one has been so difficult I have still not found it.
Expected behavior
The documentation would provide the necessary imports so one can handle errors.
Reproduction steps
Search the documentation.
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