1
votes

Using a module plug, I fetch the current users information from the database and store it in Plug.Conn.assigns as current_user. By inspecting the connection I see the current users details in the assigns but I'm unable to access any details.

For example, I would like to display the current user's name, as in, "you are logged in as @conn.assigns.current_user.name " but Phoenix is showing the current user is nil.

Please view the accompanying screenshot.

enter image description here

There is certainly a current_user object in the Plug.Conn.assigns. I should be able to access the name, "[email protected]". So, why is Phoenix telling me the current_user is nil?


EDIT: Adding the current user plug:

  def call(conn, _params) do
    if conn.assigns[:current_user] do
      conn
    else
      user_id = get_session(conn, :user_id)

      cond do
        user = Repo.get_by(User, auth_id: user_id) ->
          conn
          |> assign(:current_user, user)
          |> assign(:user_signed_in?, true)

        true ->
          conn
          |> assign(:current_user, nil)
          |> assign(:user_signed_in?, false)
      end
    end
  end

So, here I'm using Auth0 for authentication. The user logs in at Auth0 and is redirected back to app with 3 attributes that I track, a unique auth_id, a name, and an avatar. Upon logging in I check if this is a new user by checking if their auth_id is saved in my database, if it is then I know they are a returning user and I assign the current_user object to the user. If they are a new user I add them to my database (where they are given some additional attributes) and assign the current_user object to the user.

Still experiencing this problem where I have a current_user in the connection but I cannot access the user's attributes - they are showing as nil

2
I'm suspecting (might be wrong) the conn variable you are logging is different from the one your function is returning to the view (eex). Are you logging the conn from the view? Can you post the plug module - Badu
I'm logging out from the PageController with IO.inspect conn - Frederick John
Best way to know for sure is log in the eex file like <% IO.inspect(@conn) %>. That’s how you’ll know what value is in current_user from the view’s point of view(no pun). Your plug code looks ok. - Badu
Ok, so the connection in the controller is showing a current user but the connection in the view is showing no current user. This is why the app keeps breaking when a user is logged in but I don't understand why it's not grabbing the values of current_user - Frederick John
Post your controller function code let’s see - Badu

2 Answers

0
votes

You should be able to just use @current_user.name in the template to get the user.

Also, since the value of current_user is set to nil in the plug when nobody is logged in, you can just do !!@current_user instead of @conn.assigns[:user_signed_in?] up above.

0
votes

The assumptions of @Badu lead me to discover the issue. In my controller code I originally passed the current_user from the session to the render function like so:

def index(conn, _params) do
render(conn, "index.html", current_user: get_session(conn, :current_user))
end

As the application grew and I added more pages I had to keep grabbing the current user out of the session so I decided to replace this behavior with a plug, the code of which I posted above. However, I forgot to delete the current_user object I was storing in the connection from the controller. So, while a current_user object did exist in my connection, my view_template was accessing an older deprecated version of current_user, which did not have the same attributes, hence the nil values.