Frederick John
Frederick John

Reputation: 527

Phoenix Framework: current_user not available in view despite successfully fetching from database

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

Upvotes: 1

Views: 2314

Answers (2)

Frederick John
Frederick John

Reputation: 527

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.

Upvotes: 0

Mark Wilbur
Mark Wilbur

Reputation: 2915

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.

Upvotes: 0

Related Questions