-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapplication_controller.rb
More file actions
50 lines (44 loc) · 1.36 KB
/
application_controller.rb
File metadata and controls
50 lines (44 loc) · 1.36 KB
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
40
41
42
43
44
45
46
47
48
49
50
class ApplicationController < ActionController::API
include CanCan::ControllerAdditions
rescue_from ActiveRecord::RecordNotFound do
render json: { Error: "Resource not found" }, status: 404
end
rescue_from CanCan::AccessDenied do
render json: { Error: " Tah!! You are not authorized" }, status: 403
end
attr_reader :current_user, :token
helper_method :current_user
def no_route_found
found = { Error: "The end point you requested does not exist.",
Debug: "Please check the documentation for existing end points" }
render json: found, status: 404
end
def authenticate
@token = request.headers["HTTP_AUTHORIZATION"]
if token_has_expired(token)
render json: { Error: "Token has expired, please login again" }, status: 401
else
status, payload = Api::V1::Authenticate.decode_token(token)
set_payload(status, payload)
end
end
def activate(user)
unless user
render json: { Error: "You must login first" }, status: 401
end
end
private
def token_has_expired(token)
user_expired_tokens = ExpiredToken.where(token: token)
user_expired_tokens.present?
end
def set_payload(status, payload)
if status
user = payload
@current_user = User.find_by(fb_id: user["fb_id"])
activate(@current_user)
else
render json: payload, status: 401
end
end
end