Search code examples
elixirphoenix-frameworkguardian

** (UndefinedFunctionError) function Guardian.Plug.authenticated?/1 is undefined or private


I am very new to Elixir and Phoenix, I am trying to authenticate my application using {:comeonin, "~> 4.0"} and {:guardian, "~> 1.0"} and have a helper function that checks if the user is logged in:

defmodule Chatter.ViewHelper do
  def current_user(conn), do: Guardian.Plug.current_resource(conn)
  def logged_in?(conn) do
    Guardian.Plug.authenticated?(conn)
  end
end

But I get this error:

** (UndefinedFunctionError) function Guardian.Plug.authenticated?/1 is undefined or private.

Solution

  • Guardian documentation doesn't properly reference some API calls since the upgrade to v1.0. You need to call these functions from your custom MyApp.Guardian implementation and not from the actual Guardian module(s).

    Assuming you followed the guide to implement MyApp.Guardian, you need to call:

    MyApp.Guardian.Plug.authenticated?(conn)