62 lines
1.8 KiB
Ruby
62 lines
1.8 KiB
Ruby
module Api
|
|
module V1
|
|
class AccountsController < ApplicationController
|
|
def show
|
|
render json: user_json(current_user)
|
|
end
|
|
|
|
def update
|
|
name = params[:name].to_s.strip
|
|
if name.blank?
|
|
return render json: { error: "Name is required" }, status: :unprocessable_entity
|
|
end
|
|
|
|
if current_user.update(name: name)
|
|
render json: user_json(current_user)
|
|
else
|
|
render json: { error: current_user.errors.full_messages.join(", ") }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def password
|
|
result = Users::ChangePassword.call(
|
|
user: current_user,
|
|
current_password: params[:current_password],
|
|
password: params[:password],
|
|
password_confirmation: params[:password_confirmation]
|
|
)
|
|
|
|
unless result.ok?
|
|
return render json: { error: password_error_message(result.error) }, status: :unprocessable_entity
|
|
end
|
|
|
|
render json: { message: "Password updated" }
|
|
end
|
|
|
|
private
|
|
|
|
def user_json(user)
|
|
{
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
role: user.role
|
|
}
|
|
end
|
|
|
|
def password_error_message(code)
|
|
case code
|
|
when :current_incorrect then "Current password is incorrect"
|
|
when :too_short then "Password must be at least 8 characters"
|
|
when :too_long then "Password cannot exceed 72 characters"
|
|
when :too_weak
|
|
"Password must include at least 3 of: lowercase, uppercase, number, symbol"
|
|
when :same_as_current then "New password must be different from the current password"
|
|
when :mismatch then "Passwords do not match"
|
|
else "Unable to update password"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|