Skip to content

Commit

Permalink
fix: pr changes
Browse files Browse the repository at this point in the history
  • Loading branch information
slightfoot committed Feb 21, 2025
1 parent 7094042 commit f28c90e
Show file tree
Hide file tree
Showing 22 changed files with 347 additions and 250 deletions.
13 changes: 7 additions & 6 deletions packages/clerk_auth/lib/src/clerk_auth/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class Auth {
ApiResponse _housekeeping(ApiResponse resp) {
if (resp.isError) {
throw AuthError(
code: AuthErrorCode.serverErrorResponse,
message: '{arg}: ${resp.errorMessage}',
argument: resp.status.toString(),
);
Expand Down Expand Up @@ -188,9 +189,9 @@ class Auth {
: null;
final token = await _api.sessionToken(org, templateName);
if (token is! SessionToken) {
throw AuthError(
throw const AuthError(
message: 'No session token retrieved',
localizationCode: AuthErrorLocalizationCode.noSessionTokenRetrieved,
code: AuthErrorCode.noSessionTokenRetrieved,
);
}
return token;
Expand Down Expand Up @@ -345,9 +346,9 @@ class Auth {
String? signature,
}) async {
if (password != passwordConfirmation) {
throw AuthError(
throw const AuthError(
message: "Password and password confirmation must match",
localizationCode: AuthErrorLocalizationCode.passwordMatchError,
code: AuthErrorCode.passwordMatchError,
);
}

Expand Down Expand Up @@ -543,9 +544,9 @@ class Auth {

final expiry = client.signIn?.firstFactorVerification?.expireAt;
if (expiry?.isAfter(DateTime.timestamp()) != true) {
throw AuthError(
throw const AuthError(
message: 'Awaited user action not completed in required timeframe',
localizationCode: AuthErrorLocalizationCode.actionNotTimely,
code: AuthErrorCode.actionNotTimely,
);
}

Expand Down
30 changes: 23 additions & 7 deletions packages/clerk_auth/lib/src/clerk_auth/auth_error.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
/// Container for errors encountered during Clerk auth(entication|orization)
///
class AuthError extends Error {
class AuthError implements Exception {
/// Construct an [AuthError]
AuthError({required this.message, this.argument, this.localizationCode});
const AuthError({
required this.code,
required this.message,
this.argument,
});

/// Error code
final AuthErrorCode? code;

/// The associated [message]
final String message;

/// Any arguments
final String? argument;

/// Localization code
final AuthErrorLocalizationCode? localizationCode;

@override
String toString() {
if (argument case String argument) {
Expand All @@ -22,8 +26,20 @@ class AuthError extends Error {
}
}

/// Code to enable consuming apps to localize if they choose
enum AuthErrorLocalizationCode {
/// Code to enable consuming apps to identify the error
enum AuthErrorCode {
/// Server error response
serverErrorResponse,

/// Error during sign-up flow
signUpFlowError,

/// Invalid Password
invalidPassword,

/// Type Invalid
typeInvalid,

/// No stage for status
noStageForStatus,

Expand Down
2 changes: 1 addition & 1 deletion packages/clerk_auth/lib/src/models/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Client {
throw AuthError(
message: 'No session found for {arg}',
argument: user.name,
localizationCode: AuthErrorLocalizationCode.noSessionFoundForUser,
code: AuthErrorCode.noSessionFoundForUser,
);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/clerk_auth/lib/src/models/client/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ class Field {
/// email address
static const emailAddress = Field._(name: 'email_address');

/// username
static const username = Field._(name: 'username');

static final _values = <String, Field>{
phoneNumber.name: phoneNumber,
emailAddress.name: emailAddress,
username.name: username,
};

/// The [values] of the Fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SessionToken {
_ => throw AuthError(
message: "JWT poorly formatted: {arg}",
argument: jwt,
localizationCode: AuthErrorLocalizationCode.jwtPoorlyFormatted,
code: AuthErrorCode.jwtPoorlyFormatted,
),
};

Expand Down
6 changes: 3 additions & 3 deletions packages/clerk_auth/lib/src/models/client/sign_in.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ class SignIn {
throw AuthError(
message: 'Strategy {arg} unsupported for first factor',
argument: strategy.toString(),
localizationCode: AuthErrorLocalizationCode.noSuchFirstFactorStrategy,
code: AuthErrorCode.noSuchFirstFactorStrategy,
);
case Stage.second:
throw AuthError(
message: 'Strategy {arg} unsupported for second factor',
argument: strategy.toString(),
localizationCode:
AuthErrorLocalizationCode.noSuchSecondFactorStrategy,
code:
AuthErrorCode.noSuchSecondFactorStrategy,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk_auth/lib/src/models/client/strategy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class Strategy {
String name => throw AuthError(
message: 'No strategy associated with {arg}',
argument: '${T.runtimeType} \'$name\'',
localizationCode: AuthErrorLocalizationCode.noAssociatedStrategy,
code: AuthErrorCode.noAssociatedStrategy,
),
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk_auth/lib/src/models/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ enum Stage {
_ => throw AuthError(
message: 'No Stage for {arg}',
argument: status.toString(),
localizationCode: AuthErrorLocalizationCode.noStageForStatus,
code: AuthErrorCode.noStageForStatus,
),
};
}
Expand Down
31 changes: 13 additions & 18 deletions packages/clerk_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ To use this package you will need to go to your [Clerk Dashboard](https://dashbo
create an application and copy the public and publishable API keys into your project.

```dart
class ExampleApp extends StatefulWidget {
class ExampleApp extends StatelessWidget {
/// Constructs an instance of Example App
const ExampleApp({
super.key,
Expand All @@ -41,27 +41,22 @@ class ExampleApp extends StatefulWidget {
/// Publishable Key
final String publishableKey;
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: ClerkAuth(
publishableKey: widget.publishableKey,
child: Scaffold(
backgroundColor: ClerkColors.whiteSmoke,
body: Padding(
padding: horizontalPadding32,
child: Center(
child: ClerkAuthBuilder(
signedInBuilder: (context, auth) => const ClerkUserButton(),
signedOutBuilder: (context, auth) =>
const ClerkAuthenticationWidget(),
),
builder: ClerkAuth.materialAppBuilder(publishableKey: publishableKey),
home: Scaffold(
backgroundColor: const Color(0xFFf5f5f5),
body: SafeArea(
child: Center(
child: ClerkAuthBuilder(
signedInBuilder: (context, auth) {
return const ClerkUserButton();
},
signedOutBuilder: (context, auth) {
return const ClerkAuthentication();
},
),
),
),
Expand Down
9 changes: 1 addition & 8 deletions packages/clerk_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ class ExampleApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
builder: (BuildContext context, Widget? child) {
return ClerkAuth(
publishableKey: publishableKey,
pollMode: SessionTokenPollMode.hungry,
child: ClerkErrorListener(child: child!),
);
},
builder: ClerkAuth.materialAppBuilder(publishableKey: publishableKey),
home: Scaffold(
backgroundColor: const Color(0xFFf5f5f5),
body: SafeArea(
Expand All @@ -59,7 +53,6 @@ class ExampleApp extends StatelessWidget {
if (auth.env.organization.isEnabled == false) {
return const ClerkUserButton();
}

return const _UserAndOrgTabs();
},
signedOutBuilder: (context, auth) {
Expand Down
4 changes: 2 additions & 2 deletions packages/clerk_flutter/l10n/en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
}
}
},
"loading": "Loading...",
"loading": "Loading",
"logo": "Logo",
"missingRequirements": "MISSING REQUIREMENTS",
"name": "Name",
Expand Down Expand Up @@ -213,4 +213,4 @@
"welcomeBackPleaseSignInToContinue": "Welcome back! Please sign in to continue",
"welcomePleaseFillInTheDetailsToGetStarted": "Welcome! Please fill in the details to get started",
"youNeedToAdd": "You need to add:"
}
}
34 changes: 23 additions & 11 deletions packages/clerk_flutter/lib/src/clerk_auth_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';

import 'package:clerk_auth/clerk_auth.dart' as clerk;
import 'package:clerk_flutter/clerk_flutter.dart';
import 'package:clerk_flutter/src/utils/extensions.dart';
import 'package:clerk_flutter/src/utils/localization_extensions.dart';
import 'package:clerk_flutter/src/widgets/ui/common.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -90,10 +90,12 @@ class ClerkAuthState extends clerk.Auth with ChangeNotifier {
useSafeArea: false,
useRootNavigator: true,
routeSettings: const RouteSettings(name: _kSsoRouteName),
builder: (context) => _SsoWebViewOverlay(
url: url,
onError: (error) => _onError(error, onError),
),
builder: (BuildContext context) {
return _SsoWebViewOverlay(
url: url,
onError: (error) => _onError(error, onError),
);
},
);
if (responseUrl == clerk.ClerkConstants.oauthRedirect) {
await refreshClient();
Expand Down Expand Up @@ -265,9 +267,7 @@ class ClerkAuthState extends clerk.Auth with ChangeNotifier {
}

/// Add an [clerk.AuthError] for [message] to the [errorStream]
void addError(String message) {
_errors.add(clerk.AuthError(message: message));
}
void addError(clerk.AuthError error) => _errors.add(error);
}

class _SsoWebViewOverlay extends StatefulWidget {
Expand All @@ -286,11 +286,12 @@ class _SsoWebViewOverlay extends StatefulWidget {

class _SsoWebViewOverlayState extends State<_SsoWebViewOverlay> {
late final WebViewController controller;
var _title = Future<String?>.value('Loading...');
Future<String?>? _title;

@override
void initState() {
super.initState();

controller = WebViewController()
..setUserAgent(
'Clerk Flutter SDK v${clerk.ClerkConstants.flutterSdkVersion}',
Expand All @@ -301,7 +302,10 @@ class _SsoWebViewOverlayState extends State<_SsoWebViewOverlay> {
NavigationDelegate(
onPageFinished: (_) => _updateTitle(),
onWebResourceError: (e) => widget.onError(
clerk.AuthError(message: e.toString()),
clerk.AuthError(
code: clerk.AuthErrorCode.serverErrorResponse,
message: e.toString(),
),
),
onNavigationRequest: (NavigationRequest request) async {
try {
Expand All @@ -324,6 +328,14 @@ class _SsoWebViewOverlayState extends State<_SsoWebViewOverlay> {
controller.loadRequest(Uri.parse(widget.url));
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
_title ??= Future<String?>.value(
ClerkAuth.localizationsOf(context).loading,
);
}

void _updateTitle() {
setState(() {
_title = controller.getTitle();
Expand All @@ -336,7 +348,7 @@ class _SsoWebViewOverlayState extends State<_SsoWebViewOverlay> {
appBar: AppBar(
automaticallyImplyLeading: false,
title: FutureBuilder(
future: _title,
future: _title!,
builder: (context, snapshot) {
return Text(snapshot.data ?? '');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ abstract class ClerkSdkLocalizations {
/// No description provided for @loading.
///
/// In en, this message translates to:
/// **'Loading...'**
/// **'Loading'**
String get loading;

/// No description provided for @logo.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ClerkSdkLocalizationsEn extends ClerkSdkLocalizations {
}

@override
String get loading => 'Loading...';
String get loading => 'Loading';

@override
String get logo => 'Logo';
Expand Down
Loading

0 comments on commit f28c90e

Please sign in to comment.