api_user_auth 0.0.14 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14ccd567f2514189726a4dd9d4423a00402ec75083d1f1a3c124ba125dfca47b
4
- data.tar.gz: 8e9cc6376873cd8cd9a5cb52c53561938ce68e65af33b4bd50693c4b3affe3bf
3
+ metadata.gz: e03f4578b5a4c4526a3cf16ef4f5153f2b6ba53ac614f2152140188a75ed9511
4
+ data.tar.gz: fc2c4f7d22cfcf3966ae63b446a1f484c3ca3db6cd7e7f3743e00e8ce402f6c4
5
5
  SHA512:
6
- metadata.gz: a00f3a899a3ee465711d869b2aa99f3b84b9cc1b8be8d890142f596b809972a50e77423e1d2c72a43deee141160741dc0066d4f3199e5cd5a7ee84cb52a0e8b7
7
- data.tar.gz: 92b618e8ac88eb50e651031f2d7dcffbcee7f5afde785e1a1a7998d64469dcb829cf89e55b1c15738ac2c099f2ba2d418cca8f811d44e74e19444da0f4677ec7
6
+ metadata.gz: c7f7735446438b9825b2626bb747c450cab316438f748f5a8b63fc15b755110a6e8c72a00d86b742e2626b967c6867cfde622765e534ece03bf2a535874b157a
7
+ data.tar.gz: 3c32189bf9ecd93bfed90fa13e6eb278e77db613f58c6d7ded4c520a4d06db497790e172c5d66ce6b9cd5cba5a3a1edc14b9450ae3b938572350d682dd6a5321
@@ -50,6 +50,21 @@ module ApiUserAuth
50
50
  render json: auth_user.to_json
51
51
  end
52
52
 
53
+ def add_provider
54
+ if request.headers['Authorization'].blank?
55
+ raise Exceptions::Unauthorized,
56
+ 'Header [Authorization] can not be blank!'
57
+ end
58
+ token = request.headers['Authorization'].sub(/Bearer\s*=?/, '')
59
+ auth_user = AuthUser.find_fy_token(token)
60
+ if auth_user.present?
61
+ auth_user.add_provider_login(params)
62
+ render json: {}, status: 200
63
+ else
64
+ render json: {}, status: 400
65
+ end
66
+ end
67
+
53
68
  private
54
69
 
55
70
  def base_params
@@ -1,27 +1,16 @@
1
1
  module ApiUserAuth
2
2
  # Base user auth model
3
3
  class AuthUser < ApplicationRecord
4
+ include AuthUserHelper
5
+
4
6
  after_create :send_welcome
5
7
 
8
+ has_many :provider_tokens,
9
+ class_name: 'ApiUserAuth::ProviderToken'
10
+
6
11
  attr_accessor :is_new
7
12
 
8
- def self.create_by_params(params)
9
- if params[:email].blank?
10
- raise Exceptions::WrongParams, 'Email can not be blank!'
11
- end
12
- if params[:password].blank?
13
- raise Exceptions::WrongParams, 'Password can not be blank!'
14
- end
15
- auth_user = AuthUser.find_or_initialize_by(email: params[:email])
16
13
 
17
- if auth_user.new_record?
18
- auth_user.is_new = true
19
- auth_user.update_password(params[:password])
20
- else
21
- raise Exceptions::WrongParams, 'User already exists !'
22
- end
23
- auth_user
24
- end
25
14
 
26
15
  def self.login_by_params(params)
27
16
  if params[:email].blank?
@@ -104,14 +93,51 @@ module ApiUserAuth
104
93
  'Wrong provider!'
105
94
  end
106
95
 
107
- auth_user = AuthUser.find_or_initialize_by(email: provider_data[:email])
108
- auth_user.encrypted_password = params[:token]
109
- auth_user.generate_token
110
- auth_user.is_new = auth_user.new_record?
111
- auth_user.user_provider_data = provider_data
112
- auth_user.provider = params[:provider]
113
- auth_user.save
114
- auth_user
96
+ provider_token = ProviderToken.find_by_data(provider_data)
97
+
98
+ if provider_token.blank?
99
+ auth_user = AuthUser.find_or_initialize_by(email: provider_data[:email])
100
+ auth_user.encrypted_password = params[:token]
101
+ auth_user.generate_token
102
+ auth_user.is_new = auth_user.new_record?
103
+ auth_user.user_provider_data = provider_data
104
+ auth_user.provider = params[:provider]
105
+ auth_user.save
106
+ auth_user
107
+ else
108
+ provider_token.auth_user
109
+ end
110
+ end
111
+
112
+ def add_provider_login(params)
113
+ if params[:provider].blank?
114
+ raise Exceptions::WrongParams, 'Provider can not be blank!'
115
+ end
116
+ if params[:token].blank?
117
+ raise Exceptions::WrongParams, 'Token can not be blank!'
118
+ end
119
+
120
+ provider_data = case params[:provider]
121
+ when /facebook/i
122
+ Providers::Facebook.get_user(params[:token])
123
+ when /google/i
124
+ Providers::Google.get_user(params[:token])
125
+ when /instagram/i
126
+ Providers::Instagram.get_user(params[:token])
127
+ else
128
+ raise ::ApiUserAuth::Exceptions::ProviderError,
129
+ 'Wrong provider!'
130
+ end
131
+
132
+ ProviderToken.create_by_data(provider_data, self)
133
+
134
+ # auth_user.encrypted_password = params[:token]
135
+ # auth_user.generate_token
136
+ # auth_user.is_new = auth_user.new_record?
137
+ # auth_user.user_provider_data = provider_data
138
+ # auth_user.provider = params[:provider]
139
+ # auth_user.save
140
+ # auth_user
115
141
  end
116
142
 
117
143
  def self.find_fy_token(token)
@@ -0,0 +1,30 @@
1
+ module ApiUserAuth
2
+ # Model for social provider registration
3
+ class ProviderToken < ApplicationRecord
4
+ belongs_to :auth_user,
5
+ class_name: 'ApiUserAuth::AuthUser'
6
+
7
+ enum provider: %I[facebook google instagram]
8
+
9
+ def self.find_by_data(data)
10
+ find_by(
11
+ provider: data[:provider],
12
+ user_id: data[:id]
13
+ )
14
+ end
15
+
16
+ def self.create_by_data(data, auth_user)
17
+ auth_user ||= create_auth_user(data)
18
+ auth_user.provider_tokens.create(
19
+ provider: data[:provider],
20
+ user_id: data[:id],
21
+ user_data: data
22
+ )
23
+ end
24
+
25
+ def self.create_auth_user(data)
26
+ email = data[:id] + '@' + data[:provider] + '.com'
27
+ AuthUser.create(email: email, password: SecureRandom.uuid)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ module ApiUserAuth
2
+ # Auth user helper
3
+ module AuthUserHelper
4
+ extend ActiveSupport::Concern
5
+
6
+ # Class methods
7
+ module ClassMethods
8
+ def create_by_params(params)
9
+ email_exception if params[:email].blank?
10
+ password_exception if params[:password].blank?
11
+
12
+ auth_user = AuthUser.find_or_initialize_by(email: params[:email])
13
+
14
+ if auth_user.new_record?
15
+ auth_user.is_new = true
16
+ auth_user.update_password(params[:password])
17
+ else
18
+ user_exist_exception
19
+ end
20
+ auth_user
21
+ end
22
+
23
+ private
24
+
25
+ def email_exception
26
+ raise Exceptions::WrongParams, I18n.t('api_user_auth.errors.email')
27
+ end
28
+
29
+ def password_exception
30
+ raise Exceptions::WrongParams, I18n.t('api_user_auth.errors.password')
31
+ end
32
+
33
+ def user_exist_exception
34
+ raise Exceptions::WrongParams, I18n.t('api_user_auth.errors.user_exist')
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,10 @@
1
+ module ApiUserAuth
2
+ # Registration by provider
3
+ module ProvidersHelper
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ en:
2
+ api_user_auth:
3
+ errors:
4
+ email: Email can not be blank!
5
+ password: Password can not be blank!
6
+ user_exist: User already exists !
@@ -0,0 +1,6 @@
1
+ ru:
2
+ api_user_auth:
3
+ errors:
4
+ email: Email can not be blank!
5
+ password: Password can not be blank!
6
+ user_exist: User already exists !
data/config/routes.rb CHANGED
@@ -3,6 +3,7 @@ ApiUserAuth::Engine.routes.draw do
3
3
  collection do
4
4
  post 'login', action: :login
5
5
  post 'provider', action: :provider
6
+ post 'add_provider', action: :add_provider
6
7
  patch 'forgot_password', action: :forgot_password
7
8
  patch 'password', action: :password
8
9
  post 'login', action: :login
@@ -0,0 +1,12 @@
1
+ class CreateApiUserAuthProviderTokens < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :api_user_auth_provider_tokens do |t|
4
+ t.references :auth_user, index: true
5
+ t.integer :provider
6
+ t.string :user_id, null: false
7
+ t.jsonb :user_data, default: {}
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -29,6 +29,7 @@ module ApiUserAuth
29
29
  def user_data
30
30
  {
31
31
  id: @data[:id], name: @data[:name], email: @data[:email],
32
+ provider: 'facebook',
32
33
  img_url: (@data[:picture] || {}).try(:[], :data).try(:[], :url),
33
34
  info: {
34
35
  birthday: @data[:birthday],
@@ -26,7 +26,7 @@ module ApiUserAuth
26
26
  def user_data
27
27
  {
28
28
  id: @data[:id], name: @data[:displayName],
29
- email: @data[:emails].first.try(:[], :value),
29
+ email: @data[:emails].first.try(:[], :value), provider: 'google',
30
30
  img_url: (@data[:image] || {}).try(:[], :url),
31
31
  info: {
32
32
  birthday: @data[:birthday],
@@ -25,6 +25,7 @@ module ApiUserAuth
25
25
  {
26
26
  id: @data[:id], name: @data[:full_name],
27
27
  email: "#{@data[:username]}@instagram.com",
28
+ provider: 'instagram',
28
29
  img_url: @data[:profile_picture],
29
30
  info: {}
30
31
  }
@@ -1,3 +1,3 @@
1
1
  module ApiUserAuth
2
- VERSION = '0.0.14'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
@@ -340,5 +340,7 @@ module ApiUserAuth
340
340
  end
341
341
 
342
342
  end
343
+
344
+ context
343
345
  end
344
346
  end
@@ -10,7 +10,7 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(version: 2018_07_03_111608) do
13
+ ActiveRecord::Schema.define(version: 2018_07_26_140712) do
14
14
 
15
15
  # These are extensions that must be enabled in order to support this database
16
16
  enable_extension "pgcrypto"
@@ -28,4 +28,14 @@ ActiveRecord::Schema.define(version: 2018_07_03_111608) do
28
28
  t.index ["email"], name: "index_api_user_auth_auth_users_on_email", unique: true
29
29
  end
30
30
 
31
+ create_table "api_user_auth_provider_tokens", force: :cascade do |t|
32
+ t.bigint "auth_user_id"
33
+ t.integer "provider"
34
+ t.string "user_id", null: false
35
+ t.jsonb "user_data", default: {}
36
+ t.datetime "created_at", null: false
37
+ t.datetime "updated_at", null: false
38
+ t.index ["auth_user_id"], name: "index_api_user_auth_provider_tokens_on_auth_user_id"
39
+ end
40
+
31
41
  end