Created
January 12, 2021 16:58
-
-
Save theindianappguy/31db79582f0fd46d5b67ded038c56bb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:google_sign_in/google_sign_in.dart'; | |
import 'package:realtime_chat/helperfun/sharedpref_helper.dart'; | |
import 'package:realtime_chat/services/database.dart'; | |
import 'package:realtime_chat/views/home.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
class AuthMethods { | |
final FirebaseAuth auth = FirebaseAuth.instance; | |
//get current user | |
getCurrentUser() async { | |
return auth.currentUser; | |
} | |
Future<User> signInWithGoogle(BuildContext context) async { | |
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; | |
final GoogleSignIn _googleSignIn = new GoogleSignIn(); | |
final GoogleSignInAccount googleSignInAccount = | |
await _googleSignIn.signIn(); | |
final GoogleSignInAuthentication googleSignInAuthentication = | |
await googleSignInAccount.authentication; | |
final AuthCredential credential = GoogleAuthProvider.credential( | |
idToken: googleSignInAuthentication.idToken, | |
accessToken: googleSignInAuthentication.accessToken); | |
UserCredential result = | |
await _firebaseAuth.signInWithCredential(credential); | |
User userDetails = result.user; | |
if (result == null) { | |
} else { | |
SharedPreferenceHelper().saveUserEmail(userDetails.email); | |
SharedPreferenceHelper().saveUserId(userDetails.uid); | |
SharedPreferenceHelper() | |
.saveUserName(userDetails.email.replaceAll("@gmail.com", "")); | |
SharedPreferenceHelper().saveDisplayName(userDetails.displayName); | |
SharedPreferenceHelper().saveUserProfileUrl(userDetails.photoURL); | |
DatabaseMethods() | |
.addUserInfoToDB( | |
userID: userDetails.uid, | |
email: userDetails.email, | |
username: userDetails.email.replaceAll("@gmail.com", ""), | |
name: userDetails.displayName, | |
profileUrl: userDetails.photoURL) | |
.then(() { | |
Navigator.pushReplacement( | |
context, MaterialPageRoute(builder: (context) => Home())); | |
}); | |
} | |
} | |
Future signOut() async { | |
SharedPreferences prefs = await SharedPreferences.getInstance(); | |
prefs.clear(); | |
await auth.signOut(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment