Class: CommandTower::Auth::PlainTextController

Inherits:
CommandTower::ApplicationController show all
Includes:
SchemaHelper
Defined in:
app/controllers/command_tower/auth/plain_text_controller.rb

Constant Summary

Constants inherited from CommandTower::ApplicationController

CommandTower::ApplicationController::AUTHENTICATION_EXPIRE_HEADER, CommandTower::ApplicationController::AUTHENTICATION_HEADER, CommandTower::ApplicationController::AUTHENTICATION_WITH_RESET

Instance Method Summary collapse

Methods included from SchemaHelper

#invalid_arguments!, #schema_succesful!

Methods inherited from CommandTower::ApplicationController

#authenticate_user!, #authenticate_user_without_email_verification!, #authorize_user!, #current_user, #safe_boolean

Instance Method Details

#create_postObject

POST /auth/create New PlainText user creation



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/command_tower/auth/plain_text_controller.rb', line 39

def create_post
  result = CommandTower::LoginStrategy::PlainText::Create.(**create_params)
  if result.success?
    schema = CommandTower::Schema::PlainText::CreateUserResponse.new(
      full_name: result.user.full_name,
      first_name: result.first_name,
      last_name: result.last_name,
      username: result.username,
      email: result.email,
      msg: "Successfully created new User",
    )
    status = 201
    schema_succesful!(status:, schema:)
  else
    if result.invalid_arguments
      invalid_arguments!(
        status: 400,
        message: result.msg,
        argument_object: result.invalid_argument_hash,
        schema: CommandTower::Schema::PlainText::CreateUserRequest
      )
    end
  end
end

#email_verify_postObject

POST /auth/email/verify Verifies a logged in users email verification code when enabled



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/command_tower/auth/plain_text_controller.rb', line 66

def email_verify_post
  if current_user.email_validated
    schema = CommandTower::Schema::PlainText::EmailVerifyResponse.new(message: "Email is already verified.")
    status = 200
    schema_succesful!(status:, schema:)
  else
    result = CommandTower::LoginStrategy::PlainText::EmailVerification::Verify.(user: current_user, code: params[:code])
    if result.success?
      schema = CommandTower::Schema::PlainText::EmailVerifyResponse.new(message: "Successfully verified email")
      status = 201
      schema_succesful!(status:, schema:)
    else
      if result.invalid_arguments
        invalid_arguments!(
          status: result.status || 403,
          message: result.msg,
          argument_object: result.invalid_argument_hash,
          schema: CommandTower::Schema::PlainText::EmailVerifyRequest
        )
      end
    end
  end
end

#email_verify_resend_postObject

POST /auth/email/send Sends a logged in users email verification code



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/command_tower/auth/plain_text_controller.rb', line 92

def email_verify_resend_post
  if current_user.email_validated
    schema = CommandTower::Schema::PlainText::EmailVerifyResponse.new(message: "Email is already verified. No code required")
    status = 200
    schema_succesful!(status:, schema:)
  else
    result = CommandTower::LoginStrategy::PlainText::EmailVerification::Send.(user: current_user)
    if result.success?
      schema = CommandTower::Schema::PlainText::EmailVerifyResponse.new(message: "Successfully sent Email verification code")
      status = 201
      schema_succesful!(status:, schema:)
    else
      schema = CommandTower::Schema::Error::Base.new(status:, message: result.msg)
      status = result.status || 401
      render(json: schema.to_h, status:)
    end
  end
end

#login_postObject

POST /auth/login Login to the application and create/set the JWT token



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/command_tower/auth/plain_text_controller.rb', line 10

def 
  result = CommandTower::LoginStrategy::PlainText::Login.(**)
  if result.success?
    schema = CommandTower::Schema::PlainText::LoginResponse.new(
      token: result.token,
      header_name: AUTHENTICATION_HEADER,
      user: CommandTower::Schema::User.convert_user_object(user: result.user),
      message: "Successfully logged user in"
    )
    status = 201
    schema_succesful!(status:, schema:)
  else
    if result.invalid_arguments
      invalid_arguments!(
        status: 401,
        message: result.msg,
        argument_object: result.invalid_argument_hash,
        schema: CommandTower::Schema::PlainText::LoginRequest
      )
    else
      json_result = { msg: result.msg }
      status = 400
      render(json: schema.to_h, status:)
    end
  end
end