O
O
OCTAGRAM2020-10-28 05:50:12
firebase
OCTAGRAM, 2020-10-28 05:50:12

How to use persistent Firebase authorization?

I read about Firebase Auth that it’s not possible to call signInWithEmailAndPassword every time, but you can save the previous session and not create new ones every time. Moreover, this behavior (LOCAL) seems to be the default. But now I'm meditating on the documentation and just can't figure out how to do something.

Where does execution begin?

firebase.initializeApp (Config);

This is a synchronous function, it does not go to the network.

And the next call to Firebase has already started authorization:

await firebase.auth ().signInWithEmailAndPassword
  (User_Mail, User_Password);


By this point, it's too late. If I want stable authorization, I need to fit something into this gap.

The type
firebase.auth ().currentUser
thing is a synchronously retrieved property, right after initializeApp() it can't know if the user is logged in. I thought, I need to somehow kick it, something asynchronous. It made sense
await firebase.auth ().currentUser.reload ();
. It didn't work. Found out that currentUser is null. How to fix it? Maybe so?
await firebase.auth ().updateCurrentUser (null);
FirebaseError
A null user object was provided as the argument for an operation which requires a non-null user object.

Why such a requirement? The documentation is written in English on white
updateCurrentUser ( user : User | null ) : Promise < void >

I don't have firebase.auth.User, where can I get it from. And I have null, and it is written that this is possible. And it turns out you can't.

What else would be so asynchronous to call? Oh found it.
setPersistence ( persistence : Persistence ) : Promise <void>


The default value for Persistence is LOCAL, which is what I need. But suddenly, if I kick with the same value, Firebase will have an epiphany, and he will finally remember who he logged in in the closed tab. So let's go kick
await firebase.auth ().setPersistence
  (firebase.auth.Auth.Persistence.LOCAL);


As you might guess, currentUser is null. Hmm, maybe it only works the second time? No, null again.

Here is more documentation , specifically about Firebase Persistence. Allegedly about.

And what do we see in the example? A call is made to setPersistence and then signInWithEmailAndPassword.

Hello? Does anyone here understand that signInWithEmailAndPassword is exactly what I DO NOT WANT to do. I DON'T WANT to require a password every time. I DO NOT WANT to save the password in localStorage, so that there is something to pass as the signInWithEmailAndPassword argument. Show me an example of NOT CALLing signInWithEmailAndPassword.

Some kind of mockery.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question