Skip to content

Sign up with an already registered email returns type cast error #100

Closed
supabase/gotrue-dart
#84

Description

@sorineugen

Bug Report

When the response.rawData is null and response.error is not, signUpWithEmail catches a type cast error instead of the actual error from the fetch operation.
In a previous version this was not an issue, because the type casting was done after the response.error evaluation.

To reproduce

Use an email that is already registered.

final client = SupabaseClient('URL', 'KEY');

// Sign up user with email and password
final response = await client.auth.signUp('[email protected]', 'adsjbda1');

print('response.data: ${response.data}');
print('response.rawData: ${response.rawData}');
print('response.error: ${response.error}');
response.data: null
response.rawData: null
response.error: GotrueError(message: type 'Null' is not a subtype of type 'Map<String, dynamic>' in type cast)

Expected

response.data: null
response.rawData: null
response.error: GotrueError(message: User already registered)

Because the actual response from the fetch operation is:

{
  "code": 400,
  "msg": "User already registered"
}

How I changed the code locally

Modified the signUpWithEmail in gotrue_api.dart.

From:

final data = response.rawData as Map<String, dynamic>;
if (response.error != null) {
  return GotrueSessionResponse(error: response.error);
} else if (data['access_token'] == null) {
...

To:

Map<String, dynamic>? data = response.rawData;
if (response.error != null) {
  return GotrueSessionResponse(error: response.error);
} else if (data != null && data['access_token'] == null) {
...

Activity

  1. bdlukaa commented on Apr 25, 2022

    @bdlukaa
    Collaborator

    This isn't actually an issue with the library. If you change

    final data = response.rawData as Map<String, dynamic>;

    to

    final data = response.rawData as Map<String, dynamic>?;

    , it should start working. This happens because of Dart's null-safety. If you cast it as Map<String, dynamic> (non-nullable), but the value is null, a type error will be thrown.

  2. sorineugen commented on Apr 25, 2022

    @sorineugen
    Author

    This is code from the library, as I mentioned in the "How I changed the code locally" section. Yes, the ? should do it (when casting, as well when using it), but it has to be done in the library.

  3. bdlukaa commented on Apr 25, 2022

    @bdlukaa
    Collaborator

    Ooh! I thought it was yours, sorry! Indeed, this is an issue! Thanks for reporting <3

  4. added
    bug
    auth
    on Apr 25, 2022
  5. sorineugen commented on Apr 25, 2022

    @sorineugen
    Author
  6. bdlukaa commented on Apr 25, 2022

    @bdlukaa
  7. sorineugen commented on Apr 25, 2022

    @sorineugen
    Author
  8. bdlukaa commented on Jul 20, 2022

    @bdlukaa
    Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    authbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions