ruby on rails - Session variable not accessible across controllers -
i ror beginner , trying implement basic google-openid (using omniauth-openid) authentication in ror app. here code snippets:
sessions_controller.rb:
class sessionscontroller < applicationcontroller def create user = user.from_omniauth(env["omniauth.auth"]) session[:user_id] = user.id session[:my] = 'some' puts "session[:user_id] = ", session[:user_id] puts "session[:my] = ", session[:my] redirect_to root_url end end
routes:
photobucket::application.routes.draw "welcome/index" match 'auth/:provider/callback', to: 'sessions#create', via: [:get, :post] match 'auth/failure', to: redirect('/'), via: [:get, :post] match 'signout', to: 'sessions#destroy', as: 'signout', via: [:get, :post]
application controller:
class applicationcontroller < actioncontroller::base # prevent csrf attacks raising exception. # apis, may want use :null_session instead. protect_from_forgery private def current_user @current_user = user.find(session[:user_id]) end helper_method :current_user end
template:
<div id="user-widget"> <%= current_user %> </div>
here's how log looks when person signs_in website:
started "/auth/google" 127.0.0.1 @ 2014-04-07 00:44:25 +0530 (google) request phase initiated. i, [2014-04-07t00:44:25.150729 #2047] info -- openid: warning: making https request https://www.google.com/accounts/o8/id without verifying server certificate; no ca path specified. i, [2014-04-07t00:44:25.692758 #2047] info -- openid: warning: making https request https://www.google.com/accounts/o8/ud without verifying server certificate; no ca path specified. i, [2014-04-07t00:44:26.192769 #2047] info -- openid: generated checkid_setup request https://www.google.com/accounts/o8/ud using stateless mode. started "/auth/google/callback?_method=post&<some_secrets>" 127.0.0.1 @ 2014-04-07 00:44:30 +0530 (google) callback phase initiated. i, [2014-04-07t00:44:30.588217 #2047] info -- openid: error attempting use stored discovery information: openid::typeurimismatch i, [2014-04-07t00:44:30.588282 #2047] info -- openid: attempting discovery verify endpoint i, [2014-04-07t00:44:30.588308 #2047] info -- openid: performing discovery on https://www.google.com/accounts/o8/id?id=aitoawkbno-m7mk0ih6jwkv1hl2xqtnlce1vdxi i, [2014-04-07t00:44:30.588624 #2047] info -- openid: warning: making https request https://www.google.com/accounts/o8/id?id=aitoawkbno-m7mk0ih6jwkv1hl2xqtnlce1vdxi without verifying server certificate; no ca path specified. i, [2014-04-07t00:44:31.080153 #2047] info -- openid: using 'check_authentication' https://www.google.com/accounts/o8/ud i, [2014-04-07t00:44:31.081077 #2047] info -- openid: warning: making https request https://www.google.com/accounts/o8/ud without verifying server certificate; no ca path specified. processing sessionscontroller#create html parameters: {"provider"=>"google"} can't verify csrf token authenticity user load (0.2ms) select "users".* "users" "users"."provider" = 'google' , "users"."uid" = 'https://www.google.com/accounts/o8/id?id=aitoawkbno-m7mk0ih6jwkv1hl2xqtnlce1vdxi' order "users"."id" asc limit 1 (0.1ms) begin transaction (0.1ms) commit transaction session[:user_id] = 1 session[:my] = redirected http://0.0.0.0:3000/ completed 302 found in 77ms (activerecord: 1.0ms) started "/" 127.0.0.1 @ 2014-04-07 00:44:31 +0530 processing welcomecontroller#index html rendered welcome/index.html.erb within layouts/application (0.1ms) completed 500 internal server error in 5ms activerecord::recordnotfound (couldn't find user without id): app/controllers/application_controller.rb:8:in `current_user' app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__87755315857576517_2161879520' rendered /users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.7ms) rendered /users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms) rendered /users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms) rendered /users/aneeshdogra/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.0ms)
so, session variable populated in session controller not accessible in application controller.
Comments
Post a Comment