I have a couple questions about token expiration (using custom auth):
1. If the user logs out, is there a way to tell firebase that their token should no longer be valid? It looks like you can auth, unauth, and then auth again using the same token. It seems like this is a potential security hole to not be able to cancel a token on log out.
2. Is there a way to smoothly recover from token expiration? Right now my auth code looks something like this (in coffeescript):
do_auth = ->
token = get_fresh_token()
my_base.auth token, do_some_stuff, ->
do_auth()
Ie, on expiration, I automatically get a fresh token and auth again. This part works great, but unfortunately it interrupts all my .on() calls. Is there an easy way to get them to persist past token expiration? I'd want them to transparently resume updating with fresh data as soon as the token is refreshed. I suppose I could do some pattern like:
create_on = (base, event, cb) ->
base.on event, cb
add_to_list_to_be_recreated_on_reauth base, event, cb
...but that seems a little clunky, plus I can see that leading to issues with 'child_added' since I think that would reload all the initial data again.
Is there a nicer pattern for handling this?
Here's some test code I was playing with to confirm that token expiration breaks .on() handlers.
Firebase = require 'firebase'
TokenGenerator = require 'firebase-token-generator'
generator = new TokenGenerator firebase_secret
test_node = new Firebase firebase_url
#Creates a token that expires in 10 seconds
make_token = -> generator.createToken {}, {admin: true, expires: new Date().valueOf() / 1000 + 10}
#Authenticates, calls cb when done, and then re-authenticates on token expiration
do_auth = (cb) ->
my_base.auth make_token(), (err) ->
if err
console.log err
else
console.log 'auth succeed'
cb?()
, (exp) ->
console.log 'expired: ' + exp
do_auth()
#Authenticate, then count off the seconds
do_auth ->
test_node.on 'value', (snap) ->
console.log 'got a value: ' + snap.val()
, (exp) ->
#For some reason this code never seems to run!!
console.log 'lost permission: ' + exp
for i in [1..60]
do (i) ->
setTimeout (-> test_node.set i), i * 1000
The output of this code is:
auth succeed
got a value: 60 #my initial value from last time
got a value: 1
got a value: 2
got a value: 3
got a value: 4
got a value: 5
got a value: 6
got a value: 7
got a value: 8
FIREBASE WARNING: on() or once() for /test_node failed: permission_denied
lost permission: undefined
FIREBASE WARNING: auth() was canceled: Auth token is expired.
expired: Error: Auth token is expired.
auth succeed
FIREBASE WARNING: auth() was canceled: Auth token is expired.
expired: Error: Auth token is expired.
auth succeed
#...and so on to infinity... we never see numbers past 8.