Cant get Pow current user in controller

I am trying to get the current logged in user id in controller as i am using pow authentication and plugs for authentication this same method is working on front-end view but getting errors when i use in controller

def create_individual(attrs \ %{}) do
user = Pow.Plug.current_user(@conn)
attrs = Map.put(attrs, “user_id”, user.id)
%Individual{}
|> Individual.changeset(attrs)
|> Repo.insert()
end

Error: no function clause matching in Pow.Plug.fetch_config/1

You should pass in the conn to the method, seems like you are using a module attribute.

It should look like this:

def create_individual(conn, attrs \\ %{}) do
  user = Pow.Plug.current_user(conn)
  attrs = Map.put(attrs, "user_id", user.id)

  %Individual{}
  |> Individual.changeset(attrs)
  |> Repo.insert()
end
1 Like