How to know when auth token has expired

5,644 views
Skip to first unread message

Adam Brooks

unread,
Jun 6, 2013, 11:58:51 AM6/6/13
to fireba...@googlegroups.com
Hi,

Outside of simple auth how should my app tell when the auth token has expired? Using FirebaseAuthClient I get callbacks when the status of the user changes, but I don't get this with .auth(). How should I track the expiry?

(I think Firebase spoiled me with the simple auth. Using anything else know just feels painful!)

Cheers,
Adam.

Andrew Lee

unread,
Jun 6, 2013, 12:07:43 PM6/6/13
to fireba...@googlegroups.com
Adam - 

Take a look at the docs for auth:

There's an onComplete that gets called with an Error if the authentication failed. There's also an onCancel that gets called later on if the token expires.

-Andrew


On Thu, Jun 6, 2013 at 9:03 AM, Jonathan Friedman <kg4...@gmail.com> wrote:
We just wait for a request to fail and reauthenticate as needed.

Coffeescript (runs on client):

dataRef.auth firebaseToken, (error) ->
        if error?
          if error.code is "EXPIRED_TOKEN"
            console.log "[firebaseAuthError]", "EXPIRED_TOKEN Reconnecting..."
            newToken = true
            token connect
            return
          else
            console.log "[firebaseAuthError]", "Login failed!", error
            return
--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Andrew Lee
Founder, Firebase
http://twitter.com/startupandrew

Adam Brooks

unread,
Jun 6, 2013, 12:08:46 PM6/6/13
to fireba...@googlegroups.com
Thanks Jonathan.

How does this fit in the flow? Do you call .auth() before every Firebase operation?

Adam.


On Thu, Jun 6, 2013 at 5:03 PM, Jonathan Friedman <kg4...@gmail.com> wrote:
We just wait for a request to fail and reauthenticate as needed.

Coffeescript (runs on client):

dataRef.auth firebaseToken, (error) ->
        if error?
          if error.code is "EXPIRED_TOKEN"
            console.log "[firebaseAuthError]", "EXPIRED_TOKEN Reconnecting..."
            newToken = true
            token connect
            return
          else
            console.log "[firebaseAuthError]", "Login failed!", error
            return

On Thursday, June 6, 2013 10:58:51 AM UTC-5, Adam Brooks wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/btaE-hCVQdk/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.

Andrew Lee

unread,
Jun 6, 2013, 12:09:54 PM6/6/13
to fireba...@googlegroups.com
Adam -

You should only ever call auth() once for each authentication. Just monitor the 2 callbacks: onComplete and onCancel to know when things fail or expire.

-Andrew


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Adam Brooks

unread,
Jun 6, 2013, 12:09:55 PM6/6/13
to fireba...@googlegroups.com
Ah, great, thanks Andrew.


--
You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/btaE-hCVQdk/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.

Jonathan Friedman

unread,
Jun 6, 2013, 12:22:23 PM6/6/13
to fireba...@googlegroups.com
The docs say:

auth( authToken, [onComplete][onCancel] )

Is there a missing comma between the two function pointers? Like this:

auth( authToken, [onComplete], [onCancel] )

The usage example shows only one function. Is it used for both purposes?

dataRef.auth(AUTH_TOKEN, function(error) {

Should that be:

dataRef.auth(AUTH_TOKEN, function(error) {..}, function(error) {..})

To use both callbacks?

Thanks,
-Jonathan

Rob DiMarco

unread,
Jun 6, 2013, 1:21:53 PM6/6/13
to fireba...@googlegroups.com
Hi gents -

At a high level, your application should be using either custom login (where you generate a token and pass it to Firebase via .auth()) or Simple Login (where you use the FirebaseAuthClient and call .login() to invoke a specific login method).

In Simple Login, the callback that you pass to the FirebaseAuthClient constructor will be invoked once after instantiation with the current state of the user, and again any time the user's login state changes. So if the underlying session expires, or the user logs out, or the Firebase auth. token that is automatically generated by Simple Login expires, this callback will be invoked with user=null, error=null to indicate that the user is logged out, and you should attempt to reauthenticate.

I hope that makes sense and helps!

Rob
Reply all
Reply to author
Forward
0 new messages