Skip to content

Commit 936fa55

Browse files
fix: surface server errors in the ui [#122] (#135)
* fix: surface server errors in the ui [#122] * fix: pr changes * fix: cache authstate and localizations [#122] --------- Co-authored-by: Simon Lightfoot <[email protected]>
1 parent 9f438ab commit 936fa55

File tree

7 files changed

+35
-28
lines changed

7 files changed

+35
-28
lines changed

packages/clerk_auth/lib/clerk_auth.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/// Package that will allow you to authenticate and use Clerk from Dart code.
22
library clerk_auth;
33

4-
export 'src/clerk_api/telemetry.dart';
54
export 'src/clerk_api/api.dart' show ClerkLocalesLookup;
5+
export 'src/clerk_api/telemetry.dart';
66
export 'src/clerk_auth/auth.dart';
7+
export 'src/clerk_auth/auth_error.dart';
8+
export 'src/clerk_auth/http_service.dart';
9+
export 'src/clerk_auth/persistor.dart';
710
export 'src/clerk_constants.dart';
811
export 'src/models/models.dart';
912
export 'src/utils/extensions.dart';

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import 'package:clerk_auth/src/clerk_constants.dart';
1010
import 'package:clerk_auth/src/models/api/api_response.dart';
1111
import 'package:clerk_auth/src/models/models.dart';
1212

13-
export 'auth_error.dart';
14-
export 'http_service.dart';
15-
export 'persistor.dart';
16-
1713
/// [Auth] provides more abstracted access to the Clerk API
1814
///
1915
/// Requires a [publishableKey] found in the Clerk dashboard
@@ -75,6 +71,14 @@ class Auth {
7571

7672
static const _codeLength = 6;
7773

74+
final _errors = StreamController<AuthError>.broadcast();
75+
76+
/// Stream of errors reported by the SDK of type [AuthError]
77+
Stream<AuthError> get errorStream => _errors.stream;
78+
79+
/// Adds [error] to [errorStream]
80+
void addError(AuthError error) => _errors.add(error);
81+
7882
/// The [Environment] object
7983
///
8084
/// configuration of the Clerk account - rarely changes
@@ -157,10 +161,11 @@ class Auth {
157161

158162
ApiResponse _housekeeping(ApiResponse resp) {
159163
if (resp.isError) {
160-
throw AuthError(
161-
code: AuthErrorCode.serverErrorResponse,
162-
message: '{arg}: ${resp.errorMessage}',
163-
argument: resp.status.toString(),
164+
addError(
165+
AuthError(
166+
code: AuthErrorCode.serverErrorResponse,
167+
message: resp.errorMessage,
168+
),
164169
);
165170
} else if (resp.client case Client client) {
166171
this.client = client;

packages/clerk_auth/test/integration/clerk_auth/sign_in_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:clerk_auth/src/clerk_auth/auth.dart';
2+
import 'package:clerk_auth/src/clerk_auth/persistor.dart';
23
import 'package:clerk_auth/src/models/client/strategy.dart';
34
import 'package:clerk_auth/src/models/client/user.dart';
45
import 'package:clerk_auth/src/models/status.dart';

packages/clerk_auth/test/integration/clerk_auth/sign_up_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:clerk_auth/src/clerk_auth/auth.dart';
2+
import 'package:clerk_auth/src/clerk_auth/persistor.dart';
23
import 'package:clerk_auth/src/models/client/client.dart';
34
import 'package:clerk_auth/src/models/client/field.dart';
45
import 'package:clerk_auth/src/models/client/strategy.dart';

packages/clerk_flutter/lib/src/clerk_auth_state.dart

+1-8
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ class ClerkAuthState extends clerk.Auth with ChangeNotifier {
5353
return provider;
5454
}
5555

56-
/// The [clerk.AuthError] stream
57-
late final errorStream = _errors.stream;
58-
59-
final _errors = StreamController<clerk.AuthError>.broadcast();
6056
final OverlayEntry _loadingOverlay;
6157

6258
static const _kRotatingTokenNonce = 'rotating_token_nonce';
@@ -195,7 +191,7 @@ class ClerkAuthState extends clerk.Auth with ChangeNotifier {
195191
}
196192

197193
void _onError(clerk.AuthError error, ClerkErrorCallback? onError) {
198-
_errors.add(error);
194+
addError(error);
199195
onError?.call(error);
200196
}
201197

@@ -268,9 +264,6 @@ class ClerkAuthState extends clerk.Auth with ChangeNotifier {
268264

269265
return null;
270266
}
271-
272-
/// Add an [clerk.AuthError] for [message] to the [errorStream]
273-
void addError(clerk.AuthError error) => _errors.add(error);
274267
}
275268

276269
class _SsoWebViewOverlay extends StatefulWidget {

packages/clerk_flutter/lib/src/widgets/organization/clerk_organization_list.dart

+6-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class ClerkOrganizationList extends StatefulWidget {
3939

4040
class _ClerkOrganizationListState extends State<ClerkOrganizationList>
4141
with ClerkTelemetryStateMixin {
42+
ClerkAuthState? _authState;
43+
ClerkSdkLocalizations? _localizations;
4244
late clerk.User _user = widget.initialUser;
4345
clerk.User? _nextUser;
4446

@@ -53,13 +55,13 @@ class _ClerkOrganizationListState extends State<ClerkOrganizationList>
5355
}
5456

5557
List<ClerkUserAction> _defaultActions() {
56-
final authState = ClerkAuth.of(context);
57-
final localizations = ClerkAuth.localizationsOf(context);
58+
_authState ??= ClerkAuth.of(context);
59+
_localizations ??= ClerkAuth.localizationsOf(context);
5860
return [
59-
if (authState.user?.createOrganizationEnabled == true) //
61+
if (_authState!.user?.createOrganizationEnabled == true) //
6062
ClerkUserAction(
6163
asset: ClerkAssets.addIcon,
62-
label: localizations.createOrganization,
64+
label: _localizations!.createOrganization,
6365
callback: _createOrganization,
6466
),
6567
];

packages/clerk_flutter/lib/src/widgets/user/clerk_user_button.dart

+9-7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class ClerkUserButton extends StatefulWidget {
4545

4646
class _ClerkUserButtonState extends State<ClerkUserButton>
4747
with ClerkTelemetryStateMixin {
48+
ClerkAuthState? _authState;
49+
ClerkSdkLocalizations? _localizations;
4850
final _sessions = <clerk.Session>[];
4951

5052
@override
@@ -60,29 +62,29 @@ class _ClerkUserButtonState extends State<ClerkUserButton>
6062
}
6163

6264
List<ClerkUserAction> _defaultSessionActions() {
63-
final localizations = ClerkAuth.localizationsOf(context);
65+
_localizations ??= ClerkAuth.localizationsOf(context);
6466
return [
6567
ClerkUserAction(
6668
asset: ClerkAssets.gearIcon,
67-
label: localizations.profile,
69+
label: _localizations!.profile,
6870
callback: _manageAccount,
6971
),
7072
ClerkUserAction(
7173
asset: ClerkAssets.signOutIcon,
72-
label: localizations.signOut,
74+
label: _localizations!.signOut,
7375
callback: _signOut,
7476
),
7577
];
7678
}
7779

7880
List<ClerkUserAction> _defaultAdditionalActions() {
79-
final authState = ClerkAuth.of(context);
80-
final localizations = ClerkAuth.localizationsOf(context);
81+
_authState ??= ClerkAuth.of(context);
82+
_localizations ??= ClerkAuth.localizationsOf(context);
8183
return [
82-
if (authState.env.config.singleSessionMode == false)
84+
if (_authState!.env.config.singleSessionMode == false)
8385
ClerkUserAction(
8486
asset: ClerkAssets.addIcon,
85-
label: localizations.addAccount,
87+
label: _localizations!.addAccount,
8688
callback: _addAccount,
8789
),
8890
];

0 commit comments

Comments
 (0)