I am using Devise for authentication, and when someone visits a specific URL (with specific params in the URL), I would like to set the 'current_user' to a specific value from the db.
How do I do that ?
Also, will I have to make sure all the validation requirements are satisfied ? E.g., my user model has:
validates_presence_of :username, :email
If I set the current_user, would I have to set both the :username & :email, or can I just do one or the other ?
You could try
@user = User.where(:x => "y").first # Find the user depending on the params
sign_in(@user)
# do work
sign_out(@user)
I don't know if that's the best way to do it, but it will set current_user
.
devise's current_user
helper sets the instance variable @current_user, so doing this:
@current_user = User.first
will also set current_user to the selected user.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With