Class: Jobshop::ApplicationController::EmailAuthenticationToken

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/jobshop/application_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#tokenObject (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

#destroyObject



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_onObject



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_compareObject



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

#userObject



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