Skip to content

Commit 20ac2ba

Browse files
authored
fix: add redirecturl to auth.attemptsignup [#241] (#242)
* fix: add redirecturl to auth.attemptsignup [#241] * fix: make email link sign up work properly [#241] * fix: re-enable email link in signin [#241]
1 parent 7fdd029 commit 20ac2ba

File tree

16 files changed

+227
-167
lines changed

16 files changed

+227
-167
lines changed

packages/clerk_auth/lib/src/clerk_api/api.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,13 @@ class Api with Logging {
264264
Future<ApiResponse> prepareSignUp(
265265
SignUp signUp, {
266266
required Strategy strategy,
267+
String? redirectUrl,
267268
}) async {
268269
return await _fetchApiResponse(
269270
'/client/sign_ups/${signUp.id}/prepare_verification',
270271
params: {
271272
'strategy': strategy,
273+
'redirect_url': redirectUrl,
272274
},
273275
);
274276
}

packages/clerk_auth/lib/src/clerk_auth/auth.dart

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ class Auth {
500500
String? passwordConfirmation,
501501
String? code,
502502
String? signature,
503+
String? redirectUrl,
503504
bool? legalAccepted,
504505
}) async {
505506
final hasVerificationCredential = code is String || signature is String;
@@ -547,26 +548,45 @@ class Auth {
547548
when signUp.status == Status.missingRequirements &&
548549
signUp.missingFields.isEmpty &&
549550
signUp.unverifiedFields.isNotEmpty:
550-
for (final field in signUp.unverifiedFields) {
551+
if (env.supportsPhoneCode && signUp.unverified(Field.phoneNumber)) {
551552
await _api
552-
.prepareSignUp(signUp, strategy: Strategy.forField(field))
553+
.prepareSignUp(signUp, strategy: Strategy.phoneCode)
553554
.then(_housekeeping);
554555
}
555556

557+
if (signUp.unverified(Field.emailAddress)) {
558+
if (env.supportsEmailCode) {
559+
await _api
560+
.prepareSignUp(signUp, strategy: Strategy.emailCode)
561+
.then(_housekeeping);
562+
}
563+
if (env.supportsEmailLink && redirectUrl is String) {
564+
await _api
565+
.prepareSignUp(
566+
signUp,
567+
strategy: Strategy.emailLink,
568+
redirectUrl: redirectUrl,
569+
)
570+
.then(_housekeeping);
571+
}
572+
}
573+
556574
case SignUp signUp
557575
when signUp.status == Status.missingRequirements &&
558576
signUp.missingFields.isEmpty:
559577
await _api
560578
.prepareSignUp(signUp, strategy: strategy)
561579
.then(_housekeeping);
562-
await _api
563-
.attemptSignUp(
564-
client.signUp!,
565-
strategy: strategy,
566-
code: code,
567-
signature: signature,
568-
)
569-
.then(_housekeeping);
580+
if (code is String || signature is String) {
581+
await _api
582+
.attemptSignUp(
583+
client.signUp!,
584+
strategy: strategy,
585+
code: code,
586+
signature: signature,
587+
)
588+
.then(_housekeeping);
589+
}
570590
}
571591
}
572592

packages/clerk_auth/lib/src/models/client/field.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ class Field {
2626
/// username
2727
static const username = Field._('username');
2828

29+
/// password
30+
static const password = Field._('password');
31+
2932
static final _values = <String, Field>{
3033
phoneNumber.name: phoneNumber,
3134
emailAddress.name: emailAddress,
3235
username.name: username,
36+
password.name: password,
3337
};
3438

3539
/// The [values] of the Fields

packages/clerk_auth/lib/src/models/client/strategy.dart

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import 'package:clerk_auth/src/models/enums.dart';
33
import 'package:json_annotation/json_annotation.dart';
44
import 'package:meta/meta.dart';
55

6-
import 'field.dart';
7-
86
/// [Strategy] Clerk object
97
///
108
/// A [Strategy] has a [name]. The various oAuth strategies ('oauth',
@@ -240,21 +238,6 @@ class Strategy {
240238
return null;
241239
}
242240

243-
/// For a given [Field], return an appropriate [Strategy], or
244-
/// throw an error
245-
///
246-
static Strategy forField(Field field) {
247-
return switch (field) {
248-
Field.phoneNumber => Strategy.phoneCode,
249-
Field.emailAddress => Strategy.emailCode,
250-
_ => throw AuthError(
251-
message: 'No strategy associated with {arg}',
252-
argument: field.name,
253-
code: AuthErrorCode.noAssociatedStrategy,
254-
),
255-
};
256-
}
257-
258241
/// For a given [UserAttribute], return an appropriate [Strategy], or
259242
/// throw an error
260243
///

packages/clerk_auth/lib/src/models/environment/environment.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import 'package:clerk_auth/src/models/client/strategy.dart';
2-
import 'package:clerk_auth/src/models/environment/config.dart';
3-
import 'package:clerk_auth/src/models/environment/display_config.dart';
4-
import 'package:clerk_auth/src/models/environment/organization_settings.dart';
5-
import 'package:clerk_auth/src/models/environment/user_settings.dart';
1+
import 'package:clerk_auth/clerk_auth.dart';
62
import 'package:clerk_auth/src/models/informative_to_string_mixin.dart';
73
import 'package:json_annotation/json_annotation.dart';
84
import 'package:meta/meta.dart';
@@ -75,6 +71,21 @@ class Environment with InformativeToStringMixin {
7571
/// Do we have oauth strategies?
7672
bool get hasOauthStrategies => oauthStrategies.isNotEmpty;
7773

74+
bool _supports(UserAttribute attr, Strategy strategy) =>
75+
user.attributes[attr]?.verifications.contains(strategy) == true;
76+
77+
/// Can we verify email verification with [Strategy.emailCode]?
78+
bool get supportsEmailCode =>
79+
_supports(UserAttribute.emailAddress, Strategy.emailCode);
80+
81+
/// Can we verify email verification with [Strategy.emailLink]?
82+
bool get supportsEmailLink =>
83+
_supports(UserAttribute.emailAddress, Strategy.emailLink);
84+
85+
/// Can we verify phone verification with [Strategy.phoneCode]?
86+
bool get supportsPhoneCode =>
87+
_supports(UserAttribute.phoneNumber, Strategy.phoneCode);
88+
7889
/// [Iterable] of other strategies
7990
/// i.e. strategies that are neither oauth nor password-based
8091
Iterable<Strategy> get otherStrategies =>

packages/clerk_flutter/example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ExampleApp extends StatelessWidget {
4646
/// contain a [clerk.Strategy], to use in preference to a strategy
4747
/// inferred from the [Uri]
4848
ClerkDeepLink? createClerkLink(Uri uri) {
49-
if (uri.pathSegments.first == 'sign_in') {
49+
if (uri.pathSegments.first == 'auth') {
5050
return ClerkDeepLink(uri: uri);
5151
}
5252

@@ -59,7 +59,7 @@ class ExampleApp extends StatelessWidget {
5959
/// redirect for a given [clerk.Strategy], or [null] if redirection should
6060
/// be handled in-app
6161
Uri? generateDeepLink(BuildContext context, clerk.Strategy strategy) {
62-
return Uri.parse('clerk://example.com/sign_in/$strategy');
62+
return Uri.parse('clerk://example.com/auth/$strategy');
6363

6464
// if you want to use the default in-app SSO, just remove the
6565
// [redirectionGenerator] parameter from the [ClerkAuthConfig] object

packages/clerk_flutter/example/lib/pages/clerk_sign_in_example.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ class ClerkSignInExample extends StatelessWidget {
1818
),
1919
body: SafeArea(
2020
child: ClerkErrorListener(
21-
child: ClerkAuthBuilder(
22-
signedInBuilder: (context, authState) {
23-
if (authState.env.organization.isEnabled == false) {
24-
return const ClerkUserButton();
25-
}
26-
return const _UserAndOrgTabs();
27-
},
28-
signedOutBuilder: (context, authState) {
29-
return const ClerkAuthentication();
30-
},
21+
child: Padding(
22+
padding: const EdgeInsets.all(16),
23+
child: ClerkAuthBuilder(
24+
signedInBuilder: (context, authState) {
25+
if (authState.env.organization.isEnabled == false) {
26+
return const ClerkUserButton();
27+
}
28+
return const _UserAndOrgTabs();
29+
},
30+
signedOutBuilder: (context, authState) {
31+
return const ClerkAuthentication();
32+
},
33+
),
3134
),
3235
),
3336
),

packages/clerk_flutter/l10n/en.arb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@
6868
"emailAddressConcise": "email",
6969
"enrollment": "Enrollment",
7070
"enrollmentMode": "Enrollment mode:",
71-
"enterCodeSentTo": "Enter code sent to {identifier}",
72-
"@enterCodeSentTo": {
73-
"placeholders": {
74-
"identifier": {
75-
"type": "String"
76-
}
77-
}
78-
},
7971
"enterTheCodeSentTo": "Enter the code sent to {identifier}",
8072
"@enterTheCodeSentTo": {
8173
"placeholders": {

packages/clerk_flutter/lib/generated/clerk_sdk_localizations.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,6 @@ abstract class ClerkSdkLocalizations {
318318
/// **'Enrollment mode:'**
319319
String get enrollmentMode;
320320

321-
/// No description provided for @enterCodeSentTo.
322-
///
323-
/// In en, this message translates to:
324-
/// **'Enter code sent to {identifier}'**
325-
String enterCodeSentTo(String identifier);
326-
327321
/// No description provided for @enterTheCodeSentTo.
328322
///
329323
/// In en, this message translates to:

packages/clerk_flutter/lib/generated/clerk_sdk_localizations_en.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ class ClerkSdkLocalizationsEn extends ClerkSdkLocalizations {
129129
@override
130130
String get enrollmentMode => 'Enrollment mode:';
131131

132-
@override
133-
String enterCodeSentTo(String identifier) {
134-
return 'Enter code sent to $identifier';
135-
}
136-
137132
@override
138133
String enterTheCodeSentTo(String identifier) {
139134
return 'Enter the code sent to $identifier';

0 commit comments

Comments
 (0)