Skip to content

Commit

Permalink
Merge pull request #542 from songjiayang/cache_referrer
Browse files Browse the repository at this point in the history
Skip referrer cache if it's from other site
  • Loading branch information
huacnlee committed Jan 31, 2016
2 parents b2ac9d7 + 5b66404 commit 54f0000
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SessionsController < Devise::SessionsController

def new
super
session['user_return_to'] = request.referrer
cache_referrer
end

def create
Expand All @@ -25,4 +25,11 @@ def valify_captcha!
end
true
end

private
def cache_referrer
if request.referrer && request.referrer.include?(request.domain)
session['user_return_to'] = request.referrer
end
end
end
25 changes: 25 additions & 0 deletions spec/controllers/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails_helper'

describe SessionsController, type: :controller do
describe ':new' do
before { request.env["devise.mapping"] = Devise.mappings[:user] }
it 'should render new tempalte' do
get :new
expect(response).to render_template(:new)
end

it "should store referrer if it's from self site" do
referrer = "#{request.base_url}/account/edit"
request.env["HTTP_REFERER"] = referrer
get :new
session['user_return_to'].should be referrer
end

it "should skip referrer if it's from other site" do
referrer = "http://#{SecureRandom.hex(4)}.com"
request.env["HTTP_REFERER"] = referrer
get :new
session['user_return_to'].should be_nil
end
end
end

0 comments on commit 54f0000

Please sign in to comment.