Skip to content

Instantly share code, notes, and snippets.

@Hasstrup
Last active November 5, 2024 18:09
Show Gist options
  • Save Hasstrup/d9e07c2d11f1550efaada3d603c3452d to your computer and use it in GitHub Desktop.
Save Hasstrup/d9e07c2d11f1550efaada3d603c3452d to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# The Registration class handles the user registration process.
# It validates input and creates a new user in the database,
# generating an authentication token upon successful registration.
class Users::Contexts::Registration < BaseService
performs_checks
def initialize(input:)
@input = input
end
def call
safely_execute do
ActiveRecord::Base.transaction do
succeed(authentication_token)
end
end
end
private
def authentication_token
@authentication_token ||= Tokens::AuthenticationToken.create!(
owner: user,
token: Encryption::TokenManager.payload_2_token(data: { id: user.id })
)
end
def user
@user ||= User.create!(**input.to_h)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment