Method: Authify::API::Models::User#add_verification_token!

Defined in:
lib/authify/api/models/user.rb

#add_verification_token!(opts = {}) ⇒ Object

Both sets a token in the DB and emails it to the user



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/authify/api/models/user.rb', line 53

def add_verification_token!(opts = {})
  return false if verified?
  token = peppered_sha512(rand(999).to_s)[0...16]
  valid_time  = Time.now + (15 * 60)
  valid_until = valid_time.to_i
  self.verification_token = "#{token}:#{valid_until}"

  subdata = { token: token, valid_until: valid_time }

  email_opts = {
    body: if opts.key?(:body)
            dehandlebar(opts[:body], subdata)
          else
            "Your verification token is: #{token}"
          end
  }

  email_opts[:html_body] = dehandlebar(opts[:html_body], subdata) if opts.key?(:html_body)
  subject = if opts.key?(:subject)
              dehandlebar(opts[:subject], subdata)
            else
              'Authify Verification Email'
            end

  Resque.enqueue Authify::Core::Jobs::Email, email, subject, email_opts
end