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?
Asked
Active
Viewed 1,441 times
4
-
Are you saying you have a fixed set of domains that you'd allow emails for? Or are you saying you only want invited users allowed? For the latter, It would be easy enough to create an "invites" table and only allow emails which have been invited to sign up. For the latter, you could use a regex. Either way, you'd likely be overriding the devise controller sign up method. For overriding, see https://stackoverflow.com/questions/3546289/override-devise-registrations-controller. – karns Nov 14 '19 at 01:10
-
Allow only specific domain names to register in Rails App (Devise gem) https://stackoverflow.com/a/47009709/1711149 – maxcobmara Jun 02 '21 at 14:27
2 Answers
0
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
nesiseka
- 1,268
- 8
- 22
-
-
To the controller in which you want to restrict the access. If it's app-wide, then application_controller.rb – nesiseka Aug 19 '15 at 08:42
-
This doesn't look like it works. If the email doesn't have that ending, I don't even want the user to be able to sign up in the first place. – MarcioPorto Aug 19 '15 at 08:45
-
2@MPorto If thats the case, then add a validation for the email in the model. Something like `validates :email, format: { with: /\b[A-Z0-9._%a-z\-]+@xyz\.com\z/, message: "must be a xyz.com account" }` – Pavan Aug 19 '15 at 08:55
0
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
IgnazioC
- 4,554
- 4
- 33
- 46