Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Email Auth #27 #162

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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