From a14007743fe9cf63bdba902eddac15ffee387cef Mon Sep 17 00:00:00 2001 From: farhatahmad <35435341+farhatahmad@users.noreply.github.com> Date: Thu, 11 Apr 2019 12:45:43 -0400 Subject: [PATCH] Users are redirected to the url they clicked login/signup from (#446) --- app/assets/javascripts/header.js | 5 ++ app/helpers/sessions_helper.rb | 12 ++++- app/views/shared/_header.html.erb | 8 +-- spec/controllers/sessions_controller_spec.rb | 51 ++++++++++++++++++++ 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/header.js b/app/assets/javascripts/header.js index 303590fa..1fbb9fc5 100644 --- a/app/assets/javascripts/header.js +++ b/app/assets/javascripts/header.js @@ -15,6 +15,11 @@ // with BigBlueButton; if not, see . $(document).on('turbolinks:load', function(){ + // Stores the current url when the user clicks the sign in button + $(".sign-in-button").click(function(){ + document.cookie ="return_to=" + window.location.href + }) + // Checks to see if the user provided an image url and displays it if they did $("#user-image") .on("load", function() { diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index ba2d26a7..702c5d60 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -32,7 +32,17 @@ module SessionsHelper # If email verification is disabled, or the user has verified, go to their room def check_email_verified(user) if user.activated? - redirect_to user.main_room + # Get the url to redirect the user to + url = if cookies[:return_to] && cookies[:return_to] != root_url + cookies[:return_to] + else + user.main_room + end + + # Delete the cookie if it exists + cookies.delete :return_to if cookies[:return_to] + + redirect_to url else redirect_to resend_path end diff --git a/app/views/shared/_header.html.erb b/app/views/shared/_header.html.erb index e0ed6a7c..2aac493e 100755 --- a/app/views/shared/_header.html.erb +++ b/app/views/shared/_header.html.erb @@ -59,15 +59,15 @@ <% else %> <% allow_greenlight_accounts = allow_greenlight_accounts? %> <% if Rails.configuration.omniauth_ldap %> - <%= link_to t("login"), omniauth_login_url(:ldap), :class => "btn btn-pill btn-outline-primary mx-2" %> + <%= link_to t("login"), omniauth_login_url(:ldap), :class => "btn btn-pill btn-outline-primary mx-2 sign-in-button" %> <% elsif allow_greenlight_accounts %> - <%= link_to t("login"), "#loginModal", :class => "btn btn-pill btn-outline-primary mx-2", "data-toggle": "modal" %> + <%= link_to t("login"), "#loginModal", :class => "btn btn-pill btn-outline-primary mx-2 sign-in-button", "data-toggle": "modal" %> <% else %> - <%= link_to t("login"), omniauth_login_url(:bn_launcher), :class => "btn btn-pill btn-outline-primary mx-2" %> + <%= link_to t("login"), omniauth_login_url(:bn_launcher), :class => "btn btn-pill btn-outline-primary mx-2 sign-in-button" %> <% end %> <% if allow_user_signup? && allow_greenlight_accounts %> - <%= link_to t("signup.title"), signup_path, :class => "btn btn-pill btn-outline-primary mx-2" %> + <%= link_to t("signup.title"), signup_path, :class => "btn btn-pill btn-outline-primary mx-2 sign-in-button" %> <% end %> <%= render "shared/modals/login_modal" %> diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index e9e970c6..b23491c0 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -89,6 +89,57 @@ describe SessionsController, type: :controller do expect(@request.session[:user_id]).to be_nil expect(response).to redirect_to(account_activation_path(email: @user3.email)) end + + it "redirects the user to the page they clicked sign in from" do + user = create(:user, provider: "greenlight", + password: "example", password_confirmation: 'example') + + url = Faker::Internet.domain_name + + @request.cookies[:return_to] = url + + post :create, params: { + session: { + email: user.email, + password: 'example', + }, + } + + expect(@request.session[:user_id]).to eql(user.id) + expect(response).to redirect_to(url) + end + + it "redirects the user to their home room if they clicked the sign in button from root" do + user = create(:user, provider: "greenlight", + password: "example", password_confirmation: 'example') + + @request.cookies[:return_to] = root_url + + post :create, params: { + session: { + email: user.email, + password: 'example', + }, + } + + expect(@request.session[:user_id]).to eql(user.id) + expect(response).to redirect_to(user.main_room) + end + + it "redirects the user to their home room if return_to cookie doesn't exist" do + user = create(:user, provider: "greenlight", + password: "example", password_confirmation: 'example') + + post :create, params: { + session: { + email: user.email, + password: 'example', + }, + } + + expect(@request.session[:user_id]).to eql(user.id) + expect(response).to redirect_to(user.main_room) + end end describe "GET/POST #omniauth" do