nhost / nhost-dart

Nhost Dart & Flutter packages
https://nhost.io
MIT License
90 stars 33 forks source link

User class does not include fields in parity with `nhost-js` #139

Closed totzk9 closed 4 months ago

totzk9 commented 4 months ago

nhost-js has

export interface User {
    /** User's unique identifier (uuid) */
    id: string;
    /** The date-time when the user has been created */
    createdAt: string;
    /** User's display name */
    displayName: string;
    /** The URL to the user's profile picture */
    avatarUrl: string;
    /** The locale of the user, as a two-characters string
     * @example `'en'`
     */
    locale: string;
    /** User's email address */
    email?: string;
    /** Whether or not the user is anonymous */
    isAnonymous: boolean;
    /** The default role of the user
     * @example `'user'`
     */
    defaultRole: string;
    /** The roles assigned to the user
     * @example `['user', 'me']`
     */
    roles: string[];
    /** Additional attributes used for user information */
    metadata: Record<string, unknown>;
    /** Is `true` if the user email has not been verified */
    emailVerified: boolean;
    phoneNumber: string | null;
    phoneNumberVerified: boolean;
    activeMfaType: 'totp' | null;
}

while nhost-dart only have

class User {
  User({
    required this.id,
    required this.displayName,
    required this.locale,
    required this.createdAt,
    required this.isAnonymous,
    required this.defaultRole,
    required this.roles,
    this.metadata,
    this.email,
    this.avatarUrl,
  });

  final String id;
  final String? email;
  final String displayName;

  /// A [Uri] locating the user's avatar image, or `null` if none
  final Uri? avatarUrl;
  final String locale;
  final DateTime createdAt;

  final bool isAnonymous;
  final String defaultRole;
  final List<String> roles;
  final Map<String, Object?>? metadata;

  static User fromJson(dynamic json) {
    return User(
      id: json['id'] as String,
      email: json['email'] as String?,
      displayName: json['displayName'] as String,
      locale: json['locale'] as String,
      avatarUrl: json['avatarUrl'] == null
          ? null
          : Uri.parse(json['avatarUrl'] as String),
      createdAt: DateTime.parse(json['createdAt']),
      isAnonymous: json['isAnonymous'] as bool,
      defaultRole: json['defaultRole'] as String,
      roles: <String>[...json['roles']],
      metadata: json['metadata'] == null
          ? null
          : <String, Object?>{...json['metadata']},
    );
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'id': id,
      'email': email,
      'displayName': displayName,
      'avatarUrl': avatarUrl?.toString(),
      'locale': locale,
      'createdAt': createdAt.toIso8601String(),
      'isAnonymous': isAnonymous,
      'defaultRole': defaultRole,
      'metadata': metadata,
      'roles': roles,
    };
  }

  @override
  String toString() {
    return {
      'id': id,
      'displayName': displayName,
      'locale': locale,
      'createdAt': createdAt,
      'isAnonymous': isAnonymous,
      'defaultRole': defaultRole,
      'roles': roles,
      'metadata': metadata,
      'email': email,
      'avatarUrl': avatarUrl,
    }.toString();
  }
}

Reason why I pointed this out: My use case is to make use of the emailVerified, phoneNumber and phoneNumberVerified. activeMfaType will also be useful in future implementations

totzk9 commented 4 months ago

@onehassan @dbarrosop I'd like to clarify, does the NhostSession for User already contain the missing fields I mentioned? (e.g emailVerified, phoneNumber and phoneNumberVerified) If so, I could probably create a PR for this

totzk9 commented 4 months ago

Did some debugging and signIn does indeed returns the missing fields

{
"accessToken":"eyJhbGc......KI",
"accessTokenExpiresIn":2592000,
"refreshToken":"9b5....6f8",
"refreshTokenId":"465....784",
"user":{"avatarUrl":"http....=g",
"createdAt":"2024-07-13T06:13:52.606751Z",
"defaultRole":"user",
"displayName":"tyro..mail.com",
"email":"tyro...mail.com",
"emailVerified":true,
"id":"be0...e4d",
"isAnonymous":false,
"locale":"en",
"metadata":null,
"phoneNumber":"",
"phoneNumberVerified":false,
"roles":["user","me"]}
}

Will be working on a PR

totzk9 commented 4 months ago

Fixed by https://github.com/nhost/nhost-dart/pull/140

dbarrosop commented 4 months ago

Thanks for the PR, for reference, this is how it should look like:

https://github.com/nhost/hasura-auth/blob/main/go/api/openapi.yaml#L504-L569

So your PR looks good, thanks again.

totzk9 commented 4 months ago

https://github.com/nhost/nhost-dart/pull/140 merged