How do I customize the length of the token generated in :token_authenticatable in devise - Rails 3?
My token generated seems to be about 20 characters, how do I change the length to something else ?
I checked the devise.rb
file and tried both:
config.token_authentication_key = :access_key
config.token_authenticatable.length = 40
produced this error:
config/initializers/devise.rb:110:in `block in <top (required)>': undefined method `token_authenticatable' for Devise:Module (NoMethodError)
and
config.token_authentication_key = :access_key
config.token_authentication_key.length = 40
produced this error:
/config/initializers/devise.rb:110:in `block in <top (required)>': undefined method `length=' for :access_key:Symbol (NoMethodError)
and both gave me errors when I tried to run the console.
开发者_JAVA技巧Is there anyway to do this ?
Devise does not provide a functionality to set the length of this token. You would have to override the generate_token
method on your model to change the outcome of this token.
I achieved changing the reset_token_password
length overriding the friendly_token
method. For example:
module Devise
def self.friendly_token(_length = 20)
SecureRandom.urlsafe_base64(5).tr('lIO0', 'sxyz')
end
end
(Remember that the tokens are hashed in the db so even though you successfully set a shorter code if you do user.reset_password_token
you will see a long code which is the hashed token, not the real one.)
精彩评论