3

I have followed the RoR tutorial by Michael Hartl, for modeling users, sign in, and sign up. Now I'm in a position where I need to verify that whatever email is given upon login is a real email and not some random string that matches the regex I have on validation.

I know Devise handles that. And I don't want to change the working code I have. How could I use ONLY the email verification feature that Devise has ? (don't want it to do any sign-in, and sessions, and authentication etc for me)

If it's not possible to use Devise this way, how devastating would it be to plug Devise to my Rails user model ?

Thanks !

Myna
  • 569
  • 2
  • 10
  • 24
  • you just need to validate the email address field. don't need a gem for that! – c0deNinja Jul 05 '12 at 04:05
  • What do you mean ? I need to verify/confirm that the email exists. I could give chair@book.com, and it would look like a valid email address to my regex. Any ideas ? – Myna Jul 05 '12 at 04:07
  • it is not very common to do what you are trying to do. read the question too fast. – c0deNinja Jul 05 '12 at 04:29
  • It is not common indeed ... That's why I am asking the question :) – Myna Jul 05 '12 at 17:50
  • Possible duplicate of [Email confirmation in Rails without using any existing authentication gems/plugins](http://stackoverflow.com/questions/3984977/email-confirmation-in-rails-without-using-any-existing-authentication-gems-plugi) – Brad Werth Jan 18 '17 at 20:56

2 Answers2

3

Devise validates emails with regexp (it is very logical do to this using regexp).

The regexp used by devise is: (copied from devise code)

  # Email regex used to validate email formats. It simply asserts that
  # an one (and only one) @ exists in the given string. This is mainly
  # to give user feedback and not to assert the e-mail validity.
  mattr_accessor :email_regexp
  @@email_regexp = /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/

You can change this reg exp in devise initializer if you are using devise.

If you don't need devise, you can implement email validation yourself.

A very good sample is given in official rails guides, Active Record Validations document.

class EmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
      record.errors[attribute] << (options[:message] || "is not an email")
    end
  end
end
Alper Karapınar
  • 2,694
  • 1
  • 25
  • 36
  • This is unfortunately not what I am asking for. Like I said on my post, I already have email validation, one of the first thing I set up in the user model. What I am talking about is email confirmation. cf my comment on the first answer to the question. +1 on looking up the regex used by Devise. – Myna Jul 05 '12 at 17:49
  • Ah very sorry then. And thank you very much for +1. So i think i can talk about my opinion about email confirmation. I use devise in my projects maybe only for its features about email confirmation module. Password encryption, session management etc are much more easier for me than handling confirmation tokens (well, it is not that much difficult but when you do the same thing many times, it gets boring, and confirmation part requires lot of code). I agree with you that there should be a gem only for email confirmation related jobs. – Alper Karapınar Jul 05 '12 at 22:41
  • 2
    Again, not answering your question but, [this answer](http://stackoverflow.com/questions/3984977/email-confirmation-in-rails-without-using-any-existing-authentication-gems-plugi) points that there are a lot of work to do for email confirmation, and strange, there is no library only for this job. – Alper Karapınar Jul 05 '12 at 23:16
  • This is the exactly thing what i want, thanx. – Jigar Bhatt Sep 23 '14 at 05:33
1

What you need is devise confirmable. (It sends a email with a confirmation link.)

This railscast should help: http://railscasts.com/episodes/209-introducing-devise.

To enable it, check this guide or this question.

Community
  • 1
  • 1
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236