Class: ApiUserAuth::AuthUser

Inherits:
ApplicationRecord show all
Includes:
AuthUserHelper
Defined in:
app/models/api_user_auth/auth_user.rb

Overview

Base user auth model

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#is_newObject

Returns the value of attribute is_new.



11
12
13
# File 'app/models/api_user_auth/auth_user.rb', line 11

def is_new
  @is_new
end

Class Method Details

.create_by_provider(params) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/api_user_auth/auth_user.rb', line 76

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

  provider_token = ProviderToken.find_by_data(provider_data)

  if provider_token.blank?
    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
  else
    provider_token.auth_user
  end
end

.find_fy_token(token) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'app/models/api_user_auth/auth_user.rb', line 143

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



64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/api_user_auth/auth_user.rb', line 64

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/api_user_auth/auth_user.rb', line 15

def self.(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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/api_user_auth/auth_user.rb', line 35

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

#add_provider_login(params) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/api_user_auth/auth_user.rb', line 112

def (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

  ProviderToken.create_by_data(provider_data, self)

  # 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

#generate_tokenObject



158
159
160
# File 'app/models/api_user_auth/auth_user.rb', line 158

def generate_token
  auth_tokens << SecureRandom.uuid
end

#logout(token) ⇒ Object



175
176
177
178
# File 'app/models/api_user_auth/auth_user.rb', line 175

def logout(token)
  auth_tokens.delete(token)
  save
end

#password=(passwd) ⇒ Object



162
163
164
# File 'app/models/api_user_auth/auth_user.rb', line 162

def password=(passwd)
  self.encrypted_password = hexdigest(passwd) if passwd.present?
end

#send_reset_passwordObject



170
171
172
173
# File 'app/models/api_user_auth/auth_user.rb', line 170

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_jsonObject



154
155
156
# File 'app/models/api_user_auth/auth_user.rb', line 154

def to_json
  { id: id, email: email, auth_token: auth_tokens.last, is_new: is_new }
end

#update_password(password) ⇒ Object



58
59
60
61
62
# File 'app/models/api_user_auth/auth_user.rb', line 58

def update_password(password)
  self.password = password
  generate_token
  save
end

#valid_password?(passwd) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'app/models/api_user_auth/auth_user.rb', line 166

def valid_password?(passwd)
  encrypted_password == hexdigest(passwd)
end