Is it possible to delete currentUser firebase authentication user? - firebase

I used firebase_auth package to work with flutter. And I used phone authentication to sign in. Everything is working well. But when signout user is not deleted on firebase.
Is it possible to delete firebase user?
RaisedButton(
onPressed: () async {
await FirebaseAuth.instance.signOut();
}
)
I tried this way,
but error is coming..
delete called null
_auth.onAuthStateChanged.listen((currentUser)=>{
currentUser.delete()
}).onError((e)=>print("Error is $e"));

Yes. You must use FirebaseAuth.instance.currentUser.delete() method before (on) signOut() method.
RaisedButton(
onPressed: () async {
FirebaseAuth.instance.currentUser.delete();
await FirebaseAuth.instance.signOut();
}
)

You can't delete the current user after they've signed out, because at that point there is no current user anymore.
But you can prevent this whole chicken-and-egg problem by simply deleting the user without signing out. This should be all that is needed for that:
currentUser.delete()
You can call this operation "logging out" for your users of course, but in code you're simply deleting their account.

Related

How to auto signOut a firebase user in flutter?

I'm working on a flutter app with firebase as a backend and an authentication, now I want the app to have a trial version where the user can signInWithGoogle and use the app with full features ( for like one hour ), and then signs out automatically after that one hour ends.
SO FAR, I accomplished the following:1- signIn with google account.
2- add signed in user to _trial collection with timeStamp.
Future enterTrial() async {
final UserCredential user = await Auth.instance.googleSignInMethod();
final User actualUser = user.user;
try {
//_trial is a collection for users in Trial Period
await _trial.doc(actualUser.email).set({.
"name": actualUser.displayName,
"email": actualUser.email,
"creationDate": FieldValue.serverTimestamp(),
});
} catch (e) {
print(e);
}
}
WHAT's LEFT:
after one hour from signIn, signOut and move user from _trial collection to _expired collection.
so that I could check if the user is expired or not.
is there a way to automatically signOut after sometime? or to - periodically - compare creationTime with currentTime and accordingly signOut?
Thanks In Advance.
Yes You can achive this by using background_fetch package this provide you a callback function in that function you can call your logout method.

Limit Firebase authentication to session

I'm attempting to implement a "Keep me signed-in" check box when the user signs-in. If the checkbox is checked then the user should remain signed-in until they explicitly sign-out. If not checked, the user should only remain signed-in until they close the app.
The Flutter Firebase Authentication package doesn't have a method for configuring the authentication duration, or at least I've not found a method.
I've tried to listen for when the app is shutting down using the WidgetsBindingObserver's didChangeAppLifecycleState(..) method's AppLifecycleState.detached state. I can see the signOut() method called, but despite this the user remains signed-in the next time they launch the app. I'm not surprised given that the app is in the middle of exiting.
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
if( state == AppLifecycleState.detached ){
FirebaseAuth.instance.signOut();
}
}
Am I missing any good options?
By default the Firebase Authentication SDKs persist the authentication state to local storage, and then restore it from there when the app restarts.
If you want the state to not be persisted, you can configure that when the app starts by calling:
await FirebaseAuth.instance.setPersistence(Persistence.NONE);
Also see the FlutterFire documentation on persisting authentication state.
That's because signOut() returns a Future. So you'll have to await it. So, suppose you have a function userSignOut.
Future<void> userSignOut() => FirebaseAuth.instance.signOut();
And you use it on a button...
IconButton(
icon: const Icon(Icons.exit_to_app),
onPressed: () async {
await userSignOut();
},
)
This should solve your issue. If it still doesn't work, provide more relevant code.

flutter firebaseauth signout() not working

I am using the firebase auth signout method but it doesnt seem to be working properly. when i click on the logout button, it is supposed to log the current user out. however, after doing so, the console does not indicate that the firebase auth signout method is actually working, none!
the app has a sysmte where if the user is online then they will always be redirected to the home page upon opening the app unless they log out from the app (which they will be redirected to the login page). and the app always redirects me to the home page when opening the app, hence it is clear that the firebase auth signout method is not working even after i hit the logout button
this is my logout method and it is in the Authentication class
FirebaseAuth _auth = FirebaseAuth();
logOut() async {
return await _auth.signOut();
}
this is the callback and the logOutCurrentUser function is called when i click the logout button
Authentication authentication = Authentication();
logOutCurrentUser(BuildContext context) {
try {
authentication.logOut();
Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => LoginSignupToggle(true)), (route) => false);
}
catch (e) {
print(e.toString());
}
}
all in all, it seems like FirebaseAuth.instance.currentUser() is still not null even after i hit the logout button which i expect it to be null after logging out
i have tried searching for an answer as much as i could but nothing seems to work for me
My guess is that your sign-out is aborted because you're not waiting for it to finish. Since your logOut is async you need to use await when calling it:
await authentication.logOut();
Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => LoginSignupToggle(true)), (route) => false);

FireBase Auth SignOut : PlatformException(FirebaseException, User has already been linked to the given provider., null)

I am doing firebase authentication for user.I login with google and phone simultaneously.The user is authenticated successfully and signed in like that.
but when i signOut from the app,and try to sign in again with the same google and phone it tells me -
Failed to verify SMS code: PlatformException(FirebaseException, User
has already been linked to the given provider., null)
I have used the following code to signout from the app.
FlatButton(
child: Text("Sign out", style: theme.textTheme.button),
onPressed: () async {
await GoogleSignIn().signOut();
await FirebaseAuth.instance.signOut();
},
)
Well, this problem is solved by deleting the firebase user from the firebase auth console.
Hopefully,I had data linked to that account in the firestore database which I used to again authenticate that user when he tries to login after some gap and retrieving his previous data with searching the key(phone Number).
final FirebaseUser firebaseUser;
final GoogleSignInAccount googleUser;
onPressed: () async {
await GoogleSignIn().signOut();
googleUser.clearAuthCache();
print('SignedOut');
await firebaseUser.delete();
await FirebaseAuth.instance.signOut();
print('signedOut from FireBase');
}
This issue got solved without any prone damage to data linked with the User.

Login works despite wrong user credentials

I am using Firebase and FirebaseAuth in my Flutter app to manage users and data.
The problem is that the users are always taken to the next screen even if they enter wrong credentials. They can enter data to non-existent user accounts, or wrong passwords, and they are still taken to the text screen which should only happen after a successful auth.
As far as I understood, if code inside (if user!= null) should only be executed if the auth was successful, but it still happens all the time.
The console does log a response from Firebase, stating either a non-existent user or wrong passsword, but the code proceeds nontheless.
Does anyone see what I did wrong?
I'm still a learner, so it's probably a basic mistake :(
Thank you!!
final _auth = FirebaseAuth.instance;
...
onPressed: () async {
try {
final user = _auth.signInWithEmailAndPassword(
email: email, password: password);
if (user != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CoursesListScreen(),
),
);
}
} catch (e) {
print(e.message);
}
},
When you call _auth.signInWithEmailAndPassword you get back a Future, which will never be nil. So indeed the navigator will always go to the next screen. Note that the user isn't signed in at this stage, your code merely detects the wrong condition.
It's more likely that you want to wait until the Future resolves, which you do with await:
final user = await _auth.signInWithEmailAndPassword(...)

Resources