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