diff --git a/assets/images/google.png b/assets/images/google.png new file mode 100644 index 0000000..494aced Binary files /dev/null and b/assets/images/google.png differ diff --git a/lib/cubit/auth/auth_cubit.dart b/lib/cubit/auth/auth_cubit.dart index 6de63a3..6979d08 100644 --- a/lib/cubit/auth/auth_cubit.dart +++ b/lib/cubit/auth/auth_cubit.dart @@ -1,10 +1,12 @@ import 'package:donorconnect/views/pages/welcome/welcome_screen.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:donorconnect/cubit/auth/auth_state.dart'; import 'package:donorconnect/models/user_model.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:google_sign_in/google_sign_in.dart'; import 'package:shared_preferences/shared_preferences.dart'; class AuthCubit extends Cubit { @@ -58,6 +60,60 @@ class AuthCubit extends Cubit { } } + Future signInWithGoogle() async { + GoogleSignInAccount? googleUser = await GoogleSignIn().signIn(); + GoogleSignInAuthentication? googleAuth = await googleUser?.authentication; + try { + AuthCredential credential = GoogleAuthProvider.credential( + accessToken: googleAuth?.accessToken, + idToken: googleAuth?.idToken, + ); + + await FirebaseAuth.instance + .signInWithCredential(credential) + .whenComplete(() {}); + UserModel userModel = UserModel( + uid: FirebaseAuth.instance.currentUser!.uid, + name: FirebaseAuth.instance.currentUser!.displayName!, + email: FirebaseAuth.instance.currentUser!.email!, + phone: FirebaseAuth.instance.currentUser!.phoneNumber ?? '', + isOrganDonor: false, + isBloodDonor: false, + ); + _firestore + .collection('users') + .doc(FirebaseAuth.instance.currentUser!.uid) + .snapshots() + .listen( + (DocumentSnapshot snapshot) { + if (snapshot.exists) { + UserModel user = + UserModel.fromMap(snapshot.data() as Map); + emit(Authenticated(user)); + print(user.name); + print(user.email); + // Save user name to SharedPreferences + _saveUserToPrefs(FirebaseAuth.instance.currentUser!.uid, user); + } else { + _firestore + .collection('users') + .doc(FirebaseAuth.instance.currentUser!.uid) + .set(userModel.toMap()); + // Save user data to SharedPreferences + _saveUserToPrefs(FirebaseAuth.instance.currentUser!.uid, userModel); + emit(Authenticated(userModel)); + } + }, + onError: (error) { + emit(AuthError(error.toString())); + }, + ); + emit(Authenticated(userModel)); + } catch (e) { + print(e); + } + } + Future registerUser({ required String email, required String password, diff --git a/lib/views/pages/login/login.dart b/lib/views/pages/login/login.dart index 7089c11..c00425f 100644 --- a/lib/views/pages/login/login.dart +++ b/lib/views/pages/login/login.dart @@ -28,7 +28,7 @@ class _LoginPageState extends State { TextEditingController passwordController = TextEditingController(); //variable to control password visibility - bool _isPasswordVisible =false; + bool _isPasswordVisible = false; // VALIDATION bool _isValidate = false; late SharedPreferences prefs; @@ -158,16 +158,18 @@ class _LoginPageState extends State { name: _text.password, errormsg: _isValidate ? _text.password_error_text : null, - suffixIcon: IconButton( - icon: Icon( - _isPasswordVisible ? Icons.visibility : Icons.visibility_off, - ), - onPressed: () { - setState(() { - _isPasswordVisible = !_isPasswordVisible; - }); - }, - ), + suffixIcon: IconButton( + icon: Icon( + _isPasswordVisible + ? Icons.visibility + : Icons.visibility_off, + ), + onPressed: () { + setState(() { + _isPasswordVisible = !_isPasswordVisible; + }); + }, + ), ), ], ), @@ -177,16 +179,16 @@ class _LoginPageState extends State { padding: EdgeInsets.only(left: screenWidth * 0.45), child: TextButton( onPressed: () { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => BlocProvider( - create: (context) => ForgotPasswordCubit(FirebaseAuth.instance), - child: ForgotPasswordScreen(), - ), - ), -); - + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => BlocProvider( + create: (context) => ForgotPasswordCubit( + FirebaseAuth.instance), + child: ForgotPasswordScreen(), + ), + ), + ); }, child: Text( _text.forget_password, @@ -200,7 +202,7 @@ class _LoginPageState extends State { // LOGIN BUTTON SizedBox( - height: screenHeight * 0.18, + height: screenHeight * 0.12, ), GestureDetector( onTap: loginUser, @@ -225,7 +227,22 @@ class _LoginPageState extends State { ), ), ), - + + //Google Login + SizedBox( + height: screenHeight * 0.02, + ), + Container( + height: 50, + alignment: Alignment.center, + child: IconButton( + icon: Image.asset('assets/images/google.png'), + iconSize: 50, + onPressed: () { + context.read().signInWithGoogle(); + }, + ), + ), // FINAL TEXT Row( mainAxisAlignment: MainAxisAlignment.center, diff --git a/pubspec.lock b/pubspec.lock index 089054a..8193d1d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -570,6 +570,54 @@ packages: url: "https://pub.dev" source: hosted version: "6.2.1" + google_identity_services_web: + dependency: transitive + description: + name: google_identity_services_web + sha256: "5be191523702ba8d7a01ca97c17fca096822ccf246b0a9f11923a6ded06199b6" + url: "https://pub.dev" + source: hosted + version: "0.3.1+4" + google_sign_in: + dependency: "direct main" + description: + name: google_sign_in + sha256: "0b8787cb9c1a68ad398e8010e8c8766bfa33556d2ab97c439fb4137756d7308f" + url: "https://pub.dev" + source: hosted + version: "6.2.1" + google_sign_in_android: + dependency: transitive + description: + name: google_sign_in_android + sha256: "0928059d2f0840f63c7b07a30cf73b593ae872cdd0dbd46d1b9ba878d2599c01" + url: "https://pub.dev" + source: hosted + version: "6.1.33" + google_sign_in_ios: + dependency: transitive + description: + name: google_sign_in_ios + sha256: "83f015169102df1ab2905cf8abd8934e28f87db9ace7a5fa676998842fed228a" + url: "https://pub.dev" + source: hosted + version: "5.7.8" + google_sign_in_platform_interface: + dependency: transitive + description: + name: google_sign_in_platform_interface + sha256: "1f6e5787d7a120cc0359ddf315c92309069171306242e181c09472d1b00a2971" + url: "https://pub.dev" + source: hosted + version: "2.4.5" + google_sign_in_web: + dependency: transitive + description: + name: google_sign_in_web + sha256: "042805a21127a85b0dc46bba98a37926f17d2439720e8a459d27045d8ef68055" + url: "https://pub.dev" + source: hosted + version: "0.12.4+2" graphs: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index da1ea78..3ed8bca 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -50,6 +50,7 @@ dependencies: mongo_dart: ^0.10.3 url_launcher: ^6.3.1 table_calendar: ^3.1.2 + google_sign_in: ^6.2.1 dev_dependencies: