mirror of
https://github.com/wahyd4/libr-2.git
synced 2026-08-19 10:16:12 +10:00
38 lines
996 B
Ruby
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
|