Cannot sign-out from Facebook using flutter with firebase - facebook

I have a flutter app where I am authenticating with Facebook and firebase on iOS. However, I cannot get the login in page again although I am calling the logout function
I always get the facebook confirmation screen which has some text that inform me that I have already logged in.
How do I logout so that the next time I am ask to enter my email/pass
here is a snippet of my code
Future<FirebaseUser> signInWithFacebook();
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<FirebaseUser> signInWithFacebook() async {
FirebaseUser user;
var result = await _facebookLogin
.logInWithReadPermissions(['email', 'public_profile']);
if (result.status == FacebookLoginStatus.loggedIn) {
FacebookAccessToken myToken = result.accessToken;
AuthCredential credential =
FacebookAuthProvider.getCredential(accessToken: myToken.token);
user = await _auth.signInWithCredential(credential);
}
return user;
}
Future<void> signOut() async {
await _facebookLogin.logOut();
await _auth.signOut();
}
Thanks for your help

Firebase saves the FirebaseUser object in cache so the user won't need to re-signin every time he leaves the app. So the signOut function should look like this:
Future<void> signOut() async {
await _facebookLogin.logOut();
await _auth.signOut();
_user = null;
}
And the FirebaseUser user; should be moved out of the signInWithFacebook function (I renamed it to _user).

Related

how to keep user logged in

this is how I login using Google and firebase. but I couldn't figure it out as to how to keep the use logged in.. when the app restarts it log the user out automatically
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
Future<String> signInWithGoogle() async {
final GoogleSignInAccount? googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount!.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final authResult = await _auth.signInWithCredential(credential);
final User? user = authResult.user;
assert(!user!.isAnonymous);
final User? currentUser = _auth.currentUser;
assert(user!.uid == currentUser!.uid);
return 'signInWithGoogle succeeded: $user';
}
You can call await _auth.currentUser() at the start of your app to check the current user. Further you may want to store the token in shared preferences.
Firebase automatically persists the user credentials in the shared storage, and restores them when the app restarts. There's nothing you need to do for that.
What you will need to do though is listen for the authentication state as shown in the first code snippet in the documentation on getting the current user:
FirebaseAuth.instance
.authStateChanges()
.listen((User? user) {
if (user != null) {
print(user.uid);
}
});
This code needs to run when the app starts, so I typically have it in my top-level widget and then store the user in the state so that my build method can use it. By listening to auth state changes, the code is run automatically when the user sign-in state is restored at startup (which happens asynchronously, so may take a few moments) but also when the user would later be logged out (for example, if you disable the account in the Firebase Authentication console).
It may not be the best way to do it but this worked.
chooseLogin() {
if (_auth.currentUser == null) {
return const SignUo();
} else {
return const Splash();
}
}

How to logout facebook account for entring another account

I am using flutter facebook auth.
When I login for the first time came to enter email and password after that I logout and went to login again then it did not ask for email and password.
When I login for the first time came to enter email and password after that I logout and went to login again then it did not ask for email and password.
and its looks like it
this is my facebook login functionality
import 'package:cwc/ApiManager/preference.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
// final GoogleSignIn googleSignIn = GoogleSignIn();
String? fbName;
String? fbEmail;
String? fbImageUrl;
Future<String?> signInWithFacebook() async {
// Trigger the sign-in flow
final LoginResult loginResult = await FacebookAuth.instance.login();
// final GoogleSignInAccount? googleSignInAccount = await googleSignIn.signIn();
var a= await loginResult.message;
// Create a credential from the access token
final OAuthCredential facebookAuthCredential = FacebookAuthProvider.credential(loginResult.accessToken!.token);
print("facebookAuthCredential22 ${facebookAuthCredential.token}");
print("facebookAuthCredential33 ${facebookAuthCredential.accessToken}");
print("facebookAuthCredential44 ${facebookAuthCredential.rawNonce}");
// Once signed in, return the UserCredential
final UserCredential authResult =
await _auth.signInWithCredential(facebookAuthCredential);
final User? users = authResult.user;
final UserInfo? user = users!.providerData[0];
print("facebookAuthCredential11 $user");
// return FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);
if (user != null) {
// Checking if email and name is null
// assert(user.email != null);
assert(user.displayName != null);
assert(user.photoURL != null);
Preferences.saveData("socialName", user.displayName.toString());
Preferences.saveData("socialEmail", user.email.toString());
Preferences.saveData("socialImage", user.photoURL.toString());
// Only taking the first part of the name, i.e., First Name
// if (googleName!.contains(" ")) {
// googleName = googleName!.substring(0, googleName!.indexOf(" "));
// }
assert(!users.isAnonymous);
assert(await users.getIdToken() != null);
final User? currentUser = _auth.currentUser;
assert(users.uid == currentUser!.uid);
print('signInWithFacebook succeeded: $user');
return '$user';
}
return null;
}
Future<void> signOutWithFacebook() async {
await FacebookAuth.instance.logOut();
print("User Signed Out");
}
You'll also need to sign out of Facebook explicitly by calling:
FacebookAuth.instance.logOut()
Also see:
How to properly sign out of Facebook on Android with Firebase?
The documentation of flutter_facebook_auth on signing out
You can call the setCustomParameters() method on FacebookAuthProvider.
And pass the consent parameter like so:
_facebookAuth.setCustomParameters({"consent":"select_account"});
You can read about the parameters you can pass to the consent on this answer

Firebase documentation for flutter does not work for deleting user

The following documentation on deleting a user does not work:
try {
await FirebaseAuth.instance.currentUser.delete();
} catch on FirebaseAuthException (e) {
if (e.code == 'requires-recent-login') {
print('The user must reauthenticate before this operation can be executed.');
}
}
"delete()" is not a function recognized by Flutter. "FirebaseAuthException" is also not recognized by Flutter.
How do I delete a user? Where do I find this information?
Using flutter, if you want to delete firebase accounts together with the associated firestore user collection document, the following method works fine. (documents in user collection named by the firebase uid).
Database Class
class DatabaseService {
final String uid;
DatabaseService({this.uid});
final CollectionReference userCollection =
Firestore.instance.collection('users');
Future deleteuser() {
return userCollection.document(uid).delete();
}
}
Use Firebase version 0.15.0 or above otherwise, Firebase reauthenticateWithCredential() method throw an error like { noSuchMethod: was called on null }.
Authentication Class
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
Future deleteUser(String email, String password) async {
try {
FirebaseUser user = await _auth.currentUser();
AuthCredential credentials =
EmailAuthProvider.getCredential(email: email, password: password);
print(user);
AuthResult result = await user.reauthenticateWithCredential(credentials);
await DatabaseService(uid: result.user.uid)
.deleteuser(); // called from database class
await result.user.delete();
return true;
} catch (e) {
print(e.toString());
return null;
}
}
}
Then use the following code inside the clickable event of a flutter widget tree to achieve the goal:
onTap: () async {
await AuthService().deleteUser(email, password);
}

Everytime i restart the app, it displays the login page, even when there is user data (flutter)

As the title suggests, im only using signInWithGoogle() for my user auth, each time i restart the app it went to Login Page even when the user did not log out.
I know this question has been asked plenty of times, im seeing many answers pointing towards using FirebaseAuth streambuilders but i cant seem to get it to work ... Sorry im a beginner and i do not understand much of the documentations too :(
Is there anyway i can direct the users that are logged in to the main page, and those without user data will have to visit LoginPage first?
sign_in.dart :
import 'package:confApp/main.dart';
import 'package:firebase_auth/firebase_auth.dart' as fauth;
import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter/material.dart';
String name;
String email;
String imageUrlGoogle;
String id;
final fauth.FirebaseAuth _auth = fauth.FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
Future<bool> signInWithGoogle() async {
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final fauth.AuthCredential credential = fauth.GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final fauth.UserCredential authResult = await _auth.signInWithCredential(credential);
final fauth.User user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
assert(user.email != null);
assert(user.displayName != null);
assert(user.photoURL != null);
assert(user.uid != null);
name = user.displayName;
email = user.email;
imageUrlGoogle = user.photoURL;
id = user.uid;
final fauth.User currentUser = _auth.currentUser;
assert(user.uid == currentUser.uid);
if ( fauth.FirebaseAuth.instance.currentUser != null) {
return true;
} else
return false;
// return 'signInWithGoogle succeeded: $user';
}
void signOutGoogle() async {
await googleSignIn.signOut();
print("User Sign Out");
}
Let me know what other codes that you need, ill update the question with the codes. Thanks in advance!
You need to use SharedPreference Package to save user data. check this package you will know how to handle this. Basically you need to save user data by sharedPref, and retrieve while entering in the app and check if the user data is there you can route to user page, if no user data route to login page.
Here is simple counter number save example:
_incrementCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Retriving data
int counter = (prefs.getInt('counter') ?? 0) + 1;
print('Pressed $counter times.');
//Saving data
await prefs.setInt('counter', counter);
}
there are prefs.setString/ pref.manymore to save, hope it solves your problem.
LoginPage Code
main.dart
This is how i am setting Data in Shared Preferences during UserLogin
UserID = firebaseuser.uid;
UserName = firebaseuser.displayName;
UserPhoto = firebaseuser.photoURL;
UserEmail = firebaseuser.email;
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('UserID', UserID);
prefs.setString('UserEmail', UserEmail);
prefs.setString('UserName', UserName);
prefs.setString('UserPhoto', UserPhoto);`
And here i am checking whether UserId named shared Preferences data exists or not , if exists then user is logged In if not then User is not logged In to the App
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
SharedPreferences prefs;
Future<void> main() async {
prefs = await SharedPreferences.getInstance();
String UserID = prefs.getString('UserID');
String UserName = prefs.getString('UserName');
String UserEmail = prefs.getString('UserEmail');
String UserPhoto = prefs.getString('UserPhoto');
runApp(
MaterialApp(
home: UserID == null
? App()
:
MyApp(
UserID: UserID,
UserName: UserName,
UserEmail: UserEmail,
UserPhoto: UserPhoto,
)));
}`
Like this You can check whether the user is signed in or not. If signed In u can return
a different screen and if not then a different one

i'm not able to get FirebaseUser Method...! alredy add packege Firebse Auth [duplicate]

I'm new to Flutter. I have an Issue with Firebase Auth/ Google Auth
The FirebaseUser is not defined
Code:
FirebaseAuth _auth = FirebaseAuth.instance;
GoogleSignIn googleSignIn = GoogleSignIn();
Future<FirebaseUser> currentUser() async { // The Issue is here in the Future<>
final GoogleSignInAccount account = await googleSignIn.signIn();
final GoogleSignInAuthentication authentication =
await account.authentication;
final GoogleAuthCredential credential = GoogleAuthProvider.getCredential(
idToken: authentication.idToken, accessToken: authentication.accessToken);
final AuthResult authResult = await _auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user; // and here as I can't define this FirebaseUser object to return
return user;
}
Pubspec.yml
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.3
firebase_auth: ^0.18.0
location: ^3.0.2
page_transition: ^1.1.6
google_sign_in: ^4.5.1
flutter_facebook_login: ^3.0.0
firebase_database: ^4.0.0
I also face the same issue with AuthResult
final AuthResult authResult = await _auth.signInWithCredential(credential);
Starting from Version firebase_auth 0.18.0:
In the newest version of firebase_auth, the class FirebaseUser was changed to User, and the class AuthResult was changed to UserCredential. Therefore change your code to the following:
Future<User> currentUser() async {
final GoogleSignInAccount account = await googleSignIn.signIn();
final GoogleSignInAuthentication authentication =
await account.authentication;
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
idToken: authentication.idToken,
accessToken: authentication.accessToken);
final UserCredential authResult =
await _auth.signInWithCredential(credential);
final User user = authResult.user;
return user;
}
FirebaseUser changed to User
AuthResult changed to UserCredential
GoogleAuthProvider.getCredential() changed to GoogleAuthProvider.credential()
onAuthStateChanged which notifies about changes to the user's sign-in state was replaced with authStateChanges()
currentUser() which is a method to retrieve the currently logged in user, was replaced with the property currentUser and it no longer returns a Future<FirebaseUser>.
Example of the above two methods:
FirebaseAuth.instance.authStateChanges().listen((event) {
print(event.email);
});
And:
var user = FirebaseAuth.instance.currentUser;
print(user.uid);
Deprecation of UserUpdateInfo class for firebaseUser.updateProfile method.
Example:
Future updateName(String name, FirebaseUser user) async {
var userUpdateInfo = new UserUpdateInfo();
userUpdateInfo.displayName = name;
await user.updateProfile(userUpdateInfo);
await user.reload();
}
now
import 'package:firebase_auth/firebase_auth.dart' as firebaseAuth;
Future updateName(String name, auth.User firebaseUser) async {
firebaseUser.updateProfile(displayName: name);
await firebaseUser.reload();
}
Since firebase_auth 0.18.0, the class FirebaseUser was changed to User
In the newest version of firebase_auth, the class FirebaseUser was changed to User, and the class AuthResult was changed to UserCredentail. Therefore change FirebaseUser to User
The class FirebaseUser was changed to User
try this way
_registerUser() async {
try {
final User? user =
(await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailCtrl.text,
password: passCtrl.text,
))
.user;
FirebaseFirestore.instance.collection('users').doc().set({
'name': nameCtrl.text,
'uid': user!.uid,
'email': user.email,
'isEmailVerified': user.emailVerified, // will also be false
'photoUrl': user.photoURL, // will always be null
});
print("Created");
} catch (e) {
print(e.toString());
}
}
Run
flutter pub get
Then rebuild your app.
This can be your signin function with email and password as of Sept 2020.Initialze app is a new introduced method we must at least call once before we use any other firebase methods.
Future<void> signin() async {
final formState = _formkey.currentState;
await Firebase.initializeApp();
if (formState.validate()) {
setState(() {
loading = true;
});
formState.save();
try {
print(email);
final User user = (await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password))
.user;
} catch (e) {
print(e.message);
setState(() {
loading = false;
});
}
}
}