Skip to content

Instantly share code, notes, and snippets.

@coder-Aayush
coder-Aayush / authentication_state.dart
Created November 22, 2022 18:35
Authentication State
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;
@coder-Aayush
coder-Aayush / authentication_provider.dart
Created November 22, 2022 18:14
Authentication State Provider
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!),
);
}
@coder-Aayush
coder-Aayush / authentication_provider.dart
Created November 12, 2022 13:51
Authentication State Provider
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;
}
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;
@coder-Aayush
coder-Aayush / auth_remote_source.dart
Created November 12, 2022 12:36
Auth Remote Source (Firebase)
final authDataSourceProvider = Provider<AuthDataSource>(
(ref) => AuthDataSource(ref.read(firebaseAuthProvider), ref),
);
@coder-Aayush
coder-Aayush / auth_remote_source.dart
Created November 12, 2022 11:58
Auth Remote Source (Firebase)
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);
@coder-Aayush
coder-Aayush / auth_remote_source.dart
Created November 8, 2022 05:11
Authentication using Firebase (Google)
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,
@coder-Aayush
coder-Aayush / auth_remote_source.dart
Created November 8, 2022 05:03
Authentication using Firebase
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');
@coder-Aayush
coder-Aayush / auth_remote_source.dart
Last active November 8, 2022 04:57
Authentication using Firebase
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.');
}
}
@coder-Aayush
coder-Aayush / auth_remote_source.dart
Last active November 8, 2022 04:45
Authentication using Firebase
class AuthRemoteSource {
final FirebaseAuth _firebaseAuth;
final Ref _ref; // use for reading other providers
AuthRemoteSource(this._firebaseAuth, this._ref);
}