Skip to content

Commit

Permalink
feat: rename ClerkAuthProvider to ClerkAuthState to clarify its usage
Browse files Browse the repository at this point in the history
  • Loading branch information
slightfoot committed Jan 17, 2025
1 parent 52c8dd3 commit cb88919
Show file tree
Hide file tree
Showing 22 changed files with 208 additions and 203 deletions.
2 changes: 1 addition & 1 deletion packages/clerk_auth/lib/src/clerk_auth/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Auth {

/// A method to be overridden by extension classes to cope with
/// updating their systems when things change (e.g. the clerk_flutter
/// [ClerkAuthProvider] class)
/// ClerkAuth class)
///
void update() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import '../../test_helpers.dart';
void main() {
late final Api api;
late final TestEnv env;
final httpClient = TestHttpClient();
final httpService = TestHttpService();

setUpAll(() async {
env = TestEnv('.env.test');
api = Api(
publishableKey: env.publishableKey,
persistor: Persistor.none,
httpService: httpClient,
httpService: httpService,
);
await setUpLogging(printer: TestLogPrinter(), level: Level.SEVERE);
});

group('Environment:', () {
test('can fetch', () async {
await runWithLogging(() async {
httpClient.expect(
httpService.expect(
'GET /v1/environment',
200,
'{}',
Expand Down
26 changes: 13 additions & 13 deletions packages/clerk_auth/test/integration/clerk_api/sign_in_test.dart

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions packages/clerk_auth/test/integration/clerk_api/sign_up_test.dart

Large diffs are not rendered by default.

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions packages/clerk_auth/test/integration/clerk_auth/sign_in_test.dart

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions packages/clerk_auth/test/integration/clerk_auth/sign_up_test.dart

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions packages/clerk_auth/test/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TestLogPrinter extends Printer {
}
}

class TestHttpClient implements HttpService {
class TestHttpService implements HttpService {
final _expectations = <String, List<Response>>{};

@override
Expand All @@ -51,7 +51,7 @@ class TestHttpClient implements HttpService {
final resp = resps.removeAt(0);
return Future.value(resp);
}
throw TestHttpClientError(message: 'No response available for $key');
throw TestHttpServiceError(message: 'No response available for $key');
}

void expect(String key, int status, String body) {
Expand Down Expand Up @@ -112,11 +112,11 @@ class TestHttpClient implements HttpService {
}
}

class TestHttpClientError extends Error {
TestHttpClientError({required this.message});
class TestHttpServiceError extends Error {
TestHttpServiceError({required this.message});

final String message;

@override
String toString() => 'TestHttpClientError: $message';
String toString() => '$runtimeType: $message';
}
2 changes: 1 addition & 1 deletion packages/clerk_flutter/lib/clerk_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Package that will allow you to authenticate and use Clerk from Flutter code.
library;

export 'src/clerk_auth_provider.dart';
export 'src/clerk_auth_state.dart';
export 'src/utils/clerk_telemetry.dart';
export 'src/utils/clerk_translator.dart';
export 'src/widgets/authentication/clerk_authentication_widget.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import 'package:webview_flutter/webview_flutter.dart';
/// An extension of [clerk.Auth] with [ChangeNotifier] so that
/// updates to the auth state can be propagated out into the UI
///
class ClerkAuthProvider extends clerk.Auth with ChangeNotifier {
/// Construct a [ClerkAuthProvider]
ClerkAuthProvider._({
class ClerkAuthState extends clerk.Auth with ChangeNotifier {
/// Construct a [ClerkAuthState]
ClerkAuthState._({
required super.publishableKey,
required super.persistor,
required this.translator,
Expand All @@ -21,15 +21,15 @@ class ClerkAuthProvider extends clerk.Auth with ChangeNotifier {
builder: (context) => loading ?? defaultLoadingWidget,
);

/// Create an [ClerkAuthProvider] object using appropriate Clerk credentials
static Future<ClerkAuthProvider> create({
/// Create an [ClerkAuthState] object using appropriate Clerk credentials
static Future<ClerkAuthState> create({
required String publishableKey,
clerk.Persistor? persistor,
ClerkTranslator translator = const DefaultClerkTranslator(),
clerk.SessionTokenPollMode pollMode = clerk.SessionTokenPollMode.onDemand,
Widget? loading,
}) async {
final provider = ClerkAuthProvider._(
final provider = ClerkAuthState._(
publishableKey: publishableKey,
persistor: persistor ??
await clerk.DefaultPersistor.create(
Expand Down Expand Up @@ -71,10 +71,10 @@ class ClerkAuthProvider extends clerk.Auth with ChangeNotifier {
clerk.Strategy strategy, {
void Function(clerk.AuthError)? onError,
}) async {
final auth = ClerkAuth.of(context, listen: false);
final authState = ClerkAuth.of(context, listen: false);
final client = await call(
context,
() => auth.oauthSignIn(strategy: strategy),
() => authState.oauthSignIn(strategy: strategy),
onError: onError,
);
final url = client?.signIn?.firstFactorVerification?.providerUrl;
Expand All @@ -92,13 +92,13 @@ class ClerkAuthProvider extends clerk.Auth with ChangeNotifier {
if (token case String token) {
await call(
context,
() => auth.attemptSignIn(strategy: strategy, token: token),
() => authState.attemptSignIn(strategy: strategy, token: token),
onError: onError,
);
} else {
await auth.refreshClient();
await authState.refreshClient();
if (context.mounted) {
await call(context, () => auth.transfer(), onError: onError);
await call(context, () => authState.transfer(), onError: onError);
}
}
if (context.mounted) {
Expand All @@ -119,15 +119,17 @@ class ClerkAuthProvider extends clerk.Auth with ChangeNotifier {
}) async {
T? result;
try {
if (context.mounted) {
if (context.mounted && !_loadingOverlay.mounted) {
Overlay.of(context).insert(_loadingOverlay);
}
result = await fn();
} on clerk.AuthError catch (error) {
_errors.add(error);
onError?.call(error);
} finally {
_loadingOverlay.remove();
if (_loadingOverlay.mounted) {
_loadingOverlay.remove();
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel>
});
}

Future<void> _continue(ClerkAuthProvider auth,
Future<void> _continue(ClerkAuthState authState,
{clerk.Strategy? strategy, String? code}) async {
if (_hasIdent) {
final newStrategy = strategy ?? _strategy;
Expand All @@ -46,9 +46,9 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel>
});
}

await auth(
await authState(
context,
() => auth.attemptSignIn(
() => authState.attemptSignIn(
strategy: newStrategy,
identifier: _identifier,
password: _password.orNullIfEmpty,
Expand All @@ -61,12 +61,12 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel>

@override
Widget build(BuildContext context) {
final auth = ClerkAuth.of(context);
final translator = auth.translator;
final env = auth.env;
final authState = ClerkAuth.of(context);
final translator = authState.translator;
final env = authState.env;
final identifiers = env.identificationStrategies
.map((i) => i.toString().replaceAll('_', ' '));
final factor = auth.client.signIn?.supportedFirstFactors
final factor = authState.client.signIn?.supportedFirstFactors
.firstWhereOrNull((f) => f.strategy == _strategy);
final safeIdentifier = factor?.safeIdentifier;

Expand Down Expand Up @@ -119,7 +119,7 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel>
)
: translator.translate('Enter the code sent to you'),
onSubmit: (code) async {
await _continue(auth, code: code, strategy: _strategy);
await _continue(authState, code: code, strategy: _strategy);
return false;
},
),
Expand All @@ -138,7 +138,7 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel>
obscureText: true,
onChanged: (password) => _password = password,
onSubmit: (_) =>
_continue(auth, strategy: clerk.Strategy.password),
_continue(authState, strategy: clerk.Strategy.password),
),
),
if (env.hasOtherStrategies) ...[
Expand All @@ -149,19 +149,22 @@ class _ClerkSignInPanelState extends State<ClerkSignInPanel>
Padding(
padding: topPadding4,
child: StrategyButton(
key: ValueKey<clerk.Strategy>(strategy),
strategy: strategy,
onClick: () => _continue(auth, strategy: strategy)),
key: ValueKey<clerk.Strategy>(strategy),
strategy: strategy,
onClick: () => _continue(authState, strategy: strategy),
),
),
Padding(
padding: horizontalPadding32 + bottomPadding32 + topPadding16,
child: ClerkMaterialButton(
onPressed: () => _continue(auth),
onPressed: () => _continue(authState),
label: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Center(child: Text(translator.translate('Continue'))),
Center(
child: Text(translator.translate('Continue')),
),
horizontalMargin4,
const Icon(Icons.arrow_right_sharp),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class _ClerkSignOutPanelState extends State<ClerkSignOutPanel>
with ClerkTelemetryStateMixin {
@override
Widget build(BuildContext context) {
final auth = ClerkAuth.of(context);
final authState = ClerkAuth.of(context);
return Padding(
padding: horizontalPadding16,
child: ClerkMaterialButton(
onPressed: () => auth.signOut(),
label: Text(auth.translator.translate('Sign Out')),
onPressed: () => authState.signOut(),
label: Text(authState.translator.translate('Sign Out')),
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class _ClerkSignUpPanelState extends State<ClerkSignUpPanel>
final _values = <clerk.UserAttribute, String>{};
bool _obscurePassword = true;

Future<void> _continue(ClerkAuthProvider auth,
Future<void> _continue(ClerkAuthState auth,
{String? code, clerk.Strategy? strategy}) async {
await auth(context, () async {
final password = _values[clerk.UserAttribute.password];
Expand Down Expand Up @@ -54,9 +54,9 @@ class _ClerkSignUpPanelState extends State<ClerkSignUpPanel>

@override
Widget build(BuildContext context) {
final auth = ClerkAuth.of(context);
final translator = auth.translator;
final env = auth.env;
final authState = ClerkAuth.of(context);
final translator = authState.translator;
final env = authState.env;
final attributes = [
...env.user.attributes.entries
.where((a) => a.value.isEnabled)
Expand All @@ -68,7 +68,7 @@ class _ClerkSignUpPanelState extends State<ClerkSignUpPanel>
mainAxisSize: MainAxisSize.min,
children: [
Closeable(
closed: auth.signUp?.unverified(clerk.Field.phoneNumber) != true,
closed: authState.signUp?.unverified(clerk.Field.phoneNumber) != true,
child: Padding(
padding: verticalPadding8,
child: ClerkCodeInput(
Expand All @@ -80,7 +80,7 @@ class _ClerkSignUpPanelState extends State<ClerkSignUpPanel>
),
onSubmit: (code) async {
await _continue(
auth,
authState,
strategy: clerk.Strategy.phoneCode,
code: code,
);
Expand All @@ -90,7 +90,8 @@ class _ClerkSignUpPanelState extends State<ClerkSignUpPanel>
),
),
Closeable(
closed: auth.signUp?.unverified(clerk.Field.emailAddress) != true,
closed:
authState.signUp?.unverified(clerk.Field.emailAddress) != true,
child: Padding(
padding: verticalPadding8,
child: ClerkCodeInput(
Expand All @@ -101,15 +102,15 @@ class _ClerkSignUpPanelState extends State<ClerkSignUpPanel>
substitution: _values[clerk.UserAttribute.emailAddress],
),
onSubmit: (code) async {
await _continue(auth,
await _continue(authState,
strategy: clerk.Strategy.emailCode, code: code);
return false;
},
),
),
),
Closeable(
closed: auth.signUp?.unverifiedFields.isNotEmpty == true,
closed: authState.signUp?.unverifiedFields.isNotEmpty == true,
child: Column(
children: [
for (final attribute in attributes)
Expand Down Expand Up @@ -140,7 +141,7 @@ class _ClerkSignUpPanelState extends State<ClerkSignUpPanel>
),
),
ClerkMaterialButton(
onPressed: () => _continue(auth),
onPressed: () => _continue(authState),
label: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class _ClerkSSOPanelState extends State<ClerkSSOPanel>
with ClerkTelemetryStateMixin {
@override
Widget build(BuildContext context) {
final auth = ClerkAuth.of(context);
final oauthStrategies = auth.env.config.identificationStrategies //
final authState = ClerkAuth.of(context);
final oauthStrategies = authState.env.config.identificationStrategies //
.where((i) => i.isOauth)
.toList();
final socialConnections = auth.env.user.socialSettings.values //
final socialConnections = authState.env.user.socialSettings.values //
.where((s) => oauthStrategies.contains(s.strategy))
.toList();

Expand Down
Loading

0 comments on commit cb88919

Please sign in to comment.