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!










share|improve this question
























  • 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 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















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!










share|improve this question
























  • 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 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













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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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 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
















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












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





share|improve this answer























  • Thanks a lot I got it working
    – sir-haver
    Nov 11 at 16:51











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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





share|improve this answer























  • Thanks a lot I got it working
    – sir-haver
    Nov 11 at 16:51















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





share|improve this answer























  • Thanks a lot I got it working
    – sir-haver
    Nov 11 at 16:51













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine