Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

Commit

Permalink
Update API and models
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa committed May 1, 2022
1 parent 5eb0395 commit 42def9a
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 58 deletions.
2 changes: 1 addition & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Future<bool> main(List<String> arguments) async {
);
print('Logged in, uid: ${login.data!.user!.id}');
} on GotrueError catch (error) {
print('Error!');
print('Sign in Error: ${error.message}');
}

await client.signOut();
Expand Down
3 changes: 2 additions & 1 deletion lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum AuthChangeEvent {
signedIn,
signedOut,
tokenRefreshed,
userUpdated
userUpdated,
userDeleted
}

extension AuthChangeEventName on AuthChangeEvent {
Expand Down
52 changes: 31 additions & 21 deletions lib/src/gotrue_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GoTrueApi {
String password, {
AuthOptions? options,
Map<String, dynamic>? userMetadata,
String? captchaToken,
}) async {
final urlParams = [];

Expand All @@ -41,6 +42,7 @@ class GoTrueApi {
'email': email,
'password': password,
'data': userMetadata,
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
},
options: FetchOptions(headers),
);
Expand Down Expand Up @@ -100,12 +102,14 @@ class GoTrueApi {
String phone,
String password, {
Map<String, dynamic>? userMetadata,
String? captchaToken,
}) async {
final fetchOptions = FetchOptions(headers);
final body = {
'phone': phone,
'password': password,
'data': userMetadata,
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
};
final response =
await _fetch.post('$url/signup', body, options: fetchOptions);
Expand Down Expand Up @@ -140,13 +144,11 @@ class GoTrueApi {
String phone, [
String? password,
]) async {
final body = {'phone': phone, 'password': password};
final fetchOptions = FetchOptions(headers);
const queryString = '?grant_type=password';
final response = await _fetch.post(
'$url/token$queryString',
body,
options: fetchOptions,
{'phone': phone, 'password': password},
options: FetchOptions(headers),
);
final session = Session.fromJson(response.rawData as Map<String, dynamic>);
return GotrueSessionResponse.fromResponse(
Expand All @@ -163,7 +165,7 @@ class GoTrueApi {
'id_token': oidc.idToken,
'nonce': oidc.nonce,
'client_id': oidc.clientId,
"issuer": oidc.issuer,
'issuer': oidc.issuer,
'provider': oidc.provider?.name(),
};
final fetchOptions = FetchOptions(headers);
Expand All @@ -184,8 +186,9 @@ class GoTrueApi {
Future<GotrueJsonResponse> sendMagicLinkEmail(
String email, {
AuthOptions? options,
bool? shouldCreateUser,
String? captchaToken,
}) async {
final body = {'email': email};
final fetchOptions = FetchOptions(headers);
final urlParams = [];
if (options?.redirectTo != null) {
Expand All @@ -194,8 +197,12 @@ class GoTrueApi {
}
final queryString = urlParams.isNotEmpty ? '?${urlParams.join('&')}' : '';
final response = await _fetch.post(
'$url/magiclink$queryString',
body,
'$url/otp$queryString',
{
'email': email,
'create_user': shouldCreateUser,
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
},
options: fetchOptions,
);
return GotrueJsonResponse.fromResponse(
Expand All @@ -207,8 +214,16 @@ class GoTrueApi {
/// Sends a mobile OTP via SMS. Will register the account if it doesn't already exist
///
/// [phone] is the user's phone number WITH international prefix
Future<GotrueJsonResponse> sendMobileOTP(String phone) async {
final body = {'phone': phone};
Future<GotrueJsonResponse> sendMobileOTP(
String phone, {
bool? shouldCreateUser,
String? captchaToken,
}) async {
final body = {
'phone': phone,
'create_user': shouldCreateUser,
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
};
final fetchOptions = FetchOptions(headers);
final response = await _fetch.post('$url/otp', body, options: fetchOptions);
return GotrueJsonResponse.fromResponse(
Expand All @@ -222,6 +237,7 @@ class GoTrueApi {
/// [phone] is the user's phone number WITH international prefix
///
/// [token] is the token that user was sent to their mobile phone
@Deprecated('Use verifyOTP instead')
Future<GotrueSessionResponse> verifyMobileOTP(
String phone,
String token, {
Expand Down Expand Up @@ -280,8 +296,12 @@ class GoTrueApi {
Future<GotrueJsonResponse> resetPasswordForEmail(
String email, {
AuthOptions? options,
String? captchaToken,
}) async {
final body = {'email': email};
final body = {
'email': email,
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
};
final fetchOptions = FetchOptions(headers);
final urlParams = [];
if (options?.redirectTo != null) {
Expand Down Expand Up @@ -363,14 +383,4 @@ class GoTrueApi {
data: session,
);
}

// TODO: not implemented yet
Never setAuthCookie() {
throw UnimplementedError();
}

// TODO: not implemented yet
Never getUserByCookie() {
throw UnimplementedError();
}
}
2 changes: 1 addition & 1 deletion lib/src/gotrue_error.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class GotrueError extends Error {
class GotrueError {
final String message;

GotrueError(this.message);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ enum Provider {
github,
gitlab,
google,
keycloack,
linkedin,
notion,
slack,
spotify,
twitch,
twitter,
workos,
}

extension ProviderName on Provider {
Expand Down
18 changes: 12 additions & 6 deletions lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@ import 'package:gotrue/src/user.dart';
import 'package:jwt_decode/jwt_decode.dart';

class Session {
final String? providerToken;
final String accessToken;

/// The number of seconds until the token expires (since it was issued).
/// Returned when a login is confirmed.
final int? expiresIn;

final String? refreshToken;
final String? tokenType;
final String? providerToken;
final String tokenType;
final User? user;

Session({
const Session({
required this.accessToken,
this.expiresIn,
this.refreshToken,
this.tokenType,
required this.tokenType,
this.providerToken,
this.user,
});

factory Session.fromJson(Map<String, dynamic> json) => Session(
accessToken: json['access_token'] as String,
expiresIn: json['expires_in'] as int,
expiresIn: json['expires_in'] as int?,
refreshToken: json['refresh_token'] as String?,
tokenType: json['token_type'] as String?,
tokenType: json['token_type'] as String,
providerToken: json['provider_token'] as String?,
user: json['user'] != null
? User.fromJson(json['user'] as Map<String, dynamic>)
Expand All @@ -40,6 +44,8 @@ class Session {
'user': user?.toJson(),
};

/// A timestamp of when the token will expire. Returned when a login is
/// confirmed.
int? get expiresAt {
try {
final payload = Jwt.parseJwt(accessToken);
Expand Down
Loading

0 comments on commit 42def9a

Please sign in to comment.