Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a query param to Devise's login page

I have a Devise customisation issue that I'm struggling with.

We have a query parameter included in some email links that need to be passed to the login URL if the user is not already authenticated when they click on the link. For example, if the email link is:

http://my.host.com/my/path/1234?x=y&foo=bar

I'd like unauthenticated users to be redirected to

http://my.host.com/login/?foo=bar

i.e., one specific query param needs to be passed – I don't care about any others on the login form, but if all the query params have to be passed I could live with that.

I've scoured SO, and the docs & source for both Devise and Warden, but can't find a straightforward way of doing this. Apologies if I'm missing something obvious.

like image 340
Scott Matthewman Avatar asked Mar 19 '14 14:03

Scott Matthewman


1 Answers

In the end, we got the effect we wanted by going down a very slightly different route.

Rather than including the param in the query URL of the redirect to the login, we were able to modify the code concerned to accept a session parameter instead.

Then, in application_controller.rb:

class ApplicationController < ActionController::Base
  before_filter :persist_login_param
  ...

  def persist_login_param
    session[:foo] = params[:foo]
  end
end

And in the sessions controller, we act upon the value of the session parameter, discarding it once done.

Slightly messy, but it works and gets code pushed out to production now, which is the important thing!

like image 68
Scott Matthewman Avatar answered Oct 01 '22 07:10

Scott Matthewman