Sha256: bcaf4c6ad46adf73d34ff0bac3acdb148c4b5447f9ecb6a4a8b6c86d91446281

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module Ctws
  class UsersController < CtwsController
    skip_before_action :authorize_request, only: :create
    
    # POST /signup
    # return authenticated token upon signup
    def create
      # We use Active Record's create! method so that in the event there's an error, 
      # an exception will be raised and handled in the exception handler.
      ctws_user = Ctws.user_class.create!(ctws_user_params)
      if Ctws.user_validate_with_password
        auth_token = Ctws::AuthenticateUser.new(ctws_user.email, ctws_user.password).call
      elsif !Ctws.user_validate_with_password
        auth_token = Ctws::AuthenticateUser.new(ctws_user.email).call
      end
      # response = { message: Ctws::Message.account_created, auth_token: auth_token }
      
      json_response(user_as_jsonapi(ctws_user, auth_token), :created)
    end
    
    private
    
    def user_as_jsonapi user, auth_token
      [{
        type: ActiveModel::Naming.param_key(Ctws.user_class),
        id: user.id,
        attributes: {
          message: Ctws::Message.account_created, 
          auth_token: auth_token, 
          created_at: user.created_at
        },
        relationships: {
          device_apps: {
            data: [{ 
              type: ActiveModel::Naming.param_key(Ctws.device_class), 
              id: Ctws.device_class.where("#{Ctws.user_class.name.underscore}_id": user.id).last.id,
              attributes: Ctws.device_class.where("#{Ctws.user_class.name.underscore}_id": user.id).last
            }]
          },
          profile: {
            data: [{ 
              type: ActiveModel::Naming.param_key(Ctws.profile_class), 
              id: Ctws.profile_class.where("#{Ctws.user_class.name.underscore}_id": user.id).last.id,
              attributes: Ctws.profile_class.where("#{Ctws.user_class.name.underscore}_id": user.id).last
            }]
          }
        }
      }]
    end
    
    def ctws_user_params
      params.permit(Ctws.user_class_strong_params)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ctws-0.1.10.alpha app/controllers/ctws/users_controller.rb