Add API controller to get the current user. Also create authenticate_user_with_json! so we can authenticate properly using JSON.

This commit is contained in:
Eric Schultz 2021-03-01 17:46:31 -06:00 committed by Eric Schultz
parent 4757b63f69
commit 7a57bfab7b
3 changed files with 21 additions and 2 deletions

View file

@ -2,7 +2,9 @@
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
class Api::ApiController < ApplicationController class Api::ApiController < ActionController::Base
include Controllers::Locale
include Controllers::Nonprofit::Authorization
rescue_from ActiveRecord::RecordInvalid, with: :record_invalid_rescue rescue_from ActiveRecord::RecordInvalid, with: :record_invalid_rescue
protected protected

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
class Api::UsersController < Api::ApiController
include Controllers::User::Authorization
before_action :authenticate_user_with_json!
def current
render locals: { user: current_user }
end
end

View file

@ -10,7 +10,11 @@ module Controllers::User::Authorization
included do included do
helper_method :current_role?, :administered_nonprofit helper_method :current_role?, :administered_nonprofit
private protected
def authenticate_user_with_json!
reject_with_sign_in({}, :json) unless current_user
end
def authenticate_user!(msg = nil, type = :html) def authenticate_user!(msg = nil, type = :html)
reject_with_sign_in(msg, type) unless current_user reject_with_sign_in(msg, type) unless current_user