2021-06-01 07:09:07 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-03-31 16:18:42 +00:00
|
|
|
class AuthenticationController < ApplicationController
|
2021-04-29 06:41:23 +00:00
|
|
|
skip_before_action :authenticate_request
|
|
|
|
|
|
|
|
|
|
def authenticate
|
|
|
|
|
command = AuthenticateUser.call(params[:email], params[:password])
|
|
|
|
|
|
|
|
|
|
if command.success?
|
2021-06-01 07:09:07 +00:00
|
|
|
user = User.find_by email: params[:email]
|
2021-06-12 03:11:56 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
render json: { auth_token: command.result, first_name: user.first_name, last_name: user.last_name,
|
|
|
|
|
email: user.email }
|
|
|
|
|
else
|
|
|
|
|
render json: { error: command.errors }, status: :unauthorized
|
2021-03-31 16:18:42 +00:00
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-05-16 16:42:50 +00:00
|
|
|
|
|
|
|
|
def signup
|
2021-05-16 16:56:48 +00:00
|
|
|
# Check if the installation allows user signups
|
2021-06-01 07:09:07 +00:00
|
|
|
if (ENV["DISABLE_SIGNUPS"] === "true")
|
|
|
|
|
render json: {}, status: :internal_server_error
|
2021-05-16 16:56:48 +00:00
|
|
|
else
|
|
|
|
|
email = params[:email]
|
|
|
|
|
password = SecureRandom.uuid
|
2021-06-01 07:09:07 +00:00
|
|
|
org = Organization.create(name: "new org")
|
2021-05-17 11:23:11 +00:00
|
|
|
user = User.create(email: email, password: password, organization: org, invitation_token: SecureRandom.uuid)
|
2021-05-16 16:42:50 +00:00
|
|
|
|
2021-06-02 04:50:50 +00:00
|
|
|
org_user = OrganizationUser.create(user_id: user.id, organization_id: org.id, role: "admin")
|
2021-05-16 16:42:50 +00:00
|
|
|
|
2021-06-12 03:11:56 +00:00
|
|
|
UserMailer.with(user: user, sender: @current_user).new_signup_email.deliver if org_user.save
|
2021-05-16 16:56:48 +00:00
|
|
|
end
|
2021-05-16 16:42:50 +00:00
|
|
|
end
|
2021-03-31 16:18:42 +00:00
|
|
|
end
|