How to auto-generate passwords in Rails Devise?

后端 未结 4 1198
北海茫月
北海茫月 2021-02-03 20:26

I am trying out how Devise works with one of my projects for user authentication. There is a user requirement that their admin should be able to generate a batch of username and

4条回答
  •  执笔经年
    2021-02-03 21:15

    Use the Devise.friendly_token method:

    password_length = 6
    password = Devise.friendly_token.first(password_length)
    User.create!(:email => 'someone@something.com', :password => password, :password_confirmation => password)
    

    FYI: Devise.friendly_token returns a 20 character token. In the example above, we're chopping off the first password_length characters of the generated token by using the String#first method that Rails provides.

提交回复
热议问题