Why won't the work be debugged in this situation?

I am using a gem called omniauth-facebook with which I managed to implement facebook authentication.

It looks great, but it doesn't pass the data of the entire object to the view. It just says nil.

It should show an array of things when using "debug" or "checkout". It shows the contents of the session [: name] somehow.

controller

class SessionsController < ApplicationController
    def create
        @auth = request.env["omniauth.auth"]
        session[:oauth_token] = @auth.credentials.token
        session[:username] = @auth.extra.raw_info.username

        session[:name] = @auth.extra.raw_info.name
        redirect_to bookd_url, :notice => "Signed in!"
    end
end

      

View

<% if signed_in? %>
    <%= @auth.inspect %>
    <%= debug @auth %><br />
    <%= session[:name] %>
<% end %>

      

HTML output

nil
--- 
...
John

      

+3


source to share


1 answer


Your controller action creates a redirect. Once redirected, the process will start from scratch and @auth will no longer be defined. If you make a view at this point, @auth will be null. It's your problem.

You need to think about what you are trying to do here. You are setting a variable @auth

from the authentication credentials in the original request. Then you use that to set some data in a session that records who, for example, is logging. Then, on the next page where the user is logged in, you want to look at @auth. It doesn't make any sense: after you've authenticated the user, all you have to worry about is remembering which user is currently logged in. You don't need to store details of how they logged in, and in fact, you probably shouldn't.

Instead, you should do something like this:



#in ApplicationController, protected section
protected

def current_user
  if @current_user
    return @current_user
  elsif session[:username]
    @current_user = User.find_by_username(session[:username])
    return @current_user
  end
end

      

This will allow you to write current_user

in your controller and view the code to access the authenticated user initially, which is the whole reason for logging in and out.

+2


source







All Articles