Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gotrue): add reauthenticate and resend method #517

Merged
merged 7 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion packages/gotrue/lib/src/gotrue_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,73 @@ class GoTrueClient {
return refreshCompleter.future;
}

/// Sends a reauthentication OTP to the user's email or phone number.
///
/// Requires the user to be signed-in.
Future<void> reauthenticate() async {
final session = currentSession;
if (session == null) {
throw AuthException('Not logged in.');
}

final options =
GotrueRequestOptions(headers: headers, jwt: session.accessToken);

await _fetch.request(
'$_url/reauthenticate',
RequestMethodType.get,
options: options,
);
}

///Resends an existing signup confirmation email, email change email, SMS OTP or phone change OTP.
///
///Use [EmailResendParams] or [PhoneResendParams] to specify the type of resend.
Future<ResendResponse> resend({
String? email,
String? phone,
required OtpType type,
String? emailRedirectTo,
String? captchaToken,
}) async {
assert((email != null && phone == null) || (email == null && phone != null),
'`email` or `phone` needs to be specified.');

if (type != OtpType.emailChange && type != OtpType.phoneChange) {
_removeSession();
}

final body = {
if (email != null) 'email': email,
if (phone != null) 'phone': phone,
'type': type.snakeCase,
'gotrue_meta_security': {'captcha_token': captchaToken},
};

final options = GotrueRequestOptions(
headers: _headers,
body: body,
redirectTo: emailRedirectTo,
);

final response = await _fetch.request(
'$_url/resend',
RequestMethodType.post,
options: options,
);

if ((response as Map).containsKey(['message_id'])) {
return ResendResponse(messageId: response['message_id']);
} else {
return ResendResponse();
}
}

/// Updates user data, if there is a logged in user.
Future<UserResponse> updateUser(UserAttributes attributes) async {
Future<UserResponse> updateUser(
UserAttributes attributes, {
String? emailRedirectTo,
}) async {
final accessToken = currentSession?.accessToken;
if (accessToken == null) {
throw AuthException('Not logged in.');
Expand All @@ -491,6 +556,7 @@ class GoTrueClient {
headers: _headers,
body: body,
jwt: accessToken,
redirectTo: emailRedirectTo,
);
final response = await _fetch.request('$_url/user', RequestMethodType.put,
options: options);
Expand Down
9 changes: 9 additions & 0 deletions packages/gotrue/lib/src/types/auth_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class UserResponse {
UserResponse.fromJson(Map<String, dynamic> json) : user = User.fromJson(json);
}

class ResendResponse {
/// Only set for phone resend
String? messageId;

ResendResponse({
this.messageId,
});
}

class AuthSessionUrlResponse {
final Session session;
final String? redirectType;
Expand Down
7 changes: 7 additions & 0 deletions packages/gotrue/lib/src/types/user_attributes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class UserAttributes {
/// The user's password.
String? password;

/// The nonce sent for reauthentication if the user's password is to be updated.
///
/// Call reauthenticate() to obtain the nonce first.
String? nonce;

/// A custom data object to store the user's metadata. This maps to the `auth.users.user_metadata` column.
///
/// The `data` should be a JSON object that includes user-specific info, such as their first and last name.
Expand All @@ -17,13 +22,15 @@ class UserAttributes {
this.email,
this.phone,
this.password,
this.nonce,
this.data,
}) : assert(data == null || data is List || data is Map);

Map<String, dynamic> toJson() {
return {
if (email != null) 'email': email,
if (phone != null) 'phone': phone,
if (nonce != null) 'nonce': nonce,
if (password != null) 'password': password,
if (data != null) 'data': data,
};
Expand Down