How to logout facebook account for entring another account - flutter

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

Related

Can we set user displayName in firebase using password authentication through flutter? [duplicate]

I can save the email and the password with Firebase Authentication. I also save this information with Cloud Firestore. But how can I add and save the displayName after registering?
my code:
Future registerWithEmailAndPassword(String email, String password) async {
try {
AuthResult result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
FirebaseUser user = result.user;
// create a new document for the user with the uid
await DatabaseService(uid: user.uid)
.updateUserData('User', user.email, 'test.de/test.png', user.uid);
return _userFromFirebaseUser(user);
} catch (e) {
print(e.toString());
return null;
}
}
the register form button:
onPressed: () async {
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await _auth
.registerWithEmailAndPassword(
email, password);
if (result == null) {
setState(() {
error = 'Valid email, please';
loading = false;
});
}
}
}
You can use updateProfile method given in the FirebaseUser class to update the name and photo url.
updateProfile({String displayName, String photoURL}).
For email you can use the different method
updateEmail(String newEmail) which is a async method.
Or to save the username directly into the firestore, after successful login you can use 'set' method of the FirebaseFirestore to save email, password and the username.
Here's the code if you want to save the Username together with the email and password
final FirebaseAuth _auth = FirebaseAuth.instance;
//register with email & password & save username instantly
Future registerWithEmailAndPassword(String name, String password, String email) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
User user = result.user;
user.updateProfile(displayName: name); //added this line
return _user(user);
} catch(e) {
print(e.toString());
return null;
}
}
late User userFirebase;
Future<UserCredential> userCredential =await //Your code signin or signup
await userCredential.then((UserCredential value) {
userFirebase = value.user!;
});
await userFirebase.updateDisplayName(event.name);
You can use updateDisplayName() and updatePhotoURL() methods on a User object to add the displayName/profile image to Firebase user.
final userCredential = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
//After creating a user in Firebase, we then are able to change name/pictue
await userCredential.user?.updateDisplayName(name);
await userCredential.user?.updatePhotoURL(imageUrl);
As of october 2021, updateProfile() is deprecated, and you should use updateDisplayName() and updatePhotoURL() instead.

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();
}
}

Flutter : Errors appearing after updating the Firebase auth package

I created the Firebase Auth class a year ago. Now After updating the Dart language and updating the firebase auth package some errors appeared.
import 'package:firebase_auth/firebase_auth.dart';
class AuthService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Stream<String> get onAuthStateChanged => _firebaseAuth.onAuthStateChanged.map(
(FirebaseUser user) => user.uid,
);
// Sign up Email & passowrd
Future<String> createUserWithEmailAndPassword(
String email, String password, String name) async {
final currentUser = await _firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
);
//updat username
var userUpdateInfo = UserUpdateInfo();
userUpdateInfo.displayName = name;
await currentUser.updateProfile(userUpdateInfo);
await currentUser.reload();
return currentUser.uid;
}
// sign in Email & password
Future<String> sinInWithEmailAndPassword(
String email, String password) async {
return (await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password))
.uid;
}
// sign out
signOut() {
return _firebaseAuth.signOut();
}
}
click here to see error
All firebase plugins for flutter since a year ago have a lot of breaking changes you should go to firebase.flutter.dev and check it
I've been trying and searching for more than two weeks The error appears in the code:
//updat username
var userUpdateInfo = UserUpdateInfo();
userUpdateInfo.displayName = name;
await currentUser.updateProfile(userUpdateInfo);
await currentUser.reload();
return currentUser.uid;
}
The method 'UserUpdateInfo' isn't defined for the type 'AuthService'

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

Cannot sign-out from Facebook using flutter with firebase

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).