Skip to content

Commit

Permalink
feat: custom auth controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
dk-a-dev committed Aug 4, 2024
1 parent 6415c5d commit c2fd64c
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 151 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"prefs",
"riverpod"
]
}
4 changes: 2 additions & 2 deletions book_keeper/lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:book_keeper/auth/presentation/pages/home_screen.dart';
import 'package:book_keeper/auth/presentation/pages/login_screen.dart';
import 'package:book_keeper/auth/repository/auth_repo.dart';
import 'package:book_keeper/auth/repository/firebase_auth_repository.dart';
import 'package:book_keeper/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -18,7 +18,7 @@ class MainApp extends ConsumerWidget {
home: user.when(
data: (user) {
if (user != null) {
return HomeScreen();
return const HomeScreen();
} else {
return const LoginScreen();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:book_keeper/auth/repository/custom_auth_repository.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'custom_auth_login_controller.g.dart';

@riverpod
class CustomAuthLoginController extends _$CustomAuthLoginController {
@override
Future<void> build() async {}

Future<void> logInWithEmailAndPassword({
required String email,
required String password,
}) async {
final authRepo = ref.read(customAuthRepoProvider);
await authRepo.logInWithEmailAndPassword(email: email, password: password);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:book_keeper/auth/repository/custom_auth_repository.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'custom_auth_signup_controller.g.dart';

@riverpod
class CustomAuthLoginController extends _$CustomAuthLoginController {
@override
Future<void> build() async {}

Future<void> signUpWithEmailAndPassword({
required String name,
required String email,
required String password,
}) async {
final authRepo = ref.read(customAuthRepoProvider);
await authRepo.signUpWithEmailAndPassword(
name: name,
email: email,
password: password,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:book_keeper/auth/repository/auth_repo.dart';
import 'package:book_keeper/auth/repository/firebase_auth_repository.dart';

part 'login_page_controller.g.dart';
part 'google_auth_controller.g.dart';

@riverpod
class LoginPageController extends _$LoginPageController {
class GoogleAuthController extends _$GoogleAuthController {
@override
FutureOr<void> build() {}

Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions book_keeper/lib/auth/presentation/pages/login_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:book_keeper/auth/presentation/controllers/login_page_controller.dart';
import 'package:book_keeper/auth/presentation/controllers/google_auth_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

Expand All @@ -12,8 +12,8 @@ class LoginScreen extends ConsumerStatefulWidget {
class _LoginScreenState extends ConsumerState<LoginScreen> {
@override
Widget build(BuildContext context) {
final state = ref.watch(loginPageControllerProvider);
ref.listen(loginPageControllerProvider, (_, state) {
final state = ref.watch(googleAuthControllerProvider);
ref.listen(googleAuthControllerProvider, (_, state) {
if (!state.isLoading && state.hasError) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(state.error.toString())),
Expand All @@ -30,7 +30,7 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
child: state.isLoading
? const CircularProgressIndicator()
: ElevatedButton(
onPressed: () => ref.read(loginPageControllerProvider.notifier).signInWithGoogle(),
onPressed: () => ref.read(googleAuthControllerProvider.notifier).signInWithGoogle(),
child: const Text('Sign in with Google'),
),
),
Expand Down
55 changes: 0 additions & 55 deletions book_keeper/lib/auth/repository/auth_repo.g.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@

import 'dart:convert';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:http/http.dart' as http;
part 'auth_repo.g.dart';

@riverpod
Stream<User?> authStatusChanges(AuthStatusChangesRef ref) {
return FirebaseAuth.instance.authStateChanges();
}
part 'custom_auth_repository.g.dart';

abstract class CustomAuth {
Future<void> logInWithEmailAndPassword({
required String email,
required String password,
});

Future<void> signUpWithEmailAndPassword({
required String name,
required String email,
required String password,
});
// Future<void> refreshAccessToken();
// Future<void> getUser();
}

abstract class FireAuthRepo {
User? currentUser();
Future<void> signInWithGoogle();
Future<void> signOut();
}

class CustomAuthRepo implements CustomAuth {
// @override
// Future<void> getUser() {
// throw UnimplementedError();
// }
final baseUrl = dotenv.env['BASE_URL'];

@override
Future<void> logInWithEmailAndPassword(
{required String email, required String password}) async {
Expand All @@ -60,11 +45,6 @@ class CustomAuthRepo implements CustomAuth {
}
}

// @override
// Future<void> refreshAccessToken() {
// throw UnimplementedError();
// }

@override
Future<void> signUpWithEmailAndPassword({
required String name,
Expand Down Expand Up @@ -94,37 +74,4 @@ class CustomAuthRepo implements CustomAuth {
@riverpod
CustomAuth customAuthRepo(CustomAuthRepoRef ref) {
return CustomAuthRepo();
}

class FirebaseAuthRepo implements FireAuthRepo {
final GoogleSignIn _googleSignIn = GoogleSignIn();

@override
Future<void> signInWithGoogle() async {
final googleUser = await _googleSignIn.signIn();
if (googleUser == null) {
return;
}
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
}

@override
Future<void> signOut() async {
await FirebaseAuth.instance.signOut();
}

@override
User? currentUser() {
return FirebaseAuth.instance.currentUser;
}
}

@riverpod
FireAuthRepo firebaseAuthRepo(FirebaseAuthRepoRef ref) {
return FirebaseAuthRepo();
}
}
49 changes: 49 additions & 0 deletions book_keeper/lib/auth/repository/firebase_auth_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'firebase_auth_repository.g.dart';

@riverpod
Stream<User?> authStatusChanges(AuthStatusChangesRef ref) {
return FirebaseAuth.instance.authStateChanges();
}

abstract class FireAuthRepo {
User? currentUser();
Future<void> signInWithGoogle();
Future<void> signOut();
}

class FirebaseAuthRepo implements FireAuthRepo {
final GoogleSignIn _googleSignIn = GoogleSignIn();

@override
Future<void> signInWithGoogle() async {
final googleUser = await _googleSignIn.signIn();
if (googleUser == null) {
return;
}
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
}

@override
Future<void> signOut() async {
await FirebaseAuth.instance.signOut();
}

@override
User? currentUser() {
return FirebaseAuth.instance.currentUser;
}
}

@riverpod
FireAuthRepo firebaseAuthRepo(FirebaseAuthRepoRef ref) {
return FirebaseAuthRepo();
}

0 comments on commit c2fd64c

Please sign in to comment.