Skip to content

Commit

Permalink
Finish runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed Mar 8, 2024
1 parent 5455826 commit 20fcbdb
Show file tree
Hide file tree
Showing 18 changed files with 336 additions and 274 deletions.
19 changes: 14 additions & 5 deletions packages/celest_auth/example/celest/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

library; // ignore_for_file: no_leading_underscores_for_library_prefixes

import 'dart:async' as _$async;
import 'dart:io' as _$io;

import 'package:celest_core/celest_core.dart';
import 'package:celest_core/_internal.dart';
import 'package:celest_core/src/util/globals.dart';
import 'package:http/http.dart' as _$http;

import 'src/client/auth.dart';
import 'src/client/functions.dart';
import 'src/client/serializers.dart';

export 'package:celest_auth/celest_auth.dart';

final Celest celest = Celest();

enum CelestEnvironment {
Expand All @@ -31,14 +34,20 @@ class Celest with CelestBase {

late CelestEnvironment _currentEnvironment;

late final SecureStorage _secureStorage = SecureStorage();

@override
late _$http.Client httpClient = _$http.Client();
late _$http.Client httpClient =
CelestHttpClient(secureStorage: _secureStorage);

late Uri _baseUri;

final _functions = CelestFunctions();

late final CelestAuth _auth = CelestAuth(this);
late final CelestAuth _auth = CelestAuth(
this,
secureStorage: _secureStorage,
);

T _checkInitialized<T>(T Function() value) {
if (!_initialized) {
Expand All @@ -59,12 +68,12 @@ class Celest with CelestBase {
CelestAuth get auth => _checkInitialized(() => _auth);

void init({CelestEnvironment environment = CelestEnvironment.local}) {
if (environment != _currentEnvironment) {
if (_initialized && environment != _currentEnvironment) {
_auth.signOut();
}
_currentEnvironment = environment;
_baseUri = environment.baseUri;
_auth.init();
_$async.scheduleMicrotask(_auth.init);
if (!_initialized) {
initSerializers();
}
Expand Down
19 changes: 13 additions & 6 deletions packages/celest_auth/example/celest/lib/src/client/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@

library; // ignore_for_file: no_leading_underscores_for_library_prefixes

import 'package:celest_auth/src/auth.dart' as _$auth;
import 'package:celest_auth/src/flows/email_flow.dart' as _$email_flow;
import 'package:celest_core/celest_core.dart';
import 'package:celest_auth/celest_auth.dart' as _$celest_auth;
import 'package:celest_auth/src/auth_impl.dart' as _$auth_impl;
import 'package:celest_core/_internal.dart';

extension type CelestAuth._(_$auth.AuthImpl _hub) implements _$auth.Auth {
CelestAuth(CelestBase celest) : _hub = _$auth.AuthImpl(celest);
extension type CelestAuth._(_$auth_impl.AuthImpl _hub)
implements _$celest_auth.Auth {
CelestAuth(
CelestBase celest, {
required SecureStorage secureStorage,
}) : _hub = _$auth_impl.AuthImpl(
celest,
secureStorage: secureStorage,
);

_$email_flow.Email get email => _$email_flow.Email(_hub);
_$auth_impl.Email get email => _$auth_impl.Email(_hub);
}
124 changes: 38 additions & 86 deletions packages/celest_auth/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'package:celest_auth/src/flows/email_flow.dart';
import 'package:celest_backend/client.dart';
import 'package:celest_core/celest_core.dart';
import 'package:celest_backend/models/person.dart';
import 'package:flutter/material.dart';
import 'package:flutter_state_notifier/flutter_state_notifier.dart';
import 'package:http/http.dart' as http;

void main() {
celest.init();
Expand All @@ -17,38 +14,18 @@ class MainApp extends StatefulWidget {
State<MainApp> createState() => _MainAppState();
}

enum _Step { idle, signingIn, needsVerification, signedIn }

final class _State {
_State(
this.step, {
this.verification,
this.user,
});

final _Step step;
final EmailSignUpNeedsVerification? verification;
final User? user;
}

class _MainAppState extends State<MainApp> {
final _state = ValueNotifier(_State(_Step.idle));
final _emailController = TextEditingController();
final _otpController = TextEditingController();
Future<http.Response>? _request;
Future<Object>? _request;

Future<void> signUp() async {
try {
_state.value = _State(_Step.signingIn);
final verify =
await celest.auth.email.signUp(email: _emailController.text);
print('Needs verification');
await celest.auth.email.signIn(email: _emailController.text);
_emailController.clear();
_state.value = _State(_Step.needsVerification, verification: verify);
} on Exception catch (e, st) {
print('Error: $e');
print('Stacktrace: $st');
_state.value = _State(_Step.idle);
debugPrint('Error: $e');
debugPrint('Stacktrace: $st');
}
}

Expand All @@ -59,16 +36,32 @@ class _MainAppState extends State<MainApp> {
body: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: ValueListenableBuilder(
valueListenable: _state,
builder: (context, state, child) {
child: StreamBuilder(
stream: celest.auth.authStateChanges,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
final state = snapshot.data!;
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (state.user case final user?)
if (state case Authenticated(:final user))
Text('Currently signed in: ${user.email}')
else
const Text('Currently signed out'),
const SizedBox(height: 16),
TextButton(
onPressed: () {
_request = celest.functions.greeting.sayHello(
person: const Person(name: 'Dillon'),
);
if (context.mounted) {
setState(() {});
}
},
child: const Text('Make authenticated request'),
),
if (_request case final request?) ...[
const SizedBox(height: 16),
SizedBox(
Expand All @@ -77,25 +70,19 @@ class _MainAppState extends State<MainApp> {
child: FutureBuilder(
future: request,
builder: (context, snapshot) => switch (snapshot) {
AsyncSnapshot(:final Object error) ||
AsyncSnapshot(
data: http.Response(
statusCode: != 200,
body: final Object error
)
) =>
AsyncSnapshot(:final error?) =>
Text('Error: $error'),
AsyncSnapshot(data: final response?) =>
Text('Response: ${response.body}'),
Text('Response: $response'),
_ => const CircularProgressIndicator(),
},
),
),
),
],
const SizedBox(height: 16),
...switch (state.step) {
_Step.needsVerification => [
...switch (state) {
EmailNeedsVerification() => [
TextField(
key: const ValueKey('otp'),
controller: _otpController,
Expand All @@ -109,45 +96,13 @@ class _MainAppState extends State<MainApp> {
),
const SizedBox(height: 16),
TextButton(
onPressed: () async {
final user = await state.verification!.verifyOtp(
_otpController.text,
);
_state.value = _State(_Step.signedIn, user: user);
},
onPressed: () => state.verifyOtp(
_otpController.text,
),
child: const Text('Verify OTP'),
),
const SizedBox(height: 16),
StateNotifierBuilder(
stateNotifier: state.verification!.canResend,
builder: (context, canResend, child) {
final (resend, resendIn) = canResend;
return TextButton(
onPressed: resend
? () async {
await state.verification!.resend();
if (context.mounted) {
ScaffoldMessenger.of(context)
.showSnackBar(
SnackBar(
content: Text(
'Resent OTP to ${state.verification!.email}',
),
),
);
}
}
: null,
child: resend || resendIn == Duration.zero
? const Text('Resend OTP')
: Text(
'Resend OTP (${resendIn.inSeconds})',
),
);
},
),
],
_Step.idle || _Step.signingIn => [
Unauthenticated() => [
TextField(
key: const ValueKey('email'),
controller: _emailController,
Expand All @@ -161,20 +116,17 @@ class _MainAppState extends State<MainApp> {
),
const SizedBox(height: 16),
TextButton(
onPressed:
state.step != _Step.signingIn ? signUp : null,
child: const Text('Sign Up'),
onPressed: signUp,
child: const Text('Sign In with Email'),
),
],
_Step.signedIn => [
Authenticated() => [
TextButton(
onPressed: () {
_state.value = _State(_Step.idle);
},
onPressed: celest.auth.signOut,
child: const Text('Sign out'),
),
],
}
},
],
);
},
Expand Down
4 changes: 2 additions & 2 deletions packages/celest_auth/lib/celest_auth.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library;

export 'src/platform/auth_platform.dart';
export 'src/platform/passkeys/passkey_platform.dart';
export 'src/auth.dart' show Auth;
export 'src/state/auth_state.dart';
Loading

0 comments on commit 20fcbdb

Please sign in to comment.