Skip to content

Commit fe88612

Browse files
committed
Merge pull request #3 from jcgertig/cookie
Cookie
2 parents cffe875 + 81a8d58 commit fe88612

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

lib/proof/proof_actions.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def proof_actions(options={}, &block)
1111
options[:identifier] ||= :email
1212
options[:password] ||= :password
1313
options[:authenticate] ||= :authenticate
14+
options[:set_cookie] ||= false
15+
options[:expire_token] ||= false
1416
options[:block] = nil
1517
if block_given?
1618
options[:block] = block
@@ -27,12 +29,15 @@ def login
2729
identifier = self.class.proof_options[:identifier]
2830
user = proof_class.find_by(identifier => params[identifier])
2931
if user && user.send(self.class.proof_options[:authenticate], params[self.class.proof_options[:password]])
30-
auth_token = Proof::Token.from_data({ user_id: user.id })
32+
auth_token = Proof::Token.from_data({ user_id: user.id }, self.class.proof_options[:expire_token])
3133
json = { auth_token: auth_token }
3234
if !self.class.proof_options[:block].nil?
3335
json = self.class.proof_options[:block].call(user, auth_token)
3436
end
35-
render json: json
37+
if self.class.proof_options[:set_cookie]
38+
cookies[:user] = { value: json, expires: Time.at(auth_token.expiration_date) }
39+
end
40+
render json: json, status: 201
3641
else
3742
render json: { error: "Invalid Credentials." }, status: :unauthorized
3843
end

lib/proof/token.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ def initialize(data, secret_key, algorithm, token)
1717
end
1818

1919

20-
def self.from_data(data, secret_key=Rails.application.secrets.secret_key_base, algorithm='HS256', expiration_date=24.hours.from_now.to_i)
20+
def self.from_data(data, expires=true, secret_key=Rails.application.secrets.secret_key_base, algorithm='HS256', expiration_date=24.hours.from_now.to_i,)
2121
# Must Clone Data Hash to Avoid Side Effects
22-
data_immutable = data.clone.merge({ exp: expiration_date })
22+
data_immutable = data.clone
23+
if expires
24+
data_immutable = data.clone.merge({ exp: expiration_date })
25+
end
2326
token = JWT.encode(data_immutable, secret_key, algorithm)
2427
new(data_immutable, secret_key, algorithm, token)
2528
end

lib/proof/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Proof
2-
VERSION = "1.1.3"
2+
VERSION = "1.1.7"
33
end

0 commit comments

Comments
 (0)