blob: 29a88d12cc59bfa491c81c4d1432008976513020 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
class Api::Controller < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :null_session
before_action :authenticate!
protected
def current_session(session_id = auth_token[:session_id])
@current_session ||= UserSession.authenticate(session_id)
end
def current_user
@current_user ||= User.find(current_session.try(:user_id))
rescue ActiveRecord::RecordNotFound
nil
end
private
def authenticate!
return if current_user.present?
not_authenticated!
rescue
not_authenticated!
end
def not_authenticated!
render json: { errors: ['Not Authenticated'] }, status: :unauthorized
end
def auth_token
@auth_token ||= JsonWebToken.decode(http_token)
end
def http_token
request.headers['Authorization'].split(' ').last
end
end
|