Class: RegistrationService
- Inherits:
-
Object
- Object
- RegistrationService
- Defined in:
- app/services/registration_service.rb
Instance Attribute Summary collapse
-
#phone_number ⇒ Object
readonly
Returns the value of attribute phone_number.
-
#verification_token ⇒ Object
readonly
Returns the value of attribute verification_token.
Instance Method Summary collapse
- #append_login_attempt(verification) ⇒ Object
- #create_and_verify_user(verification) ⇒ Object
- #create_authentication_token(user_id) ⇒ Object
-
#initialize(phone_number, verification_token = nil) ⇒ RegistrationService
constructor
A new instance of RegistrationService.
- #register_user ⇒ Object
- #send_verification_token(phone_verification) ⇒ Object
- #token_matches?(verification) ⇒ Boolean
- #verification_error(verification) ⇒ Object
- #verification_invalid?(verification) ⇒ Boolean
- #verify_user ⇒ Object
Constructor Details
#initialize(phone_number, verification_token = nil) ⇒ RegistrationService
Returns a new instance of RegistrationService.
4 5 6 7 |
# File 'app/services/registration_service.rb', line 4 def initialize(phone_number, verification_token = nil) @phone_number = PhoneNumberUtils.cleanup_phone_number(phone_number) @verification_token = verification_token end |
Instance Attribute Details
#phone_number ⇒ Object (readonly)
Returns the value of attribute phone_number.
2 3 4 |
# File 'app/services/registration_service.rb', line 2 def phone_number @phone_number end |
#verification_token ⇒ Object (readonly)
Returns the value of attribute verification_token.
2 3 4 |
# File 'app/services/registration_service.rb', line 2 def verification_token @verification_token end |
Instance Method Details
#append_login_attempt(verification) ⇒ Object
90 91 92 93 |
# File 'app/services/registration_service.rb', line 90 def append_login_attempt(verification) verification.login_attempts.push(Time.zone.now.utc) verification.save! end |
#create_and_verify_user(verification) ⇒ Object
95 96 97 98 99 100 101 |
# File 'app/services/registration_service.rb', line 95 def create_and_verify_user(verification) user = User.create verification.user_id = user.id verification.verified_at = Time.zone.now.utc verification.save! user end |
#create_authentication_token(user_id) ⇒ Object
62 63 64 |
# File 'app/services/registration_service.rb', line 62 def create_authentication_token(user_id) AuthenticationToken.create(user_id: user_id) end |
#register_user ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/services/registration_service.rb', line 9 def register_user unless valid_phone_number?(phone_number) return [false, 'The phone number provided is not valid'] end unless allowable_phone_number?(phone_number) return [false, 'The phone number provided is not allowed to be interacted'] end verification = PhoneVerification.find_by_phone_number(phone_number) if verification.nil? verification = PhoneVerification.create(phone_number: phone_number) send_verification_token(verification) return [true, ''] end return [false, "Login attempts have been locked for this phone number. Try again in #{verification.unlocks_in_minutes} minutes."] if verification.locked? send_verification_token(verification) [true, ''] end |
#send_verification_token(phone_verification) ⇒ Object
31 32 33 34 |
# File 'app/services/registration_service.rb', line 31 def send_verification_token(phone_verification) phone_verification.reset_verification_token (phone_number, "Your verification code is #{phone_verification.verification_token}") end |
#token_matches?(verification) ⇒ Boolean
70 71 72 |
# File 'app/services/registration_service.rb', line 70 def token_matches?(verification) verification.verification_token == verification_token end |
#verification_error(verification) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/services/registration_service.rb', line 74 def verification_error(verification) if verification.nil? return verification_error_response('The information provided does not match. Please try again.') end if verification.locked? return verification_error_response("Login attempts have been locked for this phone number. Try again in #{verification.unlocks_in_minutes} minutes.") end if verification.expired? return verification_error_response('The verification token used is expired. Please request a new one and try again.') end verification_error_response('The information provided does not match. Please try again.') end |
#verification_invalid?(verification) ⇒ Boolean
66 67 68 |
# File 'app/services/registration_service.rb', line 66 def verification_invalid?(verification) verification.nil? || verification.locked? || verification.expired? end |
#verify_user ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/services/registration_service.rb', line 36 def verify_user verification = PhoneVerification.find_by_phone_number(phone_number) if verification_invalid?(verification) return verification_error(verification) end # Determine if this is the correct token # If it is not then log the attempt unless token_matches?(verification) append_login_attempt(verification) if verification.above_maximum_attempts? verification.lock_account return [false, 'This phone number has been temporarily locked due to unsuccessful login attempts. Please try again in a few minutes.'] end return [false, 'The verification token provided does not match', nil, nil, false] end # If a user doesnt exist, create one, associate it with the verification, and set verified_at user = create_and_verify_user(verification) unless verification.has_user? token = create_authentication_token(verification.user_id || user.id) verification.reset_verification_token [true, '', token.user_id, token.body, !user.nil?] end |