simply_auth 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3eee7f12215a55da586805281fcef95295c81f71
4
- data.tar.gz: 0f116bbfa9e6b7e473b4f07c81619d7ecf668f2d
3
+ metadata.gz: a571fc9014ad945181c6bdf7be79bd56b3f9b340
4
+ data.tar.gz: 1176b358d6a9c819c7f8398f09757048c34c4688
5
5
  SHA512:
6
- metadata.gz: bf9a8642239c96b488d94a83654682580584f615ec6a0dafb7f0192b9d95176e2a3041a62fdeeb92e2c03a9ef0cd0c1d69b4a79cbdc9948fddb218f2e2818b75
7
- data.tar.gz: a1fcdd2107a340214154e9faca3e59e3dc22fcc7966dd64949896772a4508e1b91af32172a098f1414f3e37cebf6dc05726a270f9ad3461639b0535d3208b2b2
6
+ metadata.gz: 97ebeecba79b67a5bc90991884a9acaf2dab4860c8471e727fa452c002046c1778b106aa0985d5aa56a514f653adf50fff5230781b1561e4e02a29f51e3dfc1c
7
+ data.tar.gz: a76810f18f67fa8a72e65dc1ac78451b1045f6b57986b42d3ffb7bfc7c3ea11a37585444265d373e99822a848e52639f1ed5992db2266754047c862c673860f0
@@ -4,19 +4,17 @@ module SimplyAuth
4
4
  @user = SimplyAuth::User.new
5
5
  end
6
6
 
7
+ def after_registration(user); end
8
+
7
9
  def create
8
10
  @user = SimplyAuth::User.new(user_params)
9
11
  if @user.save
10
- # account_name = @user.data.company_name
11
- # account_name = @user.name if account_name.blank?
12
- # account = Account.create(name: account_name)
13
- # @user.data.account_id = account.id
14
- # @user.save
12
+ after_registration(@user)
15
13
 
16
14
  @session = SimplyAuth::Session.new(email: user_params[:email], password: user_params[:password])
17
15
  @session.save
18
16
  session[:simply_auth_session_id] = @session.id
19
- redirect_to :root
17
+ redirect_to "/"
20
18
  else
21
19
  render :new
22
20
  end
@@ -25,7 +23,7 @@ module SimplyAuth
25
23
  private
26
24
 
27
25
  def user_params
28
- params.require(:simply_auth_user).permit(:name, :email, :password, data: params[:simply_auth_user][:data].keys)
26
+ params.require(:user).permit(:name, :email, :password, data: params[:user][:data].try(:keys))
29
27
  end
30
28
  end
31
29
  end
@@ -8,7 +8,7 @@ module SimplyAuth
8
8
  @session = SimplyAuth::Session.new(session_params)
9
9
  if @session.save
10
10
  session[:simply_auth_session_id] = @session.id
11
- redirect_to :root
11
+ redirect_to main_app.root_path
12
12
  else
13
13
  render :new
14
14
  end
@@ -17,13 +17,13 @@ module SimplyAuth
17
17
  def destroy
18
18
  current_session.destroy
19
19
  session.destroy
20
- redirect_to :root
20
+ redirect_to main_app.root_path
21
21
  end
22
22
 
23
23
  private
24
24
 
25
25
  def session_params
26
- params.require(:simply_auth_session).permit(:email, :password)
26
+ params.require(:session).permit(:email, :password)
27
27
  end
28
28
  end
29
29
  end
@@ -0,0 +1,28 @@
1
+ module SimplyAuth
2
+ class ApiKey < Model
3
+ attr_accessor :name, :id, :secret
4
+
5
+ def attributes
6
+ super.merge(name: name, id: id, secret: secret)
7
+ end
8
+
9
+ def self.all
10
+ response = RestClient.get(
11
+ "https://api.simplyauth.com#{collection_path}",
12
+ accept: :json
13
+ )
14
+ body = JSON.parse(response.body)
15
+ body.map do |data|
16
+ data = data.deep_transform_keys { |key| key.to_s.underscore }
17
+ new(data)
18
+ end
19
+ end
20
+
21
+ def destroy
22
+ RestClient.delete(
23
+ "https://api.simplyauth.com#{instance_path}",
24
+ accept: :json
25
+ )
26
+ end
27
+ end
28
+ end
@@ -5,6 +5,19 @@ module SimplyAuth
5
5
  include ActiveModel::Conversion
6
6
  attr_accessor :id
7
7
 
8
+ def initialize(*args)
9
+ super(*args)
10
+ @persisted = false
11
+ end
12
+
13
+ def persisted?
14
+ @persisted
15
+ end
16
+
17
+ def persisted=(val)
18
+ @persisted = val
19
+ end
20
+
8
21
  def data
9
22
  @data ||= OpenStruct.new
10
23
  end
@@ -20,24 +33,36 @@ module SimplyAuth
20
33
  end
21
34
 
22
35
  def attributes
23
- {id: id, data: data}
36
+ {id: id, data: data.to_h}
24
37
  end
25
38
 
26
39
  def save
27
40
  if valid?
41
+ Rails.logger.error([
42
+ persisted? ? :patch : :post,
43
+ "https://api.simplyauth.com#{persisted? ? instance_path : collection_path}",
44
+ attributes.to_json,
45
+ {accept: :json,
46
+ content_type: :json}
47
+ ].inspect)
28
48
  response = RestClient.send(
29
49
  persisted? ? :patch : :post,
30
- "https://api.simplyauth.com#{instance_path}",
50
+ "https://api.simplyauth.com#{persisted? ? instance_path : collection_path}",
31
51
  attributes.to_json,
32
52
  accept: :json,
33
53
  content_type: :json
34
54
  )
55
+ Rails.logger.error("3")
35
56
  body = JSON.parse(response.body)
36
57
  body = body.deep_transform_keys { |key| key.to_s.underscore }
58
+ Rails.logger.error("4")
37
59
  self.attributes = body
38
60
  changes_applied
61
+ self.persisted = true
62
+ Rails.logger.error("5")
39
63
  true
40
64
  else
65
+ Rails.logger.error("6")
41
66
  false
42
67
  end
43
68
  end
@@ -57,6 +82,10 @@ module SimplyAuth
57
82
  nil
58
83
  end
59
84
 
85
+ def owner_id
86
+ nil
87
+ end
88
+
60
89
  def self.collection_path(owner_ids = [])
61
90
  ids = [ids].flatten
62
91
  if self.owner_class
@@ -66,16 +95,28 @@ module SimplyAuth
66
95
  end
67
96
  end
68
97
 
98
+ def collection_path
99
+ if owner_id
100
+ self.class.collection_path(owner_id)
101
+ else
102
+ self.class.collection_path
103
+ end
104
+ end
105
+
69
106
  def instance_path
70
- self.class.instance_path(id)
107
+ if owner_id
108
+ self.class.instance_path([owner_id, id])
109
+ else
110
+ self.class.instance_path(id)
111
+ end
71
112
  end
72
113
 
73
114
  def self.instance_path(ids = [])
74
115
  ids = [ids].flatten
75
116
  if self.owner_class
76
- "#{self.owner_class.instance_path(ids.first(ids.length - 1))}/#{path_component}/#{ids.last}"
117
+ "#{self.owner_class.instance_path(ids.first(ids.length - 1))}/#{path_component}/#{ERB::Util.url_encode(ids.last)}"
77
118
  else
78
- "/#{path_component}/#{ids.last}"
119
+ "/#{path_component}/#{ERB::Util.url_encode(ids.last)}"
79
120
  end
80
121
  end
81
122
 
@@ -86,7 +127,15 @@ module SimplyAuth
86
127
  )
87
128
  body = JSON.parse(response.body)
88
129
  body = body.deep_transform_keys { |key| key.to_s.underscore }
89
- new(body)
130
+ new(body).tap do |r|
131
+ r.persisted = true
132
+ end
133
+ end
134
+
135
+ def self.create(attributes={})
136
+ r = new(attributes)
137
+ r.save()
138
+ r
90
139
  end
91
140
  end
92
141
  end
@@ -3,11 +3,15 @@ module SimplyAuth
3
3
  attr_accessor :name, :email, :password, :user_pool_id
4
4
  validates :name, presence: true
5
5
  validates :email, format: /[^@]+@[^@]+/
6
- validates :password, length: 6..72
6
+ validates :password, length: 6..72, if: :password
7
7
  def attributes
8
8
  super.merge(name: name, email: email, password: password)
9
9
  end
10
10
 
11
+ def owner_id
12
+ self.user_pool_id
13
+ end
14
+
11
15
  def self.owner_class_name
12
16
  "UserPool"
13
17
  end
@@ -1,7 +1,7 @@
1
1
  module SimplyAuth
2
2
  module ControllerHelpers
3
3
  def current_session
4
- SimplyAuth::Session.find(session[:simply_auth_session_id]) if session[:simply_auth_session_id]
4
+ Thread.current[:simply_auth_session] ||= find_session
5
5
  end
6
6
 
7
7
  def user_logged_in?
@@ -16,9 +16,17 @@ module SimplyAuth
16
16
  if user_logged_in?
17
17
  true
18
18
  else
19
- redirect_to new_session_path
19
+ redirect_to simply_auth.new_session_path
20
20
  false
21
21
  end
22
22
  end
23
+
24
+ def find_session
25
+ begin
26
+ SimplyAuth::Session.find(session[:simply_auth_session_id]) if session[:simply_auth_session_id]
27
+ rescue RestClient::NotFound
28
+ nil
29
+ end
30
+ end
23
31
  end
24
32
  end
@@ -6,6 +6,14 @@ module SimplyAuth
6
6
  ActiveSupport.on_load(:action_controller) do
7
7
  include SimplyAuth::ControllerHelpers
8
8
  helper_method :user_logged_in?, :current_user
9
+
10
+
11
+ before_action do
12
+ Thread.current[:simply_auth_session] = nil
13
+ end
14
+ after_action do
15
+ Thread.current[:simply_auth_session] = nil
16
+ end
9
17
  end
10
18
  end
11
19
  end
@@ -1,3 +1,3 @@
1
1
  module SimplyAuth
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simply_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Fogle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-01 00:00:00.000000000 Z
11
+ date: 2018-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -57,6 +57,7 @@ files:
57
57
  - app/helpers/simply_auth/application_helper.rb
58
58
  - app/jobs/simply_auth/application_job.rb
59
59
  - app/mailers/simply_auth/application_mailer.rb
60
+ - app/models/simply_auth/api_key.rb
60
61
  - app/models/simply_auth/config.rb
61
62
  - app/models/simply_auth/model.rb
62
63
  - app/models/simply_auth/session.rb