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

769 views Asked by At

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.
2

There are 2 answers

1
Sheharyar On BEST ANSWER

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)
2
denis.peplin On

The error shows that you are trying to call the function without arguments:

(UndefinedFunctionError) function Guardian.Plug.authenticated?/0

/0 here means no arguments.