Firebase authentication with localId / idToken (instead of email / password)
up vote
0
down vote
favorite
In my app, when a user logs in, I use firebase.auth to authenticate the user based on the email/password the user typed:
firebase.auth().signInWithEmailAndPassword(userInputs.email.value, userInputs.password.value)
Then I dispatch it to the redux state and also set the received localId and idToken on the local storage:
localStorage.setItem('token', response.user.qa);
localStorage.setItem('localId', response.user.uid);
When the user closes the app window and then reopen the app later, the localId and idToken are still set in the localstorage, but to re-authenticate I need to supply email and password. Is it possible to authenticate with localId/idToken, or should I save the email/password on localStorage instead of saving the two tokens?
Upon checking if the user is authenticated:
var user = firebase.auth().currentUser;
console.log(user);
I get 'null'.
It seems that I must sign in even if I already have the localId, without signing in I don't have access to the database.
Also, obviously my database rules grant access to the db only to authenticated users:
"rules":
".read": "auth != null",
".write": "auth != null"
Thanks in advance!
reactjs firebase redux firebase-authentication
add a comment |
up vote
0
down vote
favorite
In my app, when a user logs in, I use firebase.auth to authenticate the user based on the email/password the user typed:
firebase.auth().signInWithEmailAndPassword(userInputs.email.value, userInputs.password.value)
Then I dispatch it to the redux state and also set the received localId and idToken on the local storage:
localStorage.setItem('token', response.user.qa);
localStorage.setItem('localId', response.user.uid);
When the user closes the app window and then reopen the app later, the localId and idToken are still set in the localstorage, but to re-authenticate I need to supply email and password. Is it possible to authenticate with localId/idToken, or should I save the email/password on localStorage instead of saving the two tokens?
Upon checking if the user is authenticated:
var user = firebase.auth().currentUser;
console.log(user);
I get 'null'.
It seems that I must sign in even if I already have the localId, without signing in I don't have access to the database.
Also, obviously my database rules grant access to the db only to authenticated users:
"rules":
".read": "auth != null",
".write": "auth != null"
Thanks in advance!
reactjs firebase redux firebase-authentication
You shouldn't have to reauthenticate the user upon reload, the tokens are automatically persisted, loaded upon reload, and then refreshed. All you should have to do is listen foronAuthStateChanged
. See firebase.google.com/docs/auth/web/…
– Frank van Puffelen
Nov 10 at 23:59
But the user isn't logged in after reentering the application. I'm talking about totally closing the window and then restarting the applciation. I checked with: var user = firebase.auth().currentUser; console.log(user); And received 'null'. Doesn't it mean that I have to sign in again? I do have the tokens stored in localStorage, but firebase cannot be accessed because there isn't an authenticated user when reentering Thanks again in advance
– sir-haver
Nov 11 at 1:49
Please try withonAuthStateChanged
. If the token needs to be refreshed, that's an async opertion andfirebase.auth().currentUser
may not catch it. On anonAuthStateChanged
listener still doesn't work for you, update your question to show what you've tried with that. In that case it'd also help to know what platform (browser, phone, etc) you run on.
– Frank van Puffelen
Nov 11 at 15:04
Thanks mate! I got it
– sir-haver
Nov 11 at 16:50
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In my app, when a user logs in, I use firebase.auth to authenticate the user based on the email/password the user typed:
firebase.auth().signInWithEmailAndPassword(userInputs.email.value, userInputs.password.value)
Then I dispatch it to the redux state and also set the received localId and idToken on the local storage:
localStorage.setItem('token', response.user.qa);
localStorage.setItem('localId', response.user.uid);
When the user closes the app window and then reopen the app later, the localId and idToken are still set in the localstorage, but to re-authenticate I need to supply email and password. Is it possible to authenticate with localId/idToken, or should I save the email/password on localStorage instead of saving the two tokens?
Upon checking if the user is authenticated:
var user = firebase.auth().currentUser;
console.log(user);
I get 'null'.
It seems that I must sign in even if I already have the localId, without signing in I don't have access to the database.
Also, obviously my database rules grant access to the db only to authenticated users:
"rules":
".read": "auth != null",
".write": "auth != null"
Thanks in advance!
reactjs firebase redux firebase-authentication
In my app, when a user logs in, I use firebase.auth to authenticate the user based on the email/password the user typed:
firebase.auth().signInWithEmailAndPassword(userInputs.email.value, userInputs.password.value)
Then I dispatch it to the redux state and also set the received localId and idToken on the local storage:
localStorage.setItem('token', response.user.qa);
localStorage.setItem('localId', response.user.uid);
When the user closes the app window and then reopen the app later, the localId and idToken are still set in the localstorage, but to re-authenticate I need to supply email and password. Is it possible to authenticate with localId/idToken, or should I save the email/password on localStorage instead of saving the two tokens?
Upon checking if the user is authenticated:
var user = firebase.auth().currentUser;
console.log(user);
I get 'null'.
It seems that I must sign in even if I already have the localId, without signing in I don't have access to the database.
Also, obviously my database rules grant access to the db only to authenticated users:
"rules":
".read": "auth != null",
".write": "auth != null"
Thanks in advance!
reactjs firebase redux firebase-authentication
reactjs firebase redux firebase-authentication
edited Nov 11 at 1:57
asked Nov 10 at 23:47
sir-haver
507
507
You shouldn't have to reauthenticate the user upon reload, the tokens are automatically persisted, loaded upon reload, and then refreshed. All you should have to do is listen foronAuthStateChanged
. See firebase.google.com/docs/auth/web/…
– Frank van Puffelen
Nov 10 at 23:59
But the user isn't logged in after reentering the application. I'm talking about totally closing the window and then restarting the applciation. I checked with: var user = firebase.auth().currentUser; console.log(user); And received 'null'. Doesn't it mean that I have to sign in again? I do have the tokens stored in localStorage, but firebase cannot be accessed because there isn't an authenticated user when reentering Thanks again in advance
– sir-haver
Nov 11 at 1:49
Please try withonAuthStateChanged
. If the token needs to be refreshed, that's an async opertion andfirebase.auth().currentUser
may not catch it. On anonAuthStateChanged
listener still doesn't work for you, update your question to show what you've tried with that. In that case it'd also help to know what platform (browser, phone, etc) you run on.
– Frank van Puffelen
Nov 11 at 15:04
Thanks mate! I got it
– sir-haver
Nov 11 at 16:50
add a comment |
You shouldn't have to reauthenticate the user upon reload, the tokens are automatically persisted, loaded upon reload, and then refreshed. All you should have to do is listen foronAuthStateChanged
. See firebase.google.com/docs/auth/web/…
– Frank van Puffelen
Nov 10 at 23:59
But the user isn't logged in after reentering the application. I'm talking about totally closing the window and then restarting the applciation. I checked with: var user = firebase.auth().currentUser; console.log(user); And received 'null'. Doesn't it mean that I have to sign in again? I do have the tokens stored in localStorage, but firebase cannot be accessed because there isn't an authenticated user when reentering Thanks again in advance
– sir-haver
Nov 11 at 1:49
Please try withonAuthStateChanged
. If the token needs to be refreshed, that's an async opertion andfirebase.auth().currentUser
may not catch it. On anonAuthStateChanged
listener still doesn't work for you, update your question to show what you've tried with that. In that case it'd also help to know what platform (browser, phone, etc) you run on.
– Frank van Puffelen
Nov 11 at 15:04
Thanks mate! I got it
– sir-haver
Nov 11 at 16:50
You shouldn't have to reauthenticate the user upon reload, the tokens are automatically persisted, loaded upon reload, and then refreshed. All you should have to do is listen for
onAuthStateChanged
. See firebase.google.com/docs/auth/web/…– Frank van Puffelen
Nov 10 at 23:59
You shouldn't have to reauthenticate the user upon reload, the tokens are automatically persisted, loaded upon reload, and then refreshed. All you should have to do is listen for
onAuthStateChanged
. See firebase.google.com/docs/auth/web/…– Frank van Puffelen
Nov 10 at 23:59
But the user isn't logged in after reentering the application. I'm talking about totally closing the window and then restarting the applciation. I checked with: var user = firebase.auth().currentUser; console.log(user); And received 'null'. Doesn't it mean that I have to sign in again? I do have the tokens stored in localStorage, but firebase cannot be accessed because there isn't an authenticated user when reentering Thanks again in advance
– sir-haver
Nov 11 at 1:49
But the user isn't logged in after reentering the application. I'm talking about totally closing the window and then restarting the applciation. I checked with: var user = firebase.auth().currentUser; console.log(user); And received 'null'. Doesn't it mean that I have to sign in again? I do have the tokens stored in localStorage, but firebase cannot be accessed because there isn't an authenticated user when reentering Thanks again in advance
– sir-haver
Nov 11 at 1:49
Please try with
onAuthStateChanged
. If the token needs to be refreshed, that's an async opertion and firebase.auth().currentUser
may not catch it. On an onAuthStateChanged
listener still doesn't work for you, update your question to show what you've tried with that. In that case it'd also help to know what platform (browser, phone, etc) you run on.– Frank van Puffelen
Nov 11 at 15:04
Please try with
onAuthStateChanged
. If the token needs to be refreshed, that's an async opertion and firebase.auth().currentUser
may not catch it. On an onAuthStateChanged
listener still doesn't work for you, update your question to show what you've tried with that. In that case it'd also help to know what platform (browser, phone, etc) you run on.– Frank van Puffelen
Nov 11 at 15:04
Thanks mate! I got it
– sir-haver
Nov 11 at 16:50
Thanks mate! I got it
– sir-haver
Nov 11 at 16:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You don't have to store the email & password in the localstorage.
Firebase library automatically retain the auth infomation locally.
The cause of you get null
is you are trying to get user info with Synchronous code.
Which means that you are trying to get user before the firebase library's initalization.
firebase.auth().currentUser; // <= synchronous
Following code runs Asynchronously and you can get user after the firebase library initalize.
firebase.auth().onAuthStateChanged(function(user)
if (user)
// User is signed in.
else
// No user is signed in.
);
Thanks a lot I got it working
– sir-haver
Nov 11 at 16:51
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You don't have to store the email & password in the localstorage.
Firebase library automatically retain the auth infomation locally.
The cause of you get null
is you are trying to get user info with Synchronous code.
Which means that you are trying to get user before the firebase library's initalization.
firebase.auth().currentUser; // <= synchronous
Following code runs Asynchronously and you can get user after the firebase library initalize.
firebase.auth().onAuthStateChanged(function(user)
if (user)
// User is signed in.
else
// No user is signed in.
);
Thanks a lot I got it working
– sir-haver
Nov 11 at 16:51
add a comment |
up vote
1
down vote
You don't have to store the email & password in the localstorage.
Firebase library automatically retain the auth infomation locally.
The cause of you get null
is you are trying to get user info with Synchronous code.
Which means that you are trying to get user before the firebase library's initalization.
firebase.auth().currentUser; // <= synchronous
Following code runs Asynchronously and you can get user after the firebase library initalize.
firebase.auth().onAuthStateChanged(function(user)
if (user)
// User is signed in.
else
// No user is signed in.
);
Thanks a lot I got it working
– sir-haver
Nov 11 at 16:51
add a comment |
up vote
1
down vote
up vote
1
down vote
You don't have to store the email & password in the localstorage.
Firebase library automatically retain the auth infomation locally.
The cause of you get null
is you are trying to get user info with Synchronous code.
Which means that you are trying to get user before the firebase library's initalization.
firebase.auth().currentUser; // <= synchronous
Following code runs Asynchronously and you can get user after the firebase library initalize.
firebase.auth().onAuthStateChanged(function(user)
if (user)
// User is signed in.
else
// No user is signed in.
);
You don't have to store the email & password in the localstorage.
Firebase library automatically retain the auth infomation locally.
The cause of you get null
is you are trying to get user info with Synchronous code.
Which means that you are trying to get user before the firebase library's initalization.
firebase.auth().currentUser; // <= synchronous
Following code runs Asynchronously and you can get user after the firebase library initalize.
firebase.auth().onAuthStateChanged(function(user)
if (user)
// User is signed in.
else
// No user is signed in.
);
edited Nov 11 at 3:53
answered Nov 11 at 2:50
Shota Tamura
458
458
Thanks a lot I got it working
– sir-haver
Nov 11 at 16:51
add a comment |
Thanks a lot I got it working
– sir-haver
Nov 11 at 16:51
Thanks a lot I got it working
– sir-haver
Nov 11 at 16:51
Thanks a lot I got it working
– sir-haver
Nov 11 at 16:51
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244547%2ffirebase-authentication-with-localid-idtoken-instead-of-email-password%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You shouldn't have to reauthenticate the user upon reload, the tokens are automatically persisted, loaded upon reload, and then refreshed. All you should have to do is listen for
onAuthStateChanged
. See firebase.google.com/docs/auth/web/…– Frank van Puffelen
Nov 10 at 23:59
But the user isn't logged in after reentering the application. I'm talking about totally closing the window and then restarting the applciation. I checked with: var user = firebase.auth().currentUser; console.log(user); And received 'null'. Doesn't it mean that I have to sign in again? I do have the tokens stored in localStorage, but firebase cannot be accessed because there isn't an authenticated user when reentering Thanks again in advance
– sir-haver
Nov 11 at 1:49
Please try with
onAuthStateChanged
. If the token needs to be refreshed, that's an async opertion andfirebase.auth().currentUser
may not catch it. On anonAuthStateChanged
listener still doesn't work for you, update your question to show what you've tried with that. In that case it'd also help to know what platform (browser, phone, etc) you run on.– Frank van Puffelen
Nov 11 at 15:04
Thanks mate! I got it
– sir-haver
Nov 11 at 16:50