Closed johnf closed 1 month ago
@johnf, thanks for opening this issue with so much detail! Can you give a little more context on how you're using the error object? Are you using console.log()
or doing it in a different way?
@cwomack It is directly related to the social linking in https://github.com/aws-amplify/amplify-cli/issues/4427. You need to know what the error that was thrown in the trigger so you can perform an action.
I have other use cases where I might throw 'Mobile numbers for your country are banned, please contact support'
Hub.listen('auth', async ({ payload }) => {
const { event } = payload;
switch (event) {
case 'signInWithRedirect_failure': {
const { error } = payload.data;
if (!error) {
Sentry.captureMessage('Unhandled signInWithRedirect_failure event', { extra: { event } });
console.error('Unhandled signInWithRedirect_failure event', JSON.stringify(payload, null, 2));
return;
}
const linkedMatch = error.message.match(/LINKED_EXTERNAL_USER_([^.]+)/);
if (linkedMatch) {
const provider = (linkedMatch[1].charAt(0).toUpperCase() +
linkedMatch[1].slice(1)) as SignInWithRedirectInput['provider'];
await handleFederatedSignIn(provider);
return;
}
if (error.message.match(/User is not enabled/)) {
Toast.show('This account is in the process of being deleted, please contact support', { type: 'error' });
return;
}
Toast.show('Unhandled Error: Please contact support for help.', { type: 'error' });
Sentry.captureMessage('Unhandled signInWithRedirect_failure event', { extra: { event } });
console.error('Unhandled signInWithRedirect_failure event', JSON.stringify(payload, null, 2));
return;
}
}
@johnf, thanks for the reply.
The JSON.stringify
method cannot serialize a custom error, but you can instead access the properties of the error object before making the console.error()
call. As you've already shown in your suggestions for a PR and code change in the issue description, the error.message
is accessible.
This shouldn't require us to make any further code changes or consider this behavior to be a bug at this point.
@cwomack Sorry for wasting time here; I was down a deep rabbit hole yesterday with the current race condition around Hub.listen and oAuth.
Looking at this code with fresh eyes, it's obvious that it's superfluous.
Thanks for your help!
@johnf, thanks for following up and glad you're unblocked!
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
auth
Backend
Amplify CLI
Environment information
Describe the bug
I'm using next.js for authentication with Google
I have Cognito PreSignup trigger that creates a standard account if the user email doesn't exist and then returns an error Similar to what is described in https://github.com/aws-amplify/amplify-cli/issues/4427
This works fine in my React Native app, but on the web the path is different
It hits https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/providers/cognito/utils/oauth/completeOAuthFlow.ts#L39-L44 the
error_message
gets passed to https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/providers/cognito/utils/oauth/createOAuthError.ts#L8-L17The message looks like it's being passed into the error but it us surfaced at https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/providers/cognito/utils/oauth/attempt and then on to https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/providers/cognito/utils/oauth/handleFailure.ts#L19
The error message itself is lost.
I've fixed with this diff and can do a PR
Expected behavior
The error message from the backend exception is accessible in the Hub data
Reproduction steps
I can provide a repro if needed but it's basically
signInRedirect_withFailure
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