How to pass request parameter to Devise login page

I have a problem setting up Devise that I am struggling with.

We have a request parameter included in some email links that need to be passed to the login URL if the user is not yet authenticated when he clicks on the link. For example, if the link is via email:

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

I want unauthenticated users to be redirected to

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

ie, one specific request parameter must be passed - I don't care about the others in the login form, but if all the request parameters have to be passed, I could live with that.

I looked at SO, as well as the documents and source for both Devise and Warden, but cannot find an easy way to do this. Sorry if I miss something obvious.

+4
source share
3 answers

In the end, we got the effect that we need after going through a completely different route.

Instead of including the parameter in the URL of the login redirect request, we were able to change the corresponding code to accept the session parameter.

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 session controller, we act on the value of the session parameter, discarding it after completion.

A little dirty, but it works and now displays the code for production, which is important!

+3
source

For a user who has not passed verification, to transfer certain parameters foo=barfrom http://my.host.com/my/path/1234?x=y&foo=barto your login, follow these steps:

, myaction, myaction

  def myaction
    if member_signed_in?
      redirect_to root_path
    else
      ## If user is unauthenticated direct them to login page with specific params
      redirect_to new_member_session_path(foo: params[:foo]) 
    end
  end

foo , :

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

, Devise:: SessionsController. :

class Users::SessionsController < Devise::SessionsController
  ...
  def new
    @foo = params[:foo]
    super
  end
  ...
end
0

if statement before_action, - .

class ApplicationController < ActionController::Base
  ...
  before_filter :sending_params
  before_action :check_auth, unless: :devise_controller?

  ...

  protected

  def check_auth
    unless user_signed_in?
      redirect_to new_user_session_path(sending_params)
    end
  end

  def sending_params
    params.permit(:first_param, :second_param)
  end
  ...

, ,

<%= params[:first_param] %>

params omniauth,

<%= link_to 'Sign in by provider', omniauth_authorize_path(:user, :provider, first_param: params['first_param']) %>

Where: model name for username: provider - supplier name omniauth (: facebook ie)

In omniauth controller you can use

 def first_param
   request.env['omniauth.params']['first_param'] ||= ''
 end
0
source

Source: https://habr.com/ru/post/1532487/


All Articles