Could not find a matching SessionController.create clause to process request

Hello,

I’ve created a SessionController and when I’m trying to log in using wrong credential I get an error:

session_controller.ex

defmodule MdbmsWeb.SessionController do
  use MdbmsWeb, :controller
  import MdbmsWeb.Auth


  def new(conn, _params) do
    render(conn, "new.html")
  end

  def create(conn, %{"session" => %{"email" => email, "password" => password}}) do
    case login_with(conn, email, password, repo: Repo) do
    	{:ok, conn} ->
    		logged_user = Guardian.Plug.current_resource(conn)
    		conn
    		|> put_flash(:info, "logged in!")
    		|> redirect(to: page_path(conn, :index))
    	{:error, _reason, conn} ->
    		conn
    		|> put_flash(:error, "Wrong username/password")
    		|> render("new.html")
    end
  end
end

auth.ex

defmodule MdbmsWeb.Auth do
  import Comeonin.Pbkdf2, only: [checkpw: 2, dummy_checkpw: 0]
	import Plug.Conn

	defp login(conn, user) do
		conn
		|> Guardian.Plug.sign_in(user, :access)
	end

	def login_with(conn, email, pass, opts) do
		repo = Keyword.fetch!(opts, :repo)
		user = repo.get_by(Mdbms.User, email: email)

		cond do
			user && checkpw(pass, user.password) ->
				{:ok, login(conn, user)}
			user ->
				{:error, :unauthorized, conn}
			true ->
				dummy_checkpw()
				{:error, :not_found, conn}
		end
	end

end

I spent hours looking for the mistake and I have no idea what I did wrong and where.

"plain_password" != "password" :slight_smile:

3 Likes

Yes, you’re right… Actually “plain_password” is a virtual field. But I did change in session_controller.ex to “plain_password”:

def create(conn, %{"session" => %{"email" => email, "plain_password" => password}}) do

but then I get another error:

You haven’t aliased your Repo module, though I would recommend doing Repo work in context modules rather than in Controllers anyway. :slight_smile:

2 Likes

Because there is probably not a module Repo available at all in your project, perhaps you have forgotten to alias it?

Also could you please post copy&pasted errors instead of screenshots? I’m often on a mobile and screenshots are hard to read there.

Best way to get a pastable version of the error is to look at the terminal where you run your server.

3 Likes

Thank you so much. Stupid mistake. I did it but in a wrong place.

2 Likes

Thank you for your help and advices connected to terminal which is a better place to find mistakes. Sorry for that, I will remember to share errors using copy&paste instead of screenshots next time.

3 Likes