email y contraseña registro
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:prosappco/src/authentication/exceptions/register_failed.dart';
|
||||
import 'package:prosappco/src/screens/login.dart';
|
||||
import 'package:prosappco/src/screens/welcome.dart';
|
||||
|
||||
class AuthenticationRepository extends GetxController {
|
||||
static AuthenticationRepository get instance => Get.find();
|
||||
|
||||
//Variables
|
||||
final _auth = FirebaseAuth.instance;
|
||||
late final Rx<User?> firebaseUser;
|
||||
final GoogleSignIn googleSignIn = GoogleSignIn();
|
||||
|
||||
@override
|
||||
void onReady() {
|
||||
Future.delayed(const Duration(seconds: 6));
|
||||
firebaseUser = Rx<User?>(_auth.currentUser);
|
||||
firebaseUser.bindStream(_auth.userChanges());
|
||||
ever(firebaseUser, _setInitialScreen);
|
||||
}
|
||||
|
||||
_setInitialScreen(User? user) {
|
||||
user == null
|
||||
? Get.offAll(() => const LoginScreen())
|
||||
: Get.offAll(() => const WelcomeScreen());
|
||||
}
|
||||
|
||||
Future<void> createUserWithEmailAndPassword(
|
||||
String email, String password) async {
|
||||
try {
|
||||
await _auth.createUserWithEmailAndPassword(
|
||||
email: email, password: password);
|
||||
firebaseUser.value != null
|
||||
? Get.offAll(() => WelcomeScreen())
|
||||
: Get.to(() => LoginScreen());
|
||||
} on FirebaseAuthException catch (e) {
|
||||
final ex = SignUpWithEmailAndPasswordFailure.code(e.code);
|
||||
print('FIREBASE AUTH EXCEPTION - ${ex.message}');
|
||||
} catch (_) {
|
||||
const ex = SignUpWithEmailAndPasswordFailure();
|
||||
print('EXCEPTION - ${ex.message}');
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loginWithEmailAndPassword(String email, String password) async {
|
||||
try {
|
||||
await _auth.signInWithEmailAndPassword(email: email, password: password);
|
||||
} on FirebaseAuthException catch (e) {
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<UserCredential> signInWithGoogle() async {
|
||||
final GoogleSignInAccount? googleSignInAccount =
|
||||
await googleSignIn.signIn();
|
||||
final GoogleSignInAuthentication googleSignInAuthentication =
|
||||
await googleSignInAccount!.authentication;
|
||||
|
||||
final AuthCredential credential = GoogleAuthProvider.credential(
|
||||
accessToken: googleSignInAuthentication.accessToken,
|
||||
idToken: googleSignInAuthentication.idToken,
|
||||
);
|
||||
|
||||
return await _auth.signInWithCredential(credential);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
class SignUpWithEmailAndPasswordFailure {
|
||||
final String message;
|
||||
|
||||
const SignUpWithEmailAndPasswordFailure(
|
||||
[this.message = "An Unknown error ocurred."]);
|
||||
|
||||
factory SignUpWithEmailAndPasswordFailure.code(String code) {
|
||||
switch (code) {
|
||||
case 'weak-password':
|
||||
return const SignUpWithEmailAndPasswordFailure(
|
||||
'Please enter a stronger password.');
|
||||
case 'email-alredy-in-use':
|
||||
return const SignUpWithEmailAndPasswordFailure(
|
||||
'An account alredy exists for that email.');
|
||||
default:
|
||||
return const SignUpWithEmailAndPasswordFailure();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user