No route found for GET /users/log_out even though mix phx.routes shows the route

Hello there,
I am new to Elixir and Phoenix and so far loving it. I am trying the phx_gen_auth library and i am getting the following error.

no route found for GET /users/log_out (TeacherWeb.Router)

I ran the mix phx.routes command and can see the /users/log_out route available. Please see the results below.

Available routes

           page_path  GET     /                                     TeacherWeb.PageController :index
           post_path  GET     /posts                                TeacherWeb.PostController :index
           post_path  GET     /posts/:id/show                       TeacherWeb.PostController :show
 live_dashboard_path  GET     /dashboard                            Phoenix.LiveView.Plug :home
 live_dashboard_path  GET     /dashboard/:page                      Phoenix.LiveView.Plug :page
 live_dashboard_path  GET     /dashboard/:node/:page                Phoenix.LiveView.Plug :page

user_registration_path GET /users/register TeacherWeb.UserRegistrationController :new
user_registration_path POST /users/register TeacherWeb.UserRegistrationController :create
user_session_path GET /users/log_in TeacherWeb.UserSessionController :new
user_session_path POST /users/log_in TeacherWeb.UserSessionController :create
user_reset_password_path GET /users/reset_password TeacherWeb.UserResetPasswordController :new
user_reset_password_path POST /users/reset_password TeacherWeb.UserResetPasswordController :create
user_reset_password_path GET /users/reset_password/:token TeacherWeb.UserResetPasswordController :edit
user_reset_password_path PUT /users/reset_password/:token TeacherWeb.UserResetPasswordController :update
feed_path GET /feeds TeacherWeb.FeedController :index
user_settings_path GET /users/settings TeacherWeb.UserSettingsController :edit
user_settings_path PUT /users/settings TeacherWeb.UserSettingsController :update
user_settings_path GET /users/settings/confirm_email/:token TeacherWeb.UserSettingsController :confirm_email
user_session_path DELETE /users/log_out TeacherWeb.UserSessionController :delete
user_confirmation_path GET /users/confirm TeacherWeb.UserConfirmationController :new
user_confirmation_path POST /users/confirm TeacherWeb.UserConfirmationController :create
user_confirmation_path GET /users/confirm/:token TeacherWeb.UserConfirmationController :confirm

I am not sure what I am doing wrong.

Please help.

You don’t have a route to GET /usesr/log_out, you have a route to DELETE /users/log_out, just change your HTTP request from GET to DELETE and it should work.

Or, explicitly add such route.

I find it useful to have GET /logout or similar especially for single page apps or apps that break in general, or just in case something breaks and having GET /logout allows user to just go to this URL and basically nuke their session and start over…

1 Like

Thank you @sezaru. That worked.

delete method is ok and according to standars. The problem probably is that you are not including the app.js located on /assets/js

Make sure you have these lines on your template:

<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/>
    <script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script>

or there’s an erro in that JS file somewhere and it doesn’t install the hooks needed to make delete route work. I usually add GET to logout path just for that very case, people have weird things also like ad blockers being too greedy etc. where they accidentally break themselves JS and then can’t log out of app or there’s a weird condition for that particular user resulting in a JS crashing…

You could also inline the form usually created on demand by JS. No need for the JS then, but still get the proper route called.

1 Like

Or you have non-js users…

from my point of view, non-js users don’t deserve the right of internet navigation, not even my time to think or program special stuff for them, and I’m being polite…

Let me tell you what will happen at some point.

You will mess up. Or someone else will. Maybe a third-party library. You’ll end up with cookies exceeding the limit that your server, or load balancer will handle. Or there will be another issue that is easily fixable by giving the user log out link. Your JS may crash and DELETE /logout won’t work. I mentioned it above in 2021, and it probably happened to me once or twice since.

Even if that’s not your primary way of calling logout, maybe even keep the DELETE route but also do GET one to log users out. It has no downsides to do so and can save you a lot of trouble.

That is a good point, I’ll take it :ok_hand:

1 Like