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