How to add users via email only in Devise

I want my users to be able to "subscribe" by providing only an email address, without forcing them to register with a password if they do not want to.

I can add my own validation options to allow this, but I suspect that it will interfere with how the rest of Devise works if I start adding users without passwords or marking them with my own โ€œunregisteredโ€ field.

Currently, the easiest solution seems to put these users in a different table, but there is a little odor in this. Any ideas?

+6
source share
2 answers

I have an application that does something similar - we use Open ID and OAuth exclusively for logging in, and while we save the user's email address (a Google or Facebook account sends it during authentication), they never use it , t / need a password.

This is a callback for what happens when a user signs up via Facebook:

User.create!( :facebook_id => access_token['uid'], :email => data["email"], :name => data["name"], :password => Devise.friendly_token[0,20]) 

Devise.friendly_token[...] just generates a random password with 20 characters for the user, and we use our User model by default, and the application is just fine. Just setting a password in the same way for your users will be my approach as you will never use your password. In addition, if you ever change your mind and want them to be able to log into the system, you can simply change the user password in the database and create a login form, and the development will take care of everyone else.

+20
source

Another Brettโ€™s answer is to override required password? method in the user model.

 def password_required? super && provider.blank? end 

If you store more than one omniauth provider, then something like this should also work.

 def password_required? super && self.omniauth_credentials.empty? end 

Full credit for Ryan Bates' railscast for Devise and Omniauth for this solution: http://railscasts.com/episodes/235-devise-and-omniauth-revised

+2
source

Source: https://habr.com/ru/post/885511/


All Articles