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

Commit 42def9a

Browse files
committed
Update API and models
1 parent 5eb0395 commit 42def9a

File tree

7 files changed

+179
-58
lines changed

7 files changed

+179
-58
lines changed

example/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Future<bool> main(List<String> arguments) async {
1818
);
1919
print('Logged in, uid: ${login.data!.user!.id}');
2020
} on GotrueError catch (error) {
21-
print('Error!');
21+
print('Sign in Error: ${error.message}');
2222
}
2323

2424
await client.signOut();

lib/src/constants.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ enum AuthChangeEvent {
1515
signedIn,
1616
signedOut,
1717
tokenRefreshed,
18-
userUpdated
18+
userUpdated,
19+
userDeleted
1920
}
2021

2122
extension AuthChangeEventName on AuthChangeEvent {

lib/src/gotrue_api.dart

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class GoTrueApi {
2626
String password, {
2727
AuthOptions? options,
2828
Map<String, dynamic>? userMetadata,
29+
String? captchaToken,
2930
}) async {
3031
final urlParams = [];
3132

@@ -41,6 +42,7 @@ class GoTrueApi {
4142
'email': email,
4243
'password': password,
4344
'data': userMetadata,
45+
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
4446
},
4547
options: FetchOptions(headers),
4648
);
@@ -100,12 +102,14 @@ class GoTrueApi {
100102
String phone,
101103
String password, {
102104
Map<String, dynamic>? userMetadata,
105+
String? captchaToken,
103106
}) async {
104107
final fetchOptions = FetchOptions(headers);
105108
final body = {
106109
'phone': phone,
107110
'password': password,
108111
'data': userMetadata,
112+
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
109113
};
110114
final response =
111115
await _fetch.post('$url/signup', body, options: fetchOptions);
@@ -140,13 +144,11 @@ class GoTrueApi {
140144
String phone, [
141145
String? password,
142146
]) async {
143-
final body = {'phone': phone, 'password': password};
144-
final fetchOptions = FetchOptions(headers);
145147
const queryString = '?grant_type=password';
146148
final response = await _fetch.post(
147149
'$url/token$queryString',
148-
body,
149-
options: fetchOptions,
150+
{'phone': phone, 'password': password},
151+
options: FetchOptions(headers),
150152
);
151153
final session = Session.fromJson(response.rawData as Map<String, dynamic>);
152154
return GotrueSessionResponse.fromResponse(
@@ -163,7 +165,7 @@ class GoTrueApi {
163165
'id_token': oidc.idToken,
164166
'nonce': oidc.nonce,
165167
'client_id': oidc.clientId,
166-
"issuer": oidc.issuer,
168+
'issuer': oidc.issuer,
167169
'provider': oidc.provider?.name(),
168170
};
169171
final fetchOptions = FetchOptions(headers);
@@ -184,8 +186,9 @@ class GoTrueApi {
184186
Future<GotrueJsonResponse> sendMagicLinkEmail(
185187
String email, {
186188
AuthOptions? options,
189+
bool? shouldCreateUser,
190+
String? captchaToken,
187191
}) async {
188-
final body = {'email': email};
189192
final fetchOptions = FetchOptions(headers);
190193
final urlParams = [];
191194
if (options?.redirectTo != null) {
@@ -194,8 +197,12 @@ class GoTrueApi {
194197
}
195198
final queryString = urlParams.isNotEmpty ? '?${urlParams.join('&')}' : '';
196199
final response = await _fetch.post(
197-
'$url/magiclink$queryString',
198-
body,
200+
'$url/otp$queryString',
201+
{
202+
'email': email,
203+
'create_user': shouldCreateUser,
204+
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
205+
},
199206
options: fetchOptions,
200207
);
201208
return GotrueJsonResponse.fromResponse(
@@ -207,8 +214,16 @@ class GoTrueApi {
207214
/// Sends a mobile OTP via SMS. Will register the account if it doesn't already exist
208215
///
209216
/// [phone] is the user's phone number WITH international prefix
210-
Future<GotrueJsonResponse> sendMobileOTP(String phone) async {
211-
final body = {'phone': phone};
217+
Future<GotrueJsonResponse> sendMobileOTP(
218+
String phone, {
219+
bool? shouldCreateUser,
220+
String? captchaToken,
221+
}) async {
222+
final body = {
223+
'phone': phone,
224+
'create_user': shouldCreateUser,
225+
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
226+
};
212227
final fetchOptions = FetchOptions(headers);
213228
final response = await _fetch.post('$url/otp', body, options: fetchOptions);
214229
return GotrueJsonResponse.fromResponse(
@@ -222,6 +237,7 @@ class GoTrueApi {
222237
/// [phone] is the user's phone number WITH international prefix
223238
///
224239
/// [token] is the token that user was sent to their mobile phone
240+
@Deprecated('Use verifyOTP instead')
225241
Future<GotrueSessionResponse> verifyMobileOTP(
226242
String phone,
227243
String token, {
@@ -280,8 +296,12 @@ class GoTrueApi {
280296
Future<GotrueJsonResponse> resetPasswordForEmail(
281297
String email, {
282298
AuthOptions? options,
299+
String? captchaToken,
283300
}) async {
284-
final body = {'email': email};
301+
final body = {
302+
'email': email,
303+
'gotrue_meta_security': {'hcaptcha_token': captchaToken},
304+
};
285305
final fetchOptions = FetchOptions(headers);
286306
final urlParams = [];
287307
if (options?.redirectTo != null) {
@@ -363,14 +383,4 @@ class GoTrueApi {
363383
data: session,
364384
);
365385
}
366-
367-
// TODO: not implemented yet
368-
Never setAuthCookie() {
369-
throw UnimplementedError();
370-
}
371-
372-
// TODO: not implemented yet
373-
Never getUserByCookie() {
374-
throw UnimplementedError();
375-
}
376386
}

lib/src/gotrue_error.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class GotrueError extends Error {
1+
class GotrueError {
22
final String message;
33

44
GotrueError(this.message);

lib/src/provider.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ enum Provider {
77
github,
88
gitlab,
99
google,
10+
keycloack,
11+
linkedin,
12+
notion,
1013
slack,
1114
spotify,
1215
twitch,
1316
twitter,
17+
workos,
1418
}
1519

1620
extension ProviderName on Provider {

lib/src/session.dart

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,31 @@ import 'package:gotrue/src/user.dart';
44
import 'package:jwt_decode/jwt_decode.dart';
55

66
class Session {
7+
final String? providerToken;
78
final String accessToken;
9+
10+
/// The number of seconds until the token expires (since it was issued).
11+
/// Returned when a login is confirmed.
812
final int? expiresIn;
13+
914
final String? refreshToken;
10-
final String? tokenType;
11-
final String? providerToken;
15+
final String tokenType;
1216
final User? user;
1317

14-
Session({
18+
const Session({
1519
required this.accessToken,
1620
this.expiresIn,
1721
this.refreshToken,
18-
this.tokenType,
22+
required this.tokenType,
1923
this.providerToken,
2024
this.user,
2125
});
2226

2327
factory Session.fromJson(Map<String, dynamic> json) => Session(
2428
accessToken: json['access_token'] as String,
25-
expiresIn: json['expires_in'] as int,
29+
expiresIn: json['expires_in'] as int?,
2630
refreshToken: json['refresh_token'] as String?,
27-
tokenType: json['token_type'] as String?,
31+
tokenType: json['token_type'] as String,
2832
providerToken: json['provider_token'] as String?,
2933
user: json['user'] != null
3034
? User.fromJson(json['user'] as Map<String, dynamic>)
@@ -40,6 +44,8 @@ class Session {
4044
'user': user?.toJson(),
4145
};
4246

47+
/// A timestamp of when the token will expire. Returned when a login is
48+
/// confirmed.
4349
int? get expiresAt {
4450
try {
4551
final payload = Jwt.parseJwt(accessToken);

0 commit comments

Comments
 (0)