Module: RailsAuthUser
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/rails_auth_user.rb
Overview
include this module to your User model
class User < ApplicationRecord
include RailsAuthUser
end
Instance Method Summary collapse
- #access_token ⇒ Object
- #auth_token ⇒ Object
- #authenticate_by_token(token) ⇒ Object
- #avatar_url ⇒ Object
-
#can_login?(params) ⇒ Boolean
pass login params to this method;.
- #email_token ⇒ Object
- #generate_auth_token(**options) ⇒ Object
- #invalid_access_token ⇒ Object
- #join(params = {}) ⇒ Object
- #mobile_token ⇒ Object
- #oauth_providers ⇒ Object
- #reset_token ⇒ Object
- #unlock_token ⇒ Object
- #verified_status? ⇒ Boolean
Instance Method Details
#access_token ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/concerns/rails_auth_user.rb', line 42 def access_token if super super else VerifyToken.transaction do self.access_tokens.delete_all create_access_token end end end |
#auth_token ⇒ Object
53 54 55 |
# File 'app/models/concerns/rails_auth_user.rb', line 53 def auth_token access_token.token end |
#authenticate_by_token(token) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'app/models/concerns/rails_auth_user.rb', line 148 def authenticate_by_token(token) mobile_token = self.mobile_tokens.valid.find_by(token: token) if mobile_token self.update(mobile_confirmed: true) else false end end |
#avatar_url ⇒ Object
157 158 159 |
# File 'app/models/concerns/rails_auth_user.rb', line 157 def avatar_url avatar.service_url if avatar..present? end |
#can_login?(params) ⇒ Boolean
pass login params to this method;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/models/concerns/rails_auth_user.rb', line 114 def can_login?(params) if verified_status? return false end if password_digest? && params[:password].present? if authenticate(params[:password]) self else errors.add :base, :wrong_name_or_password return false end elsif params[:token].present? if authenticate_by_token(params[:token]) self else errors.add :base, :wrong_token return false end else errors.add :base, :token_blank false end end |
#email_token ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'app/models/concerns/rails_auth_user.rb', line 90 def email_token if super super else VerifyToken.transaction do self.email_tokens.delete_all create_email_token end end end |
#generate_auth_token(**options) ⇒ Object
169 170 171 |
# File 'app/models/concerns/rails_auth_user.rb', line 169 def generate_auth_token(**) JwtHelper.generate_jwt_token(id, password_digest, ) end |
#invalid_access_token ⇒ Object
165 166 167 |
# File 'app/models/concerns/rails_auth_user.rb', line 165 def invalid_access_token self.access_tokens.delete_all end |
#join(params = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'app/models/concerns/rails_auth_user.rb', line 101 def join(params = {}) self.assign_attributes params.slice( :name, :email, :mobile, :password, :password_confirmation ) save end |
#mobile_token ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/concerns/rails_auth_user.rb', line 79 def mobile_token if super super else VerifyToken.transaction do self.mobile_tokens.delete_all create_mobile_token end end end |
#oauth_providers ⇒ Object
161 162 163 |
# File 'app/models/concerns/rails_auth_user.rb', line 161 def oauth_providers OauthUser.(:provider).values.map(&:to_s) - oauth_users.pluck(:provider).compact end |
#reset_token ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/concerns/rails_auth_user.rb', line 57 def reset_token if super super else VerifyToken.transaction do self.reset_tokens.delete_all create_reset_token end end end |
#unlock_token ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/concerns/rails_auth_user.rb', line 68 def unlock_token if super super else VerifyToken.transaction do self.unlock_tokens.delete_all create_unlock_token end end end |
#verified_status? ⇒ Boolean
139 140 141 142 143 144 145 146 |
# File 'app/models/concerns/rails_auth_user.rb', line 139 def verified_status? if self.disabled? errors.add :base, :account_disable true else false end end |