Skip to content

Commit a0bd1ba

Browse files
committed
Added Email Auth himanshusharma89#27
1 parent 72085b0 commit a0bd1ba

File tree

4 files changed

+612
-396
lines changed

4 files changed

+612
-396
lines changed

lib/services/auth_service.dart

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import 'package:google_sign_in/google_sign_in.dart';
22
import 'package:firebase_auth/firebase_auth.dart';
3+
import 'package:retro_shopping/helpers/constants.dart';
4+
import 'package:flutter/material.dart';
35

46
class AuthenticationService {
7+
final FirebaseAuth _auth = FirebaseAuth.instance;
8+
59
static Future<String> signInWithGoogle() async {
610
final GoogleSignInAccount googleSignInAccount =
711
await GoogleSignIn().signIn();
@@ -27,4 +31,102 @@ class AuthenticationService {
2731
static Future<void> signOutGoogle() async {
2832
await GoogleSignIn().signOut();
2933
}
34+
35+
String userEmailValidation(String value, String errorMessage) {
36+
if (value.isEmpty) {
37+
return 'Required';
38+
} else if (errorMessage != null) {
39+
if (!errorMessage.contains('Password') ||
40+
errorMessage.contains('Invalid Request')) {
41+
return errorMessage;
42+
}
43+
return null;
44+
}
45+
return null;
46+
}
47+
48+
String userPasswordValidation(String value, String errorMessage) {
49+
if (value.isEmpty) {
50+
return 'Required';
51+
} else if (errorMessage != null) {
52+
if (errorMessage.contains('Password') ||
53+
errorMessage.contains('Invalid Request')) {
54+
return errorMessage;
55+
}
56+
return null;
57+
}
58+
return null;
59+
}
60+
61+
String userConfirmPasswordValidation(
62+
String value, String password, String confirmPassword) {
63+
if (value.isEmpty) {
64+
return 'Required';
65+
} else if (password != confirmPassword) {
66+
return 'Passwords Do Not Match';
67+
}
68+
return null;
69+
}
70+
71+
Future<String> userSignUp(String errorText, BuildContext context,
72+
String email, String password) async {
73+
String errorTemp = errorText;
74+
try {
75+
final UserCredential newUser = await _auth.createUserWithEmailAndPassword(
76+
email: email, password: password);
77+
if (newUser != null) {
78+
Navigator.of(context)
79+
.pushReplacementNamed(RouteConstant.DASHBOARD_SCREEN);
80+
}
81+
} catch (e) {
82+
if (e.toString().contains('invalid-email')) {
83+
errorTemp = 'Invalid Email';
84+
} else if (e.toString().contains('email-already-in-use')) {
85+
errorTemp = 'User Already Exists';
86+
} else if (e.toString().contains('weak-password')) {
87+
errorTemp = 'Password Too Short';
88+
} else {
89+
errorTemp = 'Invalid Request';
90+
}
91+
return errorTemp;
92+
}
93+
return null;
94+
}
95+
96+
Future<String> userLogin(String errorText, BuildContext context, String email,
97+
String password) async {
98+
String errorTemp = errorText;
99+
try {
100+
final UserCredential existingUser = await _auth
101+
.signInWithEmailAndPassword(email: email, password: password);
102+
if (existingUser != null) {
103+
Navigator.of(context).pushReplacementNamed(
104+
RouteConstant.DASHBOARD_SCREEN,
105+
);
106+
}
107+
} catch (e) {
108+
if (e.toString().contains('invalid-email')) {
109+
errorTemp = 'Invalid Email';
110+
} else if (e.toString().contains('user-not-found')) {
111+
errorTemp = 'User Not Found';
112+
} else if (e.toString().contains('wrong-password')) {
113+
errorTemp = 'Incorrect Password';
114+
} else {
115+
errorTemp = 'Invalid Request';
116+
}
117+
return errorTemp;
118+
}
119+
return null;
120+
}
121+
122+
Future<void> userSignOut(BuildContext context) async {
123+
final User user = _auth.currentUser;
124+
if (user != null) {
125+
await _auth.signOut();
126+
Navigator.of(context).pushNamedAndRemoveUntil(
127+
RouteConstant.LOGIN_SCREEN,
128+
(Route<dynamic> route) => false,
129+
);
130+
}
131+
}
30132
}

0 commit comments

Comments
 (0)