class UsersController < ApplicationController def create user = User.create!(inheritance_code: SecureRandom.uuid, role: 'guest') render json: { code: user.inheritance_code, user: user.slice(:id, :name, :inheritance_code, :role) } end def verify user = User.find_by(inheritance_code: params[:code]) render json: if user { valid: true, user: user.slice(:id, :name, :inheritance_code, :role) } else { valid: false } end end def renew user = current_user user.inheritance_code = SecureRandom.uuid user.save! render json: { code: user.inheritance_code } end def me user = User.find_by(inheritance_code: params[:code]) return head :not_found unless user render json: user.slice(:id, :name, :inheritance_code, :role) end def update user = current_user return head :unauthorized if user&.id != params[:id].to_i name = params[:name] return head :bad_request if name.blank? if user.update(name:) render json: user.slice(:id, :name, :inheritance_code, :role), status: :created else render json: user.errors, status: :unprocessable_entity end end end