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

Added google signin(#27) #146

Merged
merged 1 commit into from
Mar 20, 2021
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
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