Last active
November 5, 2024 18:09
-
-
Save Hasstrup/d9e07c2d11f1550efaada3d603c3452d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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