Rails + Devise: redirect to a specific url upon public page Facebook Login
I have a public assessable page
"http://www.example.com/gift/5"
There is a Facebook connect button on it.
I want the user to click the Facebook connect button and then be redirected to
"http://www.example.com/gift/5/coupon"
开发者_StackOverflow
Currently Devise/Omniauth seems to lose the session as it's authenticating the user. I've tried sending an ajax request before I do a FB.api
FB.login(function(response) {
if (response.session) {
$.ajax({
url : '/ajax/facebook/url',
type : 'post',
data : {push_to: "/gift/5/coupon"},
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
}
});
window.location = "https://graph.facebook.com/oauth/authorize?client_id=..........";
} else {
}
});
Ajax request hits
class AjaxController < ApplicationController
def ajax_facebook_url_redirect
session[:"fb_redirect_to"] = params[:push_to]
end
But I can't find the variable in either
application_controller.rb
def after_sign_in_path_for(resource)
print session[:"fb_redirect_to"]
(session[:"user.return_to"].nil?) ? "/" : session[:"user.return_to"].to_s
end
Any advice on sending the user to a specific url from a public page?
There is a new section recently added to the OmniAuth Overview docs on the Devise website that addresses this issue: https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview
Here is the gist:
def authenticate_user!
if !current_user
# This should work, but session is lost. See https://github.com/plataformatec/devise/issues/1357
# session[:return_to] = request.fullpath
redirect_to user_omniauth_authorize_path(:google_apps, :origin => request.fullpath)
end
end
def after_sign_in_path_for(resource)
# This should work, but session is lost. See https://github.com/plataformatec/devise/issues/1357
# return_to = session[:return_to]
# session[:return_to] = nil
return_to = request.env['omniauth.origin']
stored_location_for(resource) || return_to || root_path
end
精彩评论