最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase RememberMe functionality - Stack Overflow

programmeradmin1浏览0评论

My webapp allows different users to login in different tabs/browsers on the same machine with different credentials (using signInWithEmailAndPassword). I achieve this by calling firebase.initializeApp(config, 'appName'+new Date().getTime()) for each login

When the user closes the tab or reloads (in window.onbeforeunload) I call .auth().signOut() to log him out.

I now want to add a RemeberMe functionality to my app page, that is a tickbox that if (and only if) ticked, will allow following logins from the same machine and username without giving the password even if the machine was for example restarted in the meantime.

How can that be achieved ?

what I am thinking about is (when remember me is on) to generate a token on the client stored in a cookie and maintain a table on the db which links tokens to passwords, there are two problems with this, first of all the password is saved as is on the db which is bad and second the password needs to be sent back to the client which is also bad.

any better options ?

My webapp allows different users to login in different tabs/browsers on the same machine with different credentials (using signInWithEmailAndPassword). I achieve this by calling firebase.initializeApp(config, 'appName'+new Date().getTime()) for each login

When the user closes the tab or reloads (in window.onbeforeunload) I call .auth().signOut() to log him out.

I now want to add a RemeberMe functionality to my app page, that is a tickbox that if (and only if) ticked, will allow following logins from the same machine and username without giving the password even if the machine was for example restarted in the meantime.

How can that be achieved ?

what I am thinking about is (when remember me is on) to generate a token on the client stored in a cookie and maintain a table on the db which links tokens to passwords, there are two problems with this, first of all the password is saved as is on the db which is bad and second the password needs to be sent back to the client which is also bad.

any better options ?

Share Improve this question edited Jan 16, 2017 at 8:05 kofifus asked Jan 16, 2017 at 6:54 kofifuskofifus 19.3k21 gold badges116 silver badges183 bronze badges 7
  • Remembering is the default behaviour. Did you want to disable it? If you listen to onAuthStateChanged you should see automatic re-authentication (i.e. remembering). – cartant Commented Jan 16, 2017 at 7:02
  • forgot to mention I am calling .auth().signOut() on page reload (updated question). Perhaps all I need is for the tick box to not call signout if it is ticked ? I want the user to determine if he wants the session to persist on the machine with the default being no. – kofifus Commented Jan 16, 2017 at 7:09
  • Yep, that would do it and listen to onAuthStateChanged to decide whether the sign in is short circuited. – cartant Commented Jan 16, 2017 at 7:14
  • sorry me question was misleading, I have now expanded it, can you please look again ? the point is rememberme needs to work even if the user for example restarted his machine (and the box was ticked on the last login) – kofifus Commented Jan 16, 2017 at 7:25
  • That sounds trickier. I guess you have to implement it yourself. You'll have to store the credentials, too - so that you can call signInWithEmailAndPassword - and that sounds unsafe. – cartant Commented Jan 16, 2017 at 8:01
 |  Show 2 more comments

2 Answers 2

Reset to default 10

Starting with Firebase JS 4.2.0 you can now specify session persistence. You can login different users in multiple tabs by calling: firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)

And when the window is closed, the session is cleared.

For more on this, check https://firebase.google.com/support/release-notes/js#4.2.0 and https://firebase.google.com/docs/auth/web/auth-state-persistence

Just add a rememberMe checkbox to your login form with a reference variable(for an exmaple remember) and use firebase setPersistence like this,

firebase.auth().setPersistence(this.remember.checked ? fireauth.Auth.Persistence.LOCAL : fireauth.Auth.Persistence.SESSION)

(here I have used javaScript for the example)

发布评论

评论列表(0)

  1. 暂无评论