Created
March 4, 2020 02:38
-
-
Save donrestarone/3eef57f26cb153cdf10cdb32c93e9ee0 to your computer and use it in GitHub Desktop.
simple cookie authentication helpers in application controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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