1

I have a login_controller which handles user logins. It uses single sign on - so the login is seemless to the user and they will be redirected to the page they were trying to access (e.g. for example if they had a link bookmarked).

I want to execute a js snippet for analytics. But I only want to execute it once, and that is after a login via the login_controller.

I tried adding a response header in the login controller:

response.headers['user_logged_in'] = "true"

and I put my JS in the application.html.erb to check for this header - but the header is not there:

if @user && request.headers['user_logged_in'] == "true"

Whats the best way to do this?

1
  • The reason the header is not there is because the client receives the redirect response which is basically just a 3XX status code and a location header and then sends a new request for the URI in the location header. The client will not pass your custom header back. Thats what cookies where designed for. Commented Apr 7, 2020 at 9:48

1 Answer 1

3

I think your situation is perfect to use flash.

Anything you place in the flash will be exposed to the very next action and then cleared out.

So, you can do:

class SessionsController
  def create
    flash[:signed_in] = true
  end
end

Then, in your application.html.erb you can do:

<% if flash[:signed_in] %>
  ...
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.