Rails a href in devise translation .yml - ruby-on-rails

In my Rails 5 app I want to display custom error message if the user enters the password incorrectly for a custom strategy method. What I did is:
devise.en.yml
en:
devise:
failure:
custom_auth: "Email is already registered. \nPlease login <a href='https://www.some_page.com'>Some Page</a> or create an account with a different email address.
But it won't worked, the same as:
custom_auth: "Email is already registered. \nPlease login [Some Page](https://www.some_page.com/login) or create an account with a different email address.

Most likely you need to use html_safe, as in I18n.t("devise.failure.custom_auth").html_safe - otherwise the translation will be escaped, and the link will not work.

Related

Devise - password update via recoverable email causes email has already been taken

Devise version 4.3.0
Rails 4.2.8
I am having trouble letting a user update their password after they are sent a password reset email.
Steps to reproduce
When a user clicks Forgot Your Password from the sign in page.
They enter in their email on the forgot password page
They are then sent a password reset email
In the email they click the link
They are taken to the Change your password page
They enter in an updated password and password confirmation
An error is shown that the email is already taken
The email is already taken because they have an account already, I would like to update their password not create a new account.
I do not have any devise controllers that I have overridden.
After checking the user logs when I make the request it is a post request not a put. Even though the form says method: put
This is how you are trying making your form send PUT request: html: {method: :put}.
html option adds optional HTML attributes for the form tag, so what is happening here is that you are simply adding method='PUT' attribute to the form.
However, most browsers don't support methods other than "GET" and "POST" when it comes to submitting forms.
To make this work you need some Rails magic. Rails magic will work if you pass method option another way, not inside html option:
form_for(resource, as: ..., url: ..., method: :put )
http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark

How to edit devise varification message and remove "email" text from it

We are using devise gem on ruby on rails for user account. If anyone click on our verification link 3 days later, we can see this message -
Email needs to be confirmed within 3 days, please request a new one
we are seeing like this cause on the config/locales/devise.en.yml there is -
confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one"
But I have changed the confirmation_period_expired variable like this -
confirmation_period_expired: " hello world"
but i can see the verification message like this -
Email hello world
Hope you understand the problem. by default "email" text is comming. I want to remove this.
I can see on the devise gem, this file is responsible for automatic adding "email" text.
Now just say me how can I remove the by default "email" text from the devise varification message?
To solve this problem on devise helper you will see a method resource.errors.full_messages
you have to rename it resource.errors.messages

How to override the Devise Passwords Controller?

How to override the Devise Passwords Controller:
https://github.com/plataformatec/devise/blob/master/app/controllers/devise/passwords_controller.rb
I want to do the following:
downcase all emails submitted for password reset. All the emails in the DB as lowercase. If a user tried to reset a valid email with any characters uppercase, the reset fails to find a user. And devise doesn't even give a error message saying no user found
If no user is found in def create, I want to add a flash that says, no user found, did you enter the right email?
How can I accomplish the 2 items above? I believe that required overriding the devise password controller. How do I do that? Or if you have a better solution that's even cleaner, I would like to hear it.
Thanks
the devise initializer has a option to make any field case insensitive:
config.case_insensitive_keys = [ :email ]
If I remember correctly it was added in the newer version and if you don't see some related comments in you initializer then you should upgrade your devise gem using bundle upgrade devise. I am using version 1.3.3.
And this version also shows an error "Email not found" if an invalid email is entered.
If you not getting the error message add <%= devise_error_messages! %> to your view. You can customize the error messages by editing config/locales/devise.en.yml

How can I use Devise Authentication to validate that a sign up email address is from certain domain?

I would like to make sure the only people with email address of a certain domain can signup for a site that is using Devise.
For instance if people sign-up with the email joe#mysite.com, they should get a confirmation email but if the sign up with joe#yoursite.com, they should get a error message.
Uncommenting this line in config/initializers/devise.rb
# Regex to use to validate the email address
# config.email_regexp = /^([\w\.%\+\-]+)#([\w\-]+\.)+([\w]{2,})$/i
and changing it to use the domain I wanted to limit to:
config.email_regexp = /\A([\w\.%\+\-]+)#mysite\.com\z/i
did the trick.

Using custom authlogic error messages

I am using the authlogic gem for user validation on one of my sites. All is going well, but I am wondering if it's possible to change the error message that gets returned when the user types in an invalid email address.
Thanks!
authlogic has a special setting for this purpose:
class UserSession < Authlogic::Session::Base
generalize_credentials_error_messages true
end
The error message will be the same: "Email/Password combination is not valid", whether the password or email is bad. You can change the text of the message specifying a string instead of true:
generalize_credentials_error_messages "Try again"
You can override the settings for email validation with validates_format_of_email_field_options. However, if you only want to change the message you can merge options with merge_validates_format_of_email_field_options so that only the options you specify are overridden. You specify settings in your User controller like so:
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.merge_validates_format_of_email_field_options :message => 'My message'
end
end
You can also change the settings for length and uniqueness validations. There are also a lot more other settings, take a look at the documentation, in the ::Config sections of each module you can find settings and their default values and how to override them.
Alternatively you can use localization and set error_messages.email_invalid (that's what the plugin looks for before setting it to the default English sentence, also useful if you are building an international application).
Override Authlogic error messages by changing in en.yml file
It works for me.
en:
authlogic:
error_messages:
login_blank: "Please enter the email address."
login_not_found: "This email address is already in the system. Please choose a different email address."
login_invalid: "Please enter a valid email address."

Resources