mirror of
https://github.com/wahyd4/libr-2.git
synced 2026-08-22 03:35:58 +10:00
34 lines
851 B
Ruby
34 lines
851 B
Ruby
class ApplicationController < ActionController::Base
|
|
include ActionController::Serialization
|
|
|
|
serialization_scope :current_user!
|
|
skip_before_action :verify_authenticity_token
|
|
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
|