Allow only specific emails to register in Rails app (Devise)

拥有回忆 提交于 2020-02-03 08:27:11

问题


I am using Devise to authenticate and register users in my Rails app. However, I only want users who have an email with a specific ending to be able to sign up and access it (let's say @xyz.com). What do I need to do to reflect that?


回答1:


If you want to restrict users access after the registration use before_filter:

before_filter :verify_email

private

def verify_email
  (redirect_to(root_path) unless current_user.email.include?('@xyz.com')
end

Edit: if you want to restrict registration you could just do a form validation to check whether the email matches your pattern




回答2:


Devise offers a way to validate email addresses. Take a look to the documentation:

http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Validatable



来源:https://stackoverflow.com/questions/32090205/allow-only-specific-emails-to-register-in-rails-app-devise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!