Class: ApiUserAuth::AuthUser
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ApiUserAuth::AuthUser
- Defined in:
- app/models/api_user_auth/auth_user.rb
Overview
Base user auth model
Instance Attribute Summary collapse
-
#is_new ⇒ Object
Returns the value of attribute is_new.
Class Method Summary collapse
- .create_by_params(params) ⇒ Object
- .create_by_provider(params) ⇒ Object
- .find_fy_token(token) ⇒ Object
- .forgot_password(params) ⇒ Object
- .login_by_params(params) ⇒ Object
- .update_password(params) ⇒ Object
Instance Method Summary collapse
- #generate_token ⇒ Object
- #logout(token) ⇒ Object
- #password=(passwd) ⇒ Object
- #send_reset_password ⇒ Object
- #to_json ⇒ Object
- #update_password(password) ⇒ Object
- #valid_password?(passwd) ⇒ Boolean
Instance Attribute Details
#is_new ⇒ Object
Returns the value of attribute is_new.
6 7 8 |
# File 'app/models/api_user_auth/auth_user.rb', line 6 def is_new @is_new end |
Class Method Details
.create_by_params(params) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/models/api_user_auth/auth_user.rb', line 8 def self.create_by_params(params) if params[:email].blank? raise Exceptions::WrongParams, 'Email can not be blank!' end if params[:password].blank? raise Exceptions::WrongParams, 'Password can not be blank!' end auth_user = AuthUser.find_or_initialize_by(email: params[:email]) if auth_user.new_record? auth_user.is_new = true auth_user.update_password(params[:password]) else raise Exceptions::WrongParams, 'User already exists !' end auth_user end |
.create_by_provider(params) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/api_user_auth/auth_user.rb', line 87 def self.create_by_provider(params) if params[:provider].blank? raise Exceptions::WrongParams, 'Provider can not be blank!' end if params[:token].blank? raise Exceptions::WrongParams, 'Token can not be blank!' end provider_data = case params[:provider] when /facebook/i Providers::Facebook.get_user(params[:token]) when /google/i Providers::Google.get_user(params[:token]) when /instagram/i Providers::Instagram.get_user(params[:token]) else raise ::ApiUserAuth::Exceptions::ProviderError, 'Wrong provider!' end auth_user = AuthUser.find_or_initialize_by(email: provider_data[:email]) auth_user.encrypted_password = params[:token] auth_user.generate_token auth_user.is_new = auth_user.new_record? auth_user.user_provider_data = provider_data auth_user.provider = params[:provider] auth_user.save auth_user end |
.find_fy_token(token) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'app/models/api_user_auth/auth_user.rb', line 117 def self.find_fy_token(token) unless token =~ ApiUserAuth::UUID_REGEX raise Exceptions::Unauthorized, 'Header [Authorization] token is invalid!' end where( '? = ANY("api_user_auth_auth_users"."auth_tokens")', token ).limit(1).first end |
.forgot_password(params) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/models/api_user_auth/auth_user.rb', line 75 def self.forgot_password(params) if params[:email].blank? raise Exceptions::WrongParams, 'Email can not be blank!' end auth_user = AuthUser.find_by(email: params[:email]) if auth_user.blank? raise Exceptions::WrongParams, 'Email is invalid!' end auth_user.send_reset_password auth_user end |
.login_by_params(params) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/api_user_auth/auth_user.rb', line 26 def self.login_by_params(params) if params[:email].blank? raise Exceptions::WrongParams, 'Email can not be blank!' end if params[:password].blank? raise Exceptions::WrongParams, 'Password can not be blank!' end auth_user = AuthUser.find_by(email: params[:email]) if auth_user && auth_user.valid_password?(params[:password]) auth_user.generate_token auth_user.save auth_user.is_new = false else raise Exceptions::Unauthorized, 'Invalid Email or Password!' end auth_user end |
.update_password(params) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/models/api_user_auth/auth_user.rb', line 46 def self.update_password(params) if params[:email].blank? raise Exceptions::WrongParams, 'Email can not be blank!' end if params[:password].blank? raise Exceptions::WrongParams, 'Password can not be blank!' end if params[:code].blank? raise Exceptions::WrongParams, 'Code can not be blank!' end auth_user = AuthUser.find_by(email: params[:email]) if auth_user.blank? raise Exceptions::WrongParams, 'Email is invalid!' end if auth_user.code.eql?(params[:code]) auth_user.update_password(params[:password]) else raise Exceptions::WrongParams, 'Code is invalid!' end end |
Instance Method Details
#generate_token ⇒ Object
132 133 134 |
# File 'app/models/api_user_auth/auth_user.rb', line 132 def generate_token auth_tokens << SecureRandom.uuid end |
#logout(token) ⇒ Object
149 150 151 152 |
# File 'app/models/api_user_auth/auth_user.rb', line 149 def logout(token) auth_tokens.delete(token) save end |
#password=(passwd) ⇒ Object
136 137 138 |
# File 'app/models/api_user_auth/auth_user.rb', line 136 def password=(passwd) self.encrypted_password = hexdigest(passwd) if passwd.present? end |
#send_reset_password ⇒ Object
144 145 146 147 |
# File 'app/models/api_user_auth/auth_user.rb', line 144 def send_reset_password self.code = Random.new.rand((10**(6 - 1))..(10**6)).to_s ForgotPasswordMailer.reset_code(self).deliver_now if save end |
#to_json ⇒ Object
128 129 130 |
# File 'app/models/api_user_auth/auth_user.rb', line 128 def to_json { id: id, email: email, auth_token: auth_tokens.last, is_new: is_new } end |
#update_password(password) ⇒ Object
69 70 71 72 73 |
# File 'app/models/api_user_auth/auth_user.rb', line 69 def update_password(password) self.password = password generate_token save end |
#valid_password?(passwd) ⇒ Boolean
140 141 142 |
# File 'app/models/api_user_auth/auth_user.rb', line 140 def valid_password?(passwd) encrypted_password == hexdigest(passwd) end |