40 lines
711 B
Ruby
40 lines
711 B
Ruby
|
module Mobile
|
||
|
|
||
|
$LOAD_PATH << File.expand_path('..',__FILE__)
|
||
|
|
||
|
autoload :Auth, 'apis/auth'
|
||
|
autoload :Users, 'apis/users'
|
||
|
autoload :Courses, 'apis/courses'
|
||
|
|
||
|
class API < Grape::API
|
||
|
version 'v1', using: :path
|
||
|
format :json
|
||
|
|
||
|
helpers do
|
||
|
def logger
|
||
|
API.logger
|
||
|
end
|
||
|
|
||
|
def authticate!
|
||
|
error!('Unauthorized. Invalid or expired token.', 401) unless current_user
|
||
|
end
|
||
|
|
||
|
def current_user
|
||
|
token = ApiKey.where(access_token: params[:token]).first
|
||
|
if token && !token.expired?
|
||
|
@current_user = User.find(token.user_id)
|
||
|
else
|
||
|
nil
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
mount Auth
|
||
|
mount Users
|
||
|
mount Courses
|
||
|
|
||
|
end
|
||
|
end
|
||
|
|
||
|
|