You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
John Bachir edited this page Feb 8, 2017
·
1 revision
Here is one approach to API authentication
classApplicationControllerprotecteddefauthenticate_via_tokenreturnunlessapi_tokenuser=User.find_by_api_token(api_token)sign_inuserifusercookies.delete(:remember_token)# so non-browser clients don't act like browsers and persist sessions in cookiesendprivatedefapi_tokenpattern=/^Bearer /header=request.env["HTTP_AUTHORIZATION"]header.gsub(pattern,'')ifheader && header.match(pattern)endendclassMyController < ApplicationControllerbefore_action:authenticate_via_tokenend