Skip to content

Commit

Permalink
chore(auth): Add CLI support for SMS (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 authored Oct 5, 2024
1 parent e545a0c commit cf1d09f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
9 changes: 9 additions & 0 deletions packages/celest/lib/src/auth/auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
///
/// Currently, Celest supports the following authentication methods:
/// - [AuthProvider.email] Email sign-in with OTP codes.
/// - [AuthProvider.sms] SMS sign-in with OTP codes.
/// {@endtemplate}
sealed class AuthProvider {
/// {@macro celest.auth.auth_provider}
const AuthProvider();

/// A provider which enables email sign-in with OTP codes.
const factory AuthProvider.email() = _EmailAuthProvider;

/// A provider which enables SMS sign-in with OTP codes.
const factory AuthProvider.sms() = _SmsAuthProvider;
}

final class _EmailAuthProvider extends AuthProvider {
const _EmailAuthProvider();
}

final class _SmsAuthProvider extends AuthProvider {
const _SmsAuthProvider();
}
7 changes: 0 additions & 7 deletions packages/celest_auth/example/celest/auth.dart

This file was deleted.

2 changes: 2 additions & 0 deletions packages/celest_auth/example/celest/client/lib/src/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ extension type CelestAuth._(_$celest.AuthImpl _hub) implements _$celest.Auth {
);

_$celest.Email get email => _$celest.Email(_hub);

_$celest.Sms get sms => _$celest.Sms(_hub);
}
7 changes: 7 additions & 0 deletions packages/celest_auth/example/celest/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ import 'package:celest/celest.dart';
const project = Project(
name: 'celest_auth_example',
);

const auth = Auth(
providers: [
AuthProvider.email(),
AuthProvider.sms(),
],
);
30 changes: 27 additions & 3 deletions packages/celest_auth/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ class MainApp extends StatefulWidget {

class _MainAppState extends State<MainApp> {
final _emailController = TextEditingController();
final _phoneNumberController = TextEditingController();
final _otpController = TextEditingController();
Future<Object>? _request;

Future<void> signUp() async {
try {
await celest.auth.email.authenticate(email: _emailController.text);
await switch ((_emailController.text, _phoneNumberController.text)) {
(final email, _) when email.isNotEmpty =>
celest.auth.email.authenticate(
email: _emailController.text,
),
(_, final phoneNumber) when phoneNumber.isNotEmpty =>
celest.auth.sms.authenticate(
phoneNumber: _phoneNumberController.text,
),
_ => throw Exception('Email or phone number required'),
};
_emailController.clear();
_phoneNumberController.clear();
} on Exception catch (e, st) {
debugPrint('Error: $e');
debugPrint('Stacktrace: $st');
Expand Down Expand Up @@ -78,7 +90,7 @@ class _MainAppState extends State<MainApp> {
],
const SizedBox(height: 16),
...switch (state) {
EmailNeedsVerification() => [
OtpNeedsVerification() => [
TextField(
key: const ValueKey('otp'),
controller: _otpController,
Expand Down Expand Up @@ -111,9 +123,21 @@ class _MainAppState extends State<MainApp> {
keyboardType: TextInputType.emailAddress,
),
const SizedBox(height: 16),
TextField(
key: const ValueKey('phoneNumber'),
controller: _phoneNumberController,
decoration: const InputDecoration(
labelText: 'Phone Number',
),
autofillHints: const [
AutofillHints.telephoneNumber,
],
keyboardType: TextInputType.phone,
),
const SizedBox(height: 16),
TextButton(
onPressed: signUp,
child: const Text('Sign In with Email'),
child: const Text('Sign In'),
),
],
Authenticated() => [
Expand Down

0 comments on commit cf1d09f

Please sign in to comment.