|
1 | 1 | //here will be the code snippets of firebase auth
|
| 2 | +import 'package:book_store/Screens/Home/mainPage.dart'; |
| 3 | +import 'package:book_store/Screens/Registration/login.dart'; |
| 4 | +import 'package:book_store/Screens/Registration/verifyEmail.dart'; |
| 5 | +import 'package:firebase_auth/firebase_auth.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:flutter/widgets.dart'; |
| 8 | +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; |
| 9 | +import 'package:google_sign_in/google_sign_in.dart'; |
| 10 | + |
| 11 | +class AuthClass { |
| 12 | + final FirebaseAuth _auth = FirebaseAuth.instance; |
| 13 | + final GoogleSignIn _googleSignIn = GoogleSignIn(); |
| 14 | + |
| 15 | + final storage = new FlutterSecureStorage(); |
| 16 | + |
| 17 | +//Sign in with email |
| 18 | + Future<void>? signInWithEmail( |
| 19 | + {required String email, |
| 20 | + required String password, |
| 21 | + required BuildContext context}) async { |
| 22 | + try { |
| 23 | + UserCredential userCredential = await FirebaseAuth.instance |
| 24 | + .signInWithEmailAndPassword(email: email, password: password); |
| 25 | + User? user = _auth.currentUser; |
| 26 | + if (user != null && !user.emailVerified) { |
| 27 | + showSnackBar(context, 'Email was not verified'); |
| 28 | + await user.sendEmailVerification(); |
| 29 | + |
| 30 | + Navigator.pushNamed(context, VerifyEmail.routeName, |
| 31 | + arguments: VerifyEmailArguments(email)); |
| 32 | + } else { |
| 33 | + Navigator.pushNamedAndRemoveUntil( |
| 34 | + context, MainPage.routeName, (route) => false); |
| 35 | + } |
| 36 | + } on FirebaseAuthException catch (e) { |
| 37 | + if (e.code == 'user-not-found') { |
| 38 | + showSnackBar(context, 'No user found for that email.'); |
| 39 | + } else if (e.code == 'wrong-password') { |
| 40 | + showSnackBar(context, 'Wrong password provided for that user.'); |
| 41 | + } |
| 42 | + } catch (e) { |
| 43 | + showSnackBar(context, e.toString()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | +//Sign up with Email |
| 48 | + Future<void>? signUpWithEmail( |
| 49 | + {required String email, |
| 50 | + required String password, |
| 51 | + required String confirmPassword, |
| 52 | + required BuildContext context}) async { |
| 53 | + if (password == confirmPassword) { |
| 54 | + try { |
| 55 | + UserCredential userCredential = await _auth |
| 56 | + .createUserWithEmailAndPassword(email: email, password: password); |
| 57 | + print(userCredential.user!.uid); |
| 58 | + User? user = _auth.currentUser; |
| 59 | + var uid = _auth.currentUser!.uid; |
| 60 | + |
| 61 | + if (user != null && !user.emailVerified) { |
| 62 | + await user.sendEmailVerification(); |
| 63 | + } |
| 64 | + |
| 65 | + // storeTokenAndData(userCredential); |
| 66 | + |
| 67 | + Navigator.pushNamed(context, VerifyEmail.routeName, |
| 68 | + arguments: VerifyEmailArguments(email)); |
| 69 | + } on FirebaseAuthException catch (e) { |
| 70 | + if (e.code == 'weak-password') { |
| 71 | + showSnackBar(context, 'Password should atleast be of 6 characters'); |
| 72 | + } else if (e.code == 'email-already-in-use') { |
| 73 | + User? user = _auth.currentUser; |
| 74 | + if (user != null && !user.emailVerified) { |
| 75 | + showSnackBar(context, |
| 76 | + 'This email is already taken please login, (Email verification required)'); |
| 77 | + } else { |
| 78 | + showSnackBar(context, 'This email is already taken'); |
| 79 | + } |
| 80 | + } |
| 81 | + } catch (e) { |
| 82 | + showSnackBar(context, e.toString()); |
| 83 | + } |
| 84 | + } else { |
| 85 | + showSnackBar(context, "Passwords didn't matched"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +// Reset Password |
| 90 | + Future<void> resetPassword( |
| 91 | + {required String email, required BuildContext context}) async { |
| 92 | + try { |
| 93 | + await _auth.sendPasswordResetEmail( |
| 94 | + email: email, |
| 95 | + ); |
| 96 | + showModalSheet(context, |
| 97 | + 'A password reset link has been sent to your mail. Please reset your password there and login back again'); |
| 98 | + } on FirebaseAuthException catch (e) { |
| 99 | + if (e.code == 'invalid-email') { |
| 100 | + showSnackBar(context, 'Invalid email address'); |
| 101 | + } else if (e.code == 'user-not-found') { |
| 102 | + showSnackBar(context, 'No user found with this email address'); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +//Google sign In |
| 108 | + Future<void> googleSignIn(BuildContext context) async { |
| 109 | + try { |
| 110 | + GoogleSignInAccount? googleSignInAccount = await _googleSignIn.signIn(); |
| 111 | + GoogleSignInAuthentication googleSignInAuthentication = |
| 112 | + await googleSignInAccount!.authentication; |
| 113 | + AuthCredential credential = GoogleAuthProvider.credential( |
| 114 | + accessToken: googleSignInAuthentication.accessToken, |
| 115 | + idToken: googleSignInAuthentication.idToken, |
| 116 | + ); |
| 117 | + UserCredential userCredential = |
| 118 | + await _auth.signInWithCredential(credential); |
| 119 | + storeTokenAndData(userCredential); |
| 120 | + Navigator.pushNamedAndRemoveUntil( |
| 121 | + context, MainPage.routeName, (route) => false); |
| 122 | + } catch (e) { |
| 123 | + showSnackBar(context, e.toString()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + Future<String?> googleSignIn1(BuildContext context) async { |
| 128 | + try { |
| 129 | + final GoogleSignInAccount? googleSignInAccount = |
| 130 | + await _googleSignIn.signIn(); |
| 131 | + final GoogleSignInAuthentication googleSignInAuthentication = |
| 132 | + await googleSignInAccount!.authentication; |
| 133 | + final AuthCredential credential = GoogleAuthProvider.credential( |
| 134 | + accessToken: googleSignInAuthentication.accessToken, |
| 135 | + idToken: googleSignInAuthentication.idToken, |
| 136 | + ); |
| 137 | + await _auth.signInWithCredential(credential); |
| 138 | + Navigator.pushNamedAndRemoveUntil( |
| 139 | + context, MainPage.routeName, (route) => false); |
| 140 | + } on FirebaseAuthException catch (e) { |
| 141 | + showSnackBar(context, e.toString()); |
| 142 | + print(e.message); |
| 143 | + throw e; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + //Sign Out |
| 148 | + Future<void> signOut({required BuildContext context}) async { |
| 149 | + try { |
| 150 | + await _googleSignIn.signOut(); |
| 151 | + await _auth.signOut(); |
| 152 | + await storage.delete(key: "token"); |
| 153 | + Navigator.pushNamedAndRemoveUntil( |
| 154 | + context, LoginScreen.routeName, (route) => false); |
| 155 | + } catch (e) { |
| 156 | + showSnackBar(context, e.toString()); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + void storeTokenAndData(UserCredential userCredential) async { |
| 161 | + print("storing token and data"); |
| 162 | + await storage.write( |
| 163 | + key: "token", value: userCredential.credential!.token.toString()); |
| 164 | + await storage.write( |
| 165 | + key: "usercredential", value: userCredential.toString()); |
| 166 | + } |
| 167 | + |
| 168 | + Future<String?> getToken() async { |
| 169 | + return await storage.read(key: "token"); |
| 170 | + } |
| 171 | + |
| 172 | + void showSnackBar(BuildContext context, String text) { |
| 173 | + final snackBar = SnackBar(content: Text(text)); |
| 174 | + ScaffoldMessenger.of(context).showSnackBar(snackBar); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +void showModalSheet(BuildContext context, String text) { |
| 179 | + showModalBottomSheet<void>( |
| 180 | + context: context, |
| 181 | + builder: (BuildContext context) { |
| 182 | + return Container( |
| 183 | + height: 200, |
| 184 | + color: Colors.white, |
| 185 | + child: Padding( |
| 186 | + padding: const EdgeInsets.symmetric(horizontal: 20), |
| 187 | + child: Column( |
| 188 | + mainAxisAlignment: MainAxisAlignment.center, |
| 189 | + children: [ |
| 190 | + Text( |
| 191 | + text, |
| 192 | + style: TextStyle(fontSize: 16, color: Colors.black), |
| 193 | + ), |
| 194 | + SizedBox( |
| 195 | + height: 20, |
| 196 | + ), |
| 197 | + ElevatedButton( |
| 198 | + child: const Text('Ok'), |
| 199 | + onPressed: () => Navigator.pop(context), |
| 200 | + ) |
| 201 | + ], |
| 202 | + ), |
| 203 | + ), |
| 204 | + ); |
| 205 | + }, |
| 206 | + ); |
| 207 | +} |
0 commit comments