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

feat: Google oAuth added #84

Merged
merged 1 commit into from
Oct 19, 2024
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
Binary file added assets/images/google.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions lib/cubit/auth/auth_cubit.dart
Original file line number Diff line number Diff line change
@@ -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<AuthState> {
Expand Down Expand Up @@ -58,6 +60,60 @@ class AuthCubit extends Cubit<AuthState> {
}
}

Future<void> 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<String, dynamic>);
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<void> registerUser({
required String email,
required String password,
Expand Down
63 changes: 40 additions & 23 deletions lib/views/pages/login/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class _LoginPageState extends State<LoginPage> {
TextEditingController passwordController = TextEditingController();

//variable to control password visibility
bool _isPasswordVisible =false;
bool _isPasswordVisible = false;
// VALIDATION
bool _isValidate = false;
late SharedPreferences prefs;
Expand Down Expand Up @@ -158,16 +158,18 @@ class _LoginPageState extends State<LoginPage> {
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;
});
},
),
),
],
),
Expand All @@ -177,16 +179,16 @@ class _LoginPageState extends State<LoginPage> {
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,
Expand All @@ -200,7 +202,7 @@ class _LoginPageState extends State<LoginPage> {

// LOGIN BUTTON
SizedBox(
height: screenHeight * 0.18,
height: screenHeight * 0.12,
),
GestureDetector(
onTap: loginUser,
Expand All @@ -225,7 +227,22 @@ class _LoginPageState extends State<LoginPage> {
),
),
),


//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<AuthCubit>().signInWithGoogle();
},
),
),
// FINAL TEXT
Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down
48 changes: 48 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down