Skip to content

Commit

Permalink
Updoots
Browse files Browse the repository at this point in the history
  • Loading branch information
lockieRichter committed Nov 19, 2023
1 parent 2f48086 commit 55affd9
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 553 deletions.
31 changes: 10 additions & 21 deletions lib/src/data/authentication/firebase_authentication_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import 'package:collection/collection.dart';
import 'package:dantex/src/data/authentication/authentication_repository.dart';
import 'package:dantex/src/data/authentication/entity/dante_user.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter/foundation.dart' show kIsWeb;

class FirebaseAuthenticationRepository implements AuthenticationRepository {
final FirebaseAuth _fbAuth;
final GoogleSignIn _googleSignIn;

FirebaseAuthenticationRepository(this._fbAuth, this._googleSignIn);
FirebaseAuthenticationRepository(this._fbAuth);

@override
Stream<DanteUser?> get authStateChanges =>
Expand Down Expand Up @@ -90,26 +89,16 @@ class FirebaseAuthenticationRepository implements AuthenticationRepository {
}

@override
Future<UserCredential> loginWithGoogle() async {
final GoogleSignInAccount? googleSignInAccount =
await _googleSignIn.signIn();

if (googleSignInAccount == null) {
throw FirebaseAuthException(
message: 'Google sign in aborted by user',
code: 'ERROR_ABORTED_BY_USER',
Future<UserCredential> loginWithGoogle() {
if (kIsWeb) {
return _fbAuth.signInWithPopup(
GoogleAuthProvider(),
);
} else {
return _fbAuth.signInWithProvider(
GoogleAuthProvider(),
);
}

final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;

final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);

return _fbAuth.signInWithCredential(credential);
}

@override
Expand Down
5 changes: 5 additions & 0 deletions lib/src/data/google_drive/google_drive_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:dantex/src/data/book/entity/book_state.dart';
import 'package:dantex/src/data/google_drive/backup_client.dart';
import 'package:dantex/src/data/google_drive/entity/backup_data.dart';
import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis/drive/v3.dart';

Expand All @@ -15,6 +16,10 @@ class GoogleDriveClient extends BackupClient {
GoogleDriveClient({required this.googleSignIn});

Future<DriveApi> _getDriveApi() async {
if (kIsWeb && googleSignIn.currentUser == null) {
await googleSignIn.signIn();
}

final user = await googleSignIn.signInSilently();
if (user == null) {
await googleSignIn.signIn();
Expand Down
5 changes: 3 additions & 2 deletions lib/src/providers/authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ AuthenticationRepository authenticationRepository(
) =>
FirebaseAuthenticationRepository(
ref.watch(firebaseAuthProvider),
ref.watch(googleSignInProvider),
);

@riverpod
Expand All @@ -43,11 +42,13 @@ Future<DanteUser?> user(UserRef ref) {
return ref.watch(authenticationRepositoryProvider).getAccount();
}

@Riverpod(keepAlive: true)
@riverpod
GoogleSignIn googleSignIn(
GoogleSignInRef ref,
) =>
GoogleSignIn(
clientId:
'150599422814-moto7djse1tf7vtso7slemniki76ohg6.apps.googleusercontent.com',
scopes: [
DriveApi.driveFileScope,
],
Expand Down
4 changes: 2 additions & 2 deletions lib/src/providers/google_drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'google_drive.g.dart';

@Riverpod(keepAlive: true)
@riverpod
BackupClient googleDriveClient(GoogleDriveClientRef ref) =>
GoogleDriveClient(googleSignIn: ref.watch(googleSignInProvider));

@riverpod
Future<List<BackupData>> listGoogleDriveBackups(
ListGoogleDriveBackupsRef ref,
) async {
return ref.watch(googleDriveClientProvider).listBackups();
return ref.read(googleDriveClientProvider).listBackups();
}

@riverpod
Expand Down
1 change: 1 addition & 0 deletions lib/src/ui/login/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class LoginPageState extends ConsumerState<LoginPage> {
],
),
),
const SizedBox(height: 8),
DanteOutlinedButton(
onPressed: () => context.pushReplacement(
DanteRoute.emailLogin.navigationUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:dantex/src/data/authentication/entity/dante_user.dart';
import 'package:dantex/src/data/authentication/firebase_authentication_repository.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

Expand All @@ -15,20 +14,14 @@ import 'firebase_authentication_repository_test.mocks.dart';
UserCredential,
User,
UserInfo,
GoogleSignIn,
GoogleSignInAccount,
GoogleSignInAuthentication,
GoogleAuthProvider,
AuthCredential,
])
void main() {
const String testEmail = '[email protected]';
const String testPassword = 'password';

test('Get account returns DanteUser', () async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final fbAuthRepo = FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final fbAuthRepo = FirebaseAuthenticationRepository(fbAuth);
final User user = MockUser();
final UserInfo userInfo = MockUserInfo();
final DanteUser danteUser = DanteUser(
Expand Down Expand Up @@ -63,33 +56,18 @@ void main() {
group('Login', () {
test('Login with Google returns user credential on success', () async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);
final userCred = MockUserCredential();
final googleSignInAccount = MockGoogleSignInAccount();
final googleSignInAuthentication = MockGoogleSignInAuthentication();
when(googleSignIn.signIn()).thenAnswer((_) async => googleSignInAccount);
when(googleSignInAccount.authentication)
.thenAnswer((_) async => googleSignInAuthentication);
when(googleSignInAuthentication.accessToken).thenReturn('accessToken');
when(googleSignInAuthentication.idToken).thenReturn('idToken');

when(fbAuth.signInWithCredential(any)).thenAnswer((_) async => userCred);
when(fbAuth.signInWithProvider(any)).thenAnswer((_) async => userCred);

expect(await firebaseAuthRepo.loginWithGoogle(), userCred);
verify(fbAuth.signInWithCredential(any)).called(1);
verify(googleSignIn.signIn()).called(1);
verify(googleSignInAccount.authentication).called(1);
verify(googleSignInAuthentication.accessToken).called(1);
verify(googleSignInAuthentication.idToken).called(1);
verify(fbAuth.signInWithProvider(any)).called(1);
});

test('Login anonymously returns User Credential on success', () async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);
final userCred = MockUserCredential();

when(fbAuth.signInAnonymously()).thenAnswer((_) async => userCred);
Expand All @@ -100,9 +78,7 @@ void main() {

test('Login with email returns User Credential on success', () async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);
final userCred = MockUserCredential();

when(
Expand Down Expand Up @@ -130,9 +106,7 @@ void main() {

test('logout returns void on success', () async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);

when(fbAuth.signOut()).thenAnswer((_) async => Future<void>);

Expand All @@ -144,9 +118,7 @@ void main() {
'fetchSignInMethodsForEmail maps sign in methods to AuthenticationSource',
() async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);

when(fbAuth.fetchSignInMethodsForEmail(testEmail))
.thenAnswer((_) async => ['google.com', 'password', 'apple.com']);
Expand All @@ -165,9 +137,7 @@ void main() {

test('Create mail account returns User Credential on success', () async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);
final userCred = MockUserCredential();

when(
Expand Down Expand Up @@ -195,9 +165,7 @@ void main() {
test('Upgrade anonymous account returns User Credential on success',
() async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);
final user = MockUser();
final userCred = MockUserCredential();

Expand All @@ -222,9 +190,7 @@ void main() {
'Upgrade anonymous account throws FirebaseAuthException error when user not found',
() async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);

when(
fbAuth.currentUser,
Expand All @@ -241,9 +207,7 @@ void main() {

test('Update user password returns void on success', () {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);
final user = MockUser();

when(fbAuth.currentUser).thenReturn(user);
Expand All @@ -259,9 +223,7 @@ void main() {
'Upgrade password throws FirebaseAuthException error when user not found',
() async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);

when(
fbAuth.currentUser,
Expand All @@ -277,9 +239,7 @@ void main() {

test('Send password reset request returns void on success', () {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);

when(fbAuth.sendPasswordResetEmail(email: testEmail))
.thenAnswer((_) async => Future<void>);
Expand All @@ -290,9 +250,7 @@ void main() {

test('Delete user returns void on success', () async {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);
final User user = MockUser();

when(fbAuth.currentUser).thenReturn(user);
Expand All @@ -305,9 +263,7 @@ void main() {

test('Auth state changes returns DanteUser', () {
final fbAuth = MockFirebaseAuth();
final googleSignIn = MockGoogleSignIn();
final firebaseAuthRepo =
FirebaseAuthenticationRepository(fbAuth, googleSignIn);
final firebaseAuthRepo = FirebaseAuthenticationRepository(fbAuth);
final User user = MockUser();
final UserInfo userInfo = MockUserInfo();
final DanteUser danteUser = DanteUser(
Expand Down
Loading

0 comments on commit 55affd9

Please sign in to comment.