Changing Devise sign-in parameters before authentication

520 views Asked by At

I am in a situation where I need to edit the credentials that users input on the login page before calling Devise's authenticate_user! method. In my UserSessions controller, I just override the login method and edit the sign-in parameters accordingly:

def login
  params[:user][:email] = fix_email # Some method that returns the correct email address, which may be different from the initially entered one
  authenticate_user!
  # ...
end

The authentication fails here if the email ends up being changed as it seems that Devise does not actually use the params for authentication. However, after studying both Devise's and Warden's source, I still haven't been able to figure out where exactly I need to write the correct email to in order for Devise to actually authenticate with it. Can anybody clear this up for me?

1

There are 1 answers

1
Damilare Olusakin On

Hey – Interesting problem. This looks like a good job for the before_filter methods. You should be able to modify parameters before strong parameters come into play

before_filter :modify_email, only: %i[login]

def login
  authenticate!
end


....


private
def modify_email
  params[:user][:email] = fix_email
end

Ideally, this should solve the issue for you. Kindly let me know!