diff --git a/lib/services/google_auth.dart b/lib/services/google_auth.dart new file mode 100644 index 0000000..8f77192 --- /dev/null +++ b/lib/services/google_auth.dart @@ -0,0 +1,32 @@ +import 'package:firebase_core/firebase_core.dart'; +import 'package:google_sign_in/google_sign_in.dart'; +import 'package:firebase_auth/firebase_auth.dart'; + +final FirebaseAuth _auth = FirebaseAuth.instance; +final GoogleSignIn googleSignIn = GoogleSignIn(); + + +Future signInWithGoogle() async { + await Firebase.initializeApp(); + + final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn(); + final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication; + + final AuthCredential credential = GoogleAuthProvider.credential( + accessToken: googleSignInAuthentication.accessToken, + idToken: googleSignInAuthentication.idToken, + ); + + final UserCredential authResult = await _auth.signInWithCredential(credential); + final User user = authResult.user; + assert(!user.isAnonymous); + + final User currentUser = _auth.currentUser; + assert(user.uid == currentUser.uid); + + return '$user'; +} + +Future signOutGoogle() async { + await googleSignIn.signOut(); +} \ No newline at end of file diff --git a/lib/views/drawer.dart b/lib/views/drawer.dart index 5872b64..0860fe5 100644 --- a/lib/views/drawer.dart +++ b/lib/views/drawer.dart @@ -1,8 +1,10 @@ import 'package:flutter/material.dart'; +import 'package:retro_shopping/services/google_auth.dart'; import 'package:retro_shopping/views/orders.dart'; //import 'package:retro_shopping/views/profile.dart'; import '../widgets/drawer_item.dart'; -import 'wishlist.dart'; +import 'Wishlist.dart'; +import 'package:retro_shopping/views/login.dart'; class DrawerWidget extends StatelessWidget { const DrawerWidget({ @@ -49,7 +51,15 @@ class DrawerWidget extends StatelessWidget { DrawerItem( icon: Icons.logout, title: 'LOG OUT', - onTap: () {}, + onTap: () { + signOutGoogle().then( + (res) { + Navigator.pushReplacement( + context, + MaterialPageRoute(builder: (BuildContext context) => LoginScreen()),); + }, + ); + }, ), ], ), diff --git a/lib/views/login.dart b/lib/views/login.dart index f15d148..73179e9 100644 --- a/lib/views/login.dart +++ b/lib/views/login.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:retro_shopping/helpers/constants.dart'; import 'package:retro_shopping/widgets/retro_button.dart'; import 'package:retro_shopping/dashboard.dart'; +import 'package:retro_shopping/services/google_auth.dart'; class LoginScreen extends StatefulWidget{ @override @@ -195,6 +196,19 @@ class LoginScreenState extends State{ InkWell( onTap: (){ debugPrint('Navigate to google!'); + signInWithGoogle().then( + (result) { + if (result != null) { + Navigator.of(context).pushReplacement( + MaterialPageRoute( + builder: (context) { + return Dashboard(); + }, + ), + ); + }; + }, + ); }, // ignore: sized_box_for_whitespace child: Container( diff --git a/pubspec.yaml b/pubspec.yaml index dccb22b..5d2a66d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -29,15 +29,17 @@ dependencies: adobe_xd: ^1.1.0+1 cupertino_icons: ^0.1.3 fluttertoast: ^7.1.8 - firebase_core: ^1.0.0 + firebase_core: google_fonts: ^1.1.0 razorpay_flutter: 1.1.0 google_mobile_ads: ^0.11.0+3 geolocator: 5.3.2+2 geocoder: 0.2.1 flutter_svg: ^0.20.0-nullsafety.3 - provider: ^5.0.0 - advance_pdf_viewer: ^1.2.2 + provider: + advance_pdf_viewer: + firebase_auth: + google_sign_in: dev_dependencies: flutter_test: