Skip to content

Commit

Permalink
Convert authStateProvider to codegen provider
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Mar 6, 2024
1 parent d5eb578 commit ff242b7
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class _MyApp extends ConsumerState<MyApp> {
void initState() {
super.initState();
_authSub = supabase.auth.onAuthStateChange.listen((data) {
ref.read(authStateProvider.notifier).setUser(data.session?.user);
ref.read(authProvider.notifier).setUser(data.session?.user);
});
initPackageInfo();
}
Expand Down
22 changes: 18 additions & 4 deletions lib/src/cloud/providers/auth_provider.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import 'package:excode/src/cloud/models/auth_model.dart';
import 'package:excode/src/factory.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
part 'auth_provider.g.dart';

final authStateProvider =
StateNotifierProvider<_AuthNotifier, AuthModel>((ref) => _AuthNotifier());
// final authStateProvider =
// StateNotifierProvider<_AuthNotifier, AuthModel>((ref) => _AuthNotifier());

class _AuthNotifier extends StateNotifier<AuthModel> {
_AuthNotifier() : super(AuthModel(user: supabase.auth.currentUser));
// class _AuthNotifier extends StateNotifier<AuthModel> {
// _AuthNotifier() : super(AuthModel(user: supabase.auth.currentUser));

// void setUser(User? user) {
// state = state.copyWith(user: user);
// }
// }

@riverpod
class Auth extends _$Auth {
@override
AuthModel build() {
return AuthModel(user: supabase.auth.currentUser);
}

void setUser(User? user) {
state = state.copyWith(user: user);
Expand Down
24 changes: 24 additions & 0 deletions lib/src/cloud/providers/auth_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/cloud/services/supabase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:fpdart/fpdart.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

// ! Add TaskEither as .error was removed :(
class Auth {
class AuthService {
static Future<Either<String, User?>> register(
String email, String password) async {
final response =
Expand Down
11 changes: 7 additions & 4 deletions lib/src/cloud/views/auth_view.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:excode/src/cloud/providers/auth_provider.dart';
import 'package:excode/src/cloud/services/supabase_auth.dart';
import 'package:excode/src/helpers.dart';
import 'package:excode/src/settings/providers/theme_provider.dart';
Expand Down Expand Up @@ -154,14 +155,14 @@ class _AuthViewState extends ConsumerState<AuthView> {
if (state.value == AuthType.signUp) {
// TODO: Find alternative to set loading state
_toggleLoading();
final response = await Auth.register(
final response = await AuthService.register(
_emailController.text, _passwordController.text);
_toggleLoading();
response.match(
(l) => context.showErrorSnackBar(l),
(r) {
// TODO: Fix auth model
// ref.read(authStateProvider.notifier).setUser(r);
ref.read(authProvider.notifier).setUser(r);
Navigator.pop(context);
context.showSuccessSnackBar(
content:
Expand All @@ -172,8 +173,10 @@ class _AuthViewState extends ConsumerState<AuthView> {
);
} else {
_toggleLoading();
final response = await Auth.signIn(
_emailController.text, _passwordController.text);
final response = await AuthService.signIn(
_emailController.text,
_passwordController.text,
);
_toggleLoading();

response.match(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void initDioClient() {
}

Future<void> initCloudStorage() async {
// ! Suspended project breaks windows release
// FIXME: Suspended project breaks windows release
await Supabase.initialize(
debug: kDebugMode,
url: 'https://vkncfbjgekuolcmqompg.supabase.co',
Expand Down
13 changes: 7 additions & 6 deletions lib/src/settings/widgets/auth_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class AuthContainerWidget extends ConsumerWidget {
child: Column(
children: [
Text(
ref.watch(authStateProvider).user?.email ?? 'Not signed in yet!',
ref.watch(authProvider).user?.email ?? 'Not signed in yet!',
// .match((t) => t.email!, () => 'Not signed in yet!'),
style: const TextStyle(fontSize: 20),
),
Visibility(
visible: ref.watch(authStateProvider).user == null,
visible: ref.watch(authProvider).user == null,
child: TextButton(
onPressed: () {
Navigator.restorablePushNamed(context, AuthView.routeName);
Expand All @@ -42,11 +42,12 @@ class AuthContainerWidget extends ConsumerWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: [
Visibility(
visible: ref.watch(authStateProvider).user != null,
visible: ref.watch(authProvider).user != null,
child: TextButton(
onPressed: () async {
final res = await CloudDatabase.fetch(
supabase.auth.currentUser!.email!);
supabase.auth.currentUser!.email!,
).run();
res.match((l) => context.showErrorSnackBar(l), (r) {
ref
.watch(settingsProvider.notifier)
Expand All @@ -64,10 +65,10 @@ class AuthContainerWidget extends ConsumerWidget {
),
),
Visibility(
visible: ref.watch(authStateProvider).user != null,
visible: ref.watch(authProvider).user != null,
child: TextButton(
onPressed: () async {
final response = await Auth.signOut();
final response = await AuthService.signOut();
response.match(
(l) => context.showErrorSnackBar(l),
(r) => context.showSuccessSnackBar(
Expand Down
6 changes: 3 additions & 3 deletions lib/src/settings/widgets/auth_dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AuthDropDownWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final globalTheme = ref.watch(themeStateProvider);
final user = ref.watch(authStateProvider).user;
final user = ref.watch(authProvider).user;
final email = user == null
? null
: const Base64Encoder().convert(user.email!.codeUnits);
Expand Down Expand Up @@ -75,7 +75,7 @@ class AuthDropDownWidget extends ConsumerWidget {
if (user != null)
PopupMenuItem(
onTap: (() async {
final res = await CloudDatabase.fetch(user.email!);
final res = await CloudDatabase.fetch(user.email!).run();
res.match(
(l) => context.showErrorSnackBar(l),
(r) async {
Expand Down Expand Up @@ -115,7 +115,7 @@ class AuthDropDownWidget extends ConsumerWidget {
if (user != null)
PopupMenuItem(
onTap: (() async {
final response = await Auth.signOut();
final response = await AuthService.signOut();
response.match(
(l) => context.showErrorSnackBar(l),
(r) => context.showSuccessSnackBar(
Expand Down
36 changes: 30 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,26 @@ packages:
dependency: "direct dev"
description:
name: custom_lint
sha256: "22bd87a362f433ba6aae127a7bac2838645270737f3721b180916d7c5946cb5d"
sha256: "445242371d91d2e24bd7b82e3583a2c05610094ba2d0575262484ad889c8f981"
url: "https://pub.dev"
source: hosted
version: "0.5.11"
version: "0.6.2"
custom_lint_builder:
dependency: transitive
description:
name: custom_lint_builder
sha256: "4c0aed2a3491096e91cf1281923ba1b6814993f16dde0fd60f697925225bbbd6"
url: "https://pub.dev"
source: hosted
version: "0.6.2"
custom_lint_core:
dependency: transitive
description:
name: custom_lint_core
sha256: "2952837953022de610dacb464f045594854ced6506ac7f76af28d4a6490e189b"
sha256: ce5d6215f4e143f7780ce53f73dfa6fc503f39d2d30bef76c48be9ac1a09d9a6
url: "https://pub.dev"
source: hosted
version: "0.5.14"
version: "0.6.2"
dart_style:
dependency: transitive
description:
Expand Down Expand Up @@ -657,6 +665,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.4.10"
hotreloader:
dependency: transitive
description:
name: hotreloader
sha256: ed56fdc1f3a8ac924e717257621d09e9ec20e308ab6352a73a50a1d7a4d9158e
url: "https://pub.dev"
source: hosted
version: "4.2.0"
html:
dependency: transitive
description:
Expand Down Expand Up @@ -1045,10 +1061,10 @@ packages:
dependency: transitive
description:
name: riverpod_analyzer_utils
sha256: d4dabc35358413bf4611fcb6abb46308a67c4ef4cd5e69fd3367b11925c59f57
sha256: "8b71f03fc47ae27d13769496a1746332df4cec43918aeba9aff1e232783a780f"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.5.1"
riverpod_annotation:
dependency: "direct main"
description:
Expand All @@ -1065,6 +1081,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.10"
riverpod_lint:
dependency: "direct dev"
description:
name: riverpod_lint
sha256: e9bbd02e9e89e18eecb183bbca556d7b523a0669024da9b8167c08903f442937
url: "https://pub.dev"
source: hosted
version: "2.3.9"
rxdart:
dependency: transitive
description:
Expand Down
14 changes: 7 additions & 7 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_hooks: ^0.20.3
hooks_riverpod: ^2.4.9
flutter_hooks: ^0.20.5
hooks_riverpod: ^2.4.10
# flutter_localizations:
# sdk: flutter
linked_scroll_controller: ^0.2.0
Expand Down Expand Up @@ -50,23 +50,23 @@ dependencies:
flutter_native_splash: ^2.3.10
after_layout: ^1.2.0
gap: ^3.0.1
riverpod_annotation: ^2.3.3
riverpod_annotation: ^2.3.4

dev_dependencies:
flutter_test:
sdk: flutter

build_runner: ^2.4.7

build_runner: ^2.4.8
flutter_lints: ^3.0.1
freezed: ^2.4.5
json_serializable: ^6.7.1
json_annotation: ^4.8.1
flutter_launcher_icons: ^0.13.1
msix: ^3.16.6
test: ^1.24.3
riverpod_generator: ^2.3.9
custom_lint: ^0.5.7
riverpod_generator: ^2.3.10
custom_lint: ^0.6.2
riverpod_lint: ^2.3.9

flutter:
uses-material-design: true
Expand Down

0 comments on commit ff242b7

Please sign in to comment.