Class: Securial::AccountsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/securial/accounts_controller.rb

Overview

AccountsController

This controller handles user account-related operations including:

- User registration
- Profile viewing and management
-  updates
-  deletion

Routes typically mounted at Securial/accounts/* in the host application.

Instance Method Summary collapse

Methods inherited from ApplicationController

#render_400, #render_404

Instance Method Details

#delete_accountvoid

This method returns an undefined value.

Permanently deletes the current user’s account.

Removes the user account and all associated data after password verification.

Parameters:

  • params[:current_password] (String)

    User’s current password for verification



93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/securial/accounts_controller.rb', line 93

def 
  @securial_user = Current.user
  if @securial_user.authenticate(params.expect(securial_user: [:current_password]).dig(:current_password))
    @securial_user.destroy
    render json: { message: "Account deleted successfully" }, status: :ok
  else
    render json: {
      errors: ["Current password is incorrect"],
      instructions: "Please verify your current password and try again.",
    }, status: :unprocessable_entity
  end
end

#mevoid

This method returns an undefined value.

Retrieves the current user’s profile.

Provides the authenticated user’s complete profile information. Requires authentication via the Identity concern.



20
21
22
23
24
# File 'app/controllers/securial/accounts_controller.rb', line 20

def me
  @securial_user = Current.user

  render :show, status: :ok, location: @securial_user
end

#registervoid

This method returns an undefined value.

Registers a new user account.

Creates a new user in the system with the provided registration information.

Parameters:

  • params[:securial_user] (Hash)

    User attributes including email_address, password, etc.



51
52
53
54
55
56
57
58
59
# File 'app/controllers/securial/accounts_controller.rb', line 51

def register
  @securial_user = Securial::User.new(user_params)
  if @securial_user.save
    render :show, status: :created, location: @securial_user
  else
    render json: {
      errors: @securial_user.errors.full_messages }, status: :unprocessable_entity
  end
end

#render_user_profilevoid (private)

This method returns an undefined value.

Renders the user profile or a not found response.



120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/securial/accounts_controller.rb', line 120

def 
  if @securial_user
    render :show, status: :ok, location: @securial_user
  else
    render json: {
      errors: ["User not found"],
      instructions: "Please check the username and try again.",
    }, status: :not_found
  end
end

#showvoid

This method returns an undefined value.

Shows a specific user’s profile by username.

Retrieves and displays public profile information for the requested user. Requires the ‘enable_other_profiles` configuration to be true.

Parameters:

  • params[:username] (String)

    The username of the requested user profile



33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/securial/accounts_controller.rb', line 33

def show
  if Securial.configuration.enable_other_profiles
    @securial_user = Securial::User.find_by(username: params.expect(:username))
    
  else
    render json: {
      errors: ["User profiles are not enabled"],
      instructions: "Please contact support for assistance.",
    }, status: :forbidden
  end
end

#update_profilevoid

This method returns an undefined value.

Updates the current user’s profile information.

Allows users to modify their profile after authenticating with their current password.

Parameters:

  • current_password (String)

    User’s current password for verification

  • params[:securial_user] (Hash)

    Updated user attributes



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/securial/accounts_controller.rb', line 68

def update_profile
  @securial_user = Current.user
  if @securial_user.authenticate(params[:securial_user][:current_password])
    if @securial_user.update(user_params)
      render :show, status: :ok, location: @securial_user
    else
      render json: {
        errors: @securial_user.errors.full_messages,
        instructions: "Please ensure all required fields are filled out correctly.",
        }, status: :unprocessable_entity
    end
  else
    render json: {
      errors: ["Current password is incorrect"],
      instructions: "Please verify your current password and try again.",
      }, status: :unprocessable_entity
  end
end

#user_paramsActionController::Parameters (private)

Permits and extracts user parameters from the request.

Returns:

  • (ActionController::Parameters)

    Permitted user parameters



112
113
114
# File 'app/controllers/securial/accounts_controller.rb', line 112

def user_params
  params.expect(securial_user: [:email_address, :password, :password_confirmation, :first_name, :last_name, :phone, :username, :bio])
end