Files
libr-2/app/controllers/application_controller.rb
T
2015-08-02 12:45:38 +08:00

38 lines
996 B
Ruby

class ApplicationController < ActionController::Base
include ActionController::Serialization
serialization_scope :current_user!
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
# protect_from_forgery with: :exception
protect_from_forgery with: :null_session
before_action :current_user!
def current_user!
@current_user = nil
@current_user = current_user if user_signed_in?
ap @current_user
end
def user_json(user= current_user)
{
user: {
id: user.id,
name: user.name,
email: user.email,
token: JWT.encode({token: user.active_auth_token}, user.email)
}
}
end
def authenticate_user_from_token
auth_token = AuthToken.find_by(value: request.headers["Authorization"].try(:split, ' ').try(:last))
if auth_token
@current_user = auth_token.user
else
return render json: {message: 'Token is not valid'}, status: 401
end
end
end