Class: Jobshop::ApplicationController::EmailAuthenticationToken
- Inherits:
-
Object
- Object
- Jobshop::ApplicationController::EmailAuthenticationToken
- Defined in:
- app/controllers/jobshop/application_controller.rb
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #destroy ⇒ Object
- #expired? ⇒ Boolean
- #expires_on ⇒ Object
-
#initialize(email, token) ⇒ EmailAuthenticationToken
constructor
A new instance of EmailAuthenticationToken.
- #secure_compare ⇒ Object
- #user ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(email, token) ⇒ EmailAuthenticationToken
46 47 48 49 |
# File 'app/controllers/jobshop/application_controller.rb', line 46 def initialize(email, token) @email = email @token = token end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
44 45 46 |
# File 'app/controllers/jobshop/application_controller.rb', line 44 def token @token end |
Instance Method Details
#destroy ⇒ Object
60 61 62 63 64 65 |
# File 'app/controllers/jobshop/application_controller.rb', line 60 def destroy user.update({ email_authentication_token: nil, email_authentication_token_sent_at: nil }) end |
#expired? ⇒ Boolean
74 75 76 |
# File 'app/controllers/jobshop/application_controller.rb', line 74 def expired? @expired ||= Time.now >= expires_on end |
#expires_on ⇒ Object
78 79 80 81 |
# File 'app/controllers/jobshop/application_controller.rb', line 78 def expires_on # TODO: Make token expiration configurable in initializers/jobshop.rb. @expires_on ||= user.email_authentication_token_sent_at + 6.hours end |
#secure_compare ⇒ Object
67 68 69 70 71 72 |
# File 'app/controllers/jobshop/application_controller.rb', line 67 def secure_compare # Notice how we use Devise.secure_compare to compare the token in the # database with the token given in the params, mitigating timing # attacks. Devise.secure_compare(user.email_authentication_token, token) end |
#user ⇒ Object
55 56 57 58 |
# File 'app/controllers/jobshop/application_controller.rb', line 55 def user @user ||= Jobshop::User.where(email: @email) .where.not(email_authentication_token_sent_at: nil).first end |
#valid? ⇒ Boolean
51 52 53 |
# File 'app/controllers/jobshop/application_controller.rb', line 51 def valid? user && token && !expired? && secure_compare end |