Skip to content

Instantly share code, notes, and snippets.

@donrestarone
Created March 4, 2020 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donrestarone/3eef57f26cb153cdf10cdb32c93e9ee0 to your computer and use it in GitHub Desktop.
Save donrestarone/3eef57f26cb153cdf10cdb32c93e9ee0 to your computer and use it in GitHub Desktop.
simple cookie authentication helpers in application controller
class ApplicationController < ActionController::API
include ActionController::Cookies
def authenticate_cookie
token = cookies.signed[:jwt]
decoded_token = CoreModules::JsonWebToken.decode(token)
if decoded_token
user = User.find_by(id: decoded_token["user_id"])
end
if user then return true else render json: {status: 'unauthorized', code: 401} end
end
def current_user
token = cookies.signed[:jwt]
decoded_token = CoreModules::JsonWebToken.decode(token)
if decoded_token
user = User.find_by(id: decoded_token["user_id"])
end
if user then return user else return false end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment