Advertisement
Hitesh_jadhav

firebase_user.id

Feb 28th, 2022
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import 'package:fluttertoast/fluttertoast.dart';
  4. import 'package:firebase_auth/firebase_auth.dart';
  5. import 'package:google_sign_in/google_sign_in.dart';
  6. import 'package:cloud_firestore/cloud_firestore.dart';
  7. import 'package:shop_app_mobb/pages/home.dart';
  8.  
  9. class login extends StatefulWidget {
  10. const login({Key? key}) : super(key: key);
  11.  
  12. @override
  13. _loginState createState() => _loginState();
  14. }
  15.  
  16. class _loginState extends State<login> {
  17. final googlesignin = GoogleSignIn();
  18. final firebaseauth = FirebaseAuth.instance;
  19. late SharedPreferences preferences;
  20. bool loading = false;
  21. bool islogedin = false;
  22.  
  23. @override
  24. void initState() {
  25. super.initState();
  26. issignedin();
  27. }
  28.  
  29. void issignedin() async {
  30. setState(() {
  31. loading = true;
  32. });
  33. preferences = await SharedPreferences.getInstance();
  34. islogedin = await googlesignin.isSignedIn();
  35. if (islogedin) {
  36. Navigator.pushReplacement(
  37. context, MaterialPageRoute(builder: (context) => Homepage()));
  38. }
  39. setState(() {
  40. loading = false;
  41. });
  42. }
  43.  
  44. Future handlesignin() async {
  45. preferences = await SharedPreferences.getInstance();
  46. setState(() async {
  47. loading = true;
  48. GoogleSignInAccount? googleuser = await googlesignin.signIn();
  49. GoogleSignInAuthentication googleSignInAuthentication =
  50. await googleuser!.authentication;
  51. final AuthCredential credential = GoogleAuthProvider.credential(
  52. accessToken: googleSignInAuthentication.accessToken,
  53. idToken: googleSignInAuthentication.idToken,
  54. );
  55. final User firebaseuser =
  56. (await firebaseauth.signInWithCredential(credential)) as User;
  57. if (firebaseuser != null) {
  58. final QuerySnapshot result = (await FirebaseFirestore.instance
  59. .collection("users")
  60. .where("id", isEqualTo: firebaseuser.uid))
  61. as QuerySnapshot<Object?>;
  62. final List<DocumentSnapshot> documents =
  63. result as List<DocumentSnapshot<Object?>>;
  64. // if (documents.length == 0) {
  65. // FirebaseFirestore.instance.collection("users").doc(User.uid).set({
  66. // "id": User.uid,
  67. // "username": User.displayName,
  68. // "profilepicture": User.photoURL
  69. // });
  70. FirebaseFirestore.instance.collection("users").add({
  71. "id": User.uid,
  72. "username": User.displayName,
  73. "profilepicture": User.photoURL
  74. });
  75. }
  76. }
  77. }
  78.  
  79. );
  80. }
  81.  
  82. @override
  83. Widget build(BuildContext context) {
  84. return Container();
  85. }}
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement