Skip to content

Raising an InvalidTokenException if invalid token #1

Closed
wants to merge 2 commits into from

3 participants

@rudyrigot

I'm really, really not sure I'm doing this right; but I keep having that "invalid access token" issue, and I would just like to be able to sign out automatically when it happens.
But to do that, I need to know when it happens, and I figured the cleaner way to do it was to do it was to raise it as soon as the response comes back.
But I'm using a string comparison to do it; that's what makes me think there's probably a better way to do it.
Let me know.

@dohzya
prismic.io member
dohzya commented Nov 19, 2013

Small mistake on the test:

JSON.parse(response.body)['error'] == "Invalid access token"

(FYI include? checks if an object is present in a collection)

The test is based on the error message, so I'm afraid it is not so robust.
I think you should add a more generic AuthenticationException and raise it when the response.code is 401.
For instance:

if response.code == "401"
  error = JSON.parse(response.body)['error'] || response.body
  raise AuthenticationException, "Authentication Error : #{error}"
end
@rudyrigot

Ok, I didn't realize a 401 was raised, I thought it was a 500 ; so yeah, obviously, much better.

Now that you explained to me how to run this, I'll be able to write it and test it, so there shouldn't be many surprises. :)

I'll look at it today.

@rudyrigot rudyrigot closed this Nov 19, 2013
@guillaumebort

Take care there are both 401 and 403 cases to handle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.