This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:firebase_auth/firebase_auth.dart'; | |
| import 'package:freezed_annotation/freezed_annotation.dart'; | |
| part "authentication_state.freezed.dart"; | |
| @freezed | |
| class AuthenticationState with _$AuthenticationState { | |
| const factory AuthenticationState.initial() = _Initial; | |
| const factory AuthenticationState.loading() = _Loading; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Future<void> login({required String email, required String password}) async { | |
| state = const AuthenticationState.loading(); | |
| final response = await _dataSource.login(email: email, password: password); | |
| state = response.fold( | |
| (error) => AuthenticationState.unauthenticated(message: error), | |
| (response) => AuthenticationState.authenticated(user: response!), | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:firebase_auth_riverpod/src/feature/auth/data_source/auth_data_source.dart'; | |
| import 'package:firebase_auth_riverpod/src/feature/auth/providers/state/authentication_state.dart'; | |
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | |
| class AuthNotifier extends StateNotifier<AuthenticationState> { | |
| AuthNotifier(this._dataSource) : super(const AuthenticationState.initial()); | |
| final AuthDataSource _dataSource; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:firebase_auth/firebase_auth.dart'; | |
| import 'package:freezed_annotation/freezed_annotation.dart'; | |
| part "authentication_state.freezed.dart"; | |
| @freezed | |
| class AuthenticationState with _$AuthenticationState { | |
| const factory AuthenticationState.initial() = _Initial; | |
| const factory AuthenticationState.loading() = _Loading; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final authDataSourceProvider = Provider<AuthDataSource>( | |
| (ref) => AuthDataSource(ref.read(firebaseAuthProvider), ref), | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:dartz/dartz.dart'; | |
| import 'package:firebase_auth/firebase_auth.dart'; | |
| import 'package:google_sign_in/google_sign_in.dart'; | |
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | |
| class AuthDataSource { | |
| final FirebaseAuth _firebaseAuth; | |
| final Ref _ref; // use for reading other providers | |
| AuthDataSource(this._firebaseAuth, this._ref); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Future<Either<String, User>> continueWithGoogle() async { | |
| try { | |
| final googleSignIn = GoogleSignIn(); | |
| final GoogleSignInAccount? googleUser = await googleSignIn.signIn(); | |
| if (googleUser != null) { | |
| final GoogleSignInAuthentication googleAuth = | |
| await googleUser.authentication; | |
| final AuthCredential credential = GoogleAuthProvider.credential( | |
| accessToken: googleAuth.accessToken, | |
| idToken: googleAuth.idToken, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Future<Either<String, User?>> login( | |
| {required String email, required String password}) async { | |
| try { | |
| final response = await _firebaseAuth.signInWithEmailAndPassword( | |
| email: email, | |
| password: password, | |
| ); | |
| return right(response.user); | |
| } on FirebaseAuthException catch (e) { | |
| return left(e.message ?? 'Failed to Login'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Future<Either<String, User>> signup( | |
| {required String email, required String password}) async { | |
| try { | |
| final response = await _firebaseAuth.createUserWithEmailAndPassword( | |
| email: email, password: password); | |
| return right(response.user!); | |
| } on FirebaseAuthException catch (e) { | |
| return left(e.message ?? 'Failed to Signup.'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AuthRemoteSource { | |
| final FirebaseAuth _firebaseAuth; | |
| final Ref _ref; // use for reading other providers | |
| AuthRemoteSource(this._firebaseAuth, this._ref); | |
| } |