Skip to content

Commit

Permalink
Merge pull request #146 from Manzee1609/GoogleAuth
Browse files Browse the repository at this point in the history
Added google signin(#27)
  • Loading branch information
himanshusharma89 authored Mar 20, 2021
2 parents d2ea9a4 + 03eaf97 commit 31628b2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
32 changes: 32 additions & 0 deletions lib/services/google_auth.dart
Original file line number Diff line number Diff line change
@@ -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<String> 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<void> signOutGoogle() async {
await googleSignIn.signOut();
}
14 changes: 12 additions & 2 deletions lib/views/drawer.dart
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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()),);
},
);
},
),
],
),
Expand Down
14 changes: 14 additions & 0 deletions lib/views/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -195,6 +196,19 @@ class LoginScreenState extends State<LoginScreen>{
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(
Expand Down
8 changes: 5 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 31628b2

Please sign in to comment.