Skip to content

Commit

Permalink
Merge pull request #162 from puru-ganda/EmailAuthentication
Browse files Browse the repository at this point in the history
Added Email Auth #27
  • Loading branch information
himanshusharma89 authored Mar 25, 2021
2 parents 72085b0 + a0bd1ba commit 7e56ba1
Show file tree
Hide file tree
Showing 4 changed files with 612 additions and 396 deletions.
102 changes: 102 additions & 0 deletions lib/services/auth_service.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:retro_shopping/helpers/constants.dart';
import 'package:flutter/material.dart';

class AuthenticationService {
final FirebaseAuth _auth = FirebaseAuth.instance;

static Future<String> signInWithGoogle() async {
final GoogleSignInAccount googleSignInAccount =
await GoogleSignIn().signIn();
Expand All @@ -27,4 +31,102 @@ class AuthenticationService {
static Future<void> signOutGoogle() async {
await GoogleSignIn().signOut();
}

String userEmailValidation(String value, String errorMessage) {
if (value.isEmpty) {
return 'Required';
} else if (errorMessage != null) {
if (!errorMessage.contains('Password') ||
errorMessage.contains('Invalid Request')) {
return errorMessage;
}
return null;
}
return null;
}

String userPasswordValidation(String value, String errorMessage) {
if (value.isEmpty) {
return 'Required';
} else if (errorMessage != null) {
if (errorMessage.contains('Password') ||
errorMessage.contains('Invalid Request')) {
return errorMessage;
}
return null;
}
return null;
}

String userConfirmPasswordValidation(
String value, String password, String confirmPassword) {
if (value.isEmpty) {
return 'Required';
} else if (password != confirmPassword) {
return 'Passwords Do Not Match';
}
return null;
}

Future<String> userSignUp(String errorText, BuildContext context,
String email, String password) async {
String errorTemp = errorText;
try {
final UserCredential newUser = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
if (newUser != null) {
Navigator.of(context)
.pushReplacementNamed(RouteConstant.DASHBOARD_SCREEN);
}
} catch (e) {
if (e.toString().contains('invalid-email')) {
errorTemp = 'Invalid Email';
} else if (e.toString().contains('email-already-in-use')) {
errorTemp = 'User Already Exists';
} else if (e.toString().contains('weak-password')) {
errorTemp = 'Password Too Short';
} else {
errorTemp = 'Invalid Request';
}
return errorTemp;
}
return null;
}

Future<String> userLogin(String errorText, BuildContext context, String email,
String password) async {
String errorTemp = errorText;
try {
final UserCredential existingUser = await _auth
.signInWithEmailAndPassword(email: email, password: password);
if (existingUser != null) {
Navigator.of(context).pushReplacementNamed(
RouteConstant.DASHBOARD_SCREEN,
);
}
} catch (e) {
if (e.toString().contains('invalid-email')) {
errorTemp = 'Invalid Email';
} else if (e.toString().contains('user-not-found')) {
errorTemp = 'User Not Found';
} else if (e.toString().contains('wrong-password')) {
errorTemp = 'Incorrect Password';
} else {
errorTemp = 'Invalid Request';
}
return errorTemp;
}
return null;
}

Future<void> userSignOut(BuildContext context) async {
final User user = _auth.currentUser;
if (user != null) {
await _auth.signOut();
Navigator.of(context).pushNamedAndRemoveUntil(
RouteConstant.LOGIN_SCREEN,
(Route<dynamic> route) => false,
);
}
}
}
Loading

0 comments on commit 7e56ba1

Please sign in to comment.