mirror of
https://github.com/wahyd4/libr-2.git
synced 2026-08-09 13:26:05 +10:00
32 lines
781 B
Ruby
32 lines
781 B
Ruby
class ApplicationController < ActionController::Base
|
|
include ActionController::Serialization
|
|
|
|
serialization_scope :view_context
|
|
before_action :current_user!
|
|
|
|
def current_user!
|
|
@current_user = nil
|
|
@current_user = current_user if user_signed_in?
|
|
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
|