Pow.Plug.authenticate_user :error, what are the errors?

I am having troubles with https://hexdocs.pm/pow/Pow.Plug.html#authenticate_user/2
I have a use case where I need to authenticate a user automatically in by backend and keep him logged in.

I have a map of parameters that looks like

user_params = %{
username: ..
email: ..
password: ..
password_confirmation: ..

And I successfully create a user with Pow.Plug.create_user(conn, user_params)

If I subsequently try to authenticate the same user with the same params

Pow.Plug.authenticate_user(conn, user_params)

I get a {:error, conn} response. Two questions arise:

  1. How do I see a list of errors? What went wrong in the authentication?
  2. Why does the authentication break? I am (programmatically sure) that user_params is the same map passed to create_user before.

I was not able to figure out any of the above from the docs and the examples (https://hexdocs.pm/pow/Pow.Plug.html#authenticate_user/2)

What do your params look like?

Something like this

 15  def get_login_params(user) do
 14     email = user.id <> "-" <> user.name <> "@company.app"
 13     password = user.id <> "-" <> user.name
 10     %{
  9       username: user.name,
  8       email: email,
  7       password: password,
  6       password_confirmation: password
  5     }
  4   end

You can think of this as the default login credentials for a company employee before he goes there and change them when he access the service

Just do IO.inspect conn and you will find them. :slight_smile:

1 Like

You don’t need to authenticate immediately after, as Pow.Plug.create_user/2 already creates a session. To automatically sign in a user (create a session), you can call Pow.Plug.create/2, passing in the user as the second param.

When you receive {:error, conn} from Pow.Plug.authenticate_user/2 it means that the credentials are invalid, either a user doesn’t exist with the provided email or the password is incorrect. It doesn’t reveal any detailed information. I’m uncertain why the same params doesn’t work to sign in the user, and I would need to see more of your config to figure out what’s the issue.