class UserController < Users::ApplicationController
  before_action :allow_all_origins
  layout 'user/foundation'

  def index
    handle_message_params
  end

  def feedback
    user_feedback = UserFeedback.create(user_id: current_user.id, content: params[:feedback].to_s)
    Notifier.user_feedback(user_feedback.id).deliver_later
    render json: {}
  end

  def obtain_token
    redirect_to user_omniauth_authorize_path(params[:provider].presence || 'facebook') and return unless current_user.present?
    respond_to do |format|
      format.html { render layout: 'user/obtain_token' }
      format.json do
        render json: json_response(ok: false, status: 401) and return unless params[:user].present? && params[:user][:email].present? && params[:user][:password].present?
        user = User.find_by_email(params[:user][:email])
        render json: json_response(ok: false, status: 401) and return unless user
        render json: json_response(ok: false, status: 401) and return unless user.valid_password?(params[:user][:password])
        user.ensure_authentication_token
        sign_in user
        render json: json_response(ok: true, auth_token: user.authentication_token, user_id: user.id)
      end

    end
  end

end
