Skip to content

Commit

Permalink
feat: add logout
Browse files Browse the repository at this point in the history
  • Loading branch information
zvonimirr committed Jun 16, 2024
1 parent cfd3b84 commit 58f70cc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/swipex_web/controllers/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,17 @@ defmodule SwipexWeb.PageController do
|> put_flash(:error, "Invalid name or password.")
|> redirect(to: "/login")
end

def logout(conn, _params) do
case get_session(conn, :user_id) do
nil ->
redirect(conn, to: "/")

_ ->
conn
|> delete_session(:user_id)
|> put_flash(:info, "Logged out successfully!")
|> redirect(to: "/login")
end
end
end
2 changes: 2 additions & 0 deletions lib/swipex_web/live/profile_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ defmodule SwipexWeb.ProfileLive do
</div>
<% end %>
</div>
<hr />
<a href="/logout" class="text-red-500">Logout</a>
</div>
"""
end
Expand Down
1 change: 1 addition & 0 deletions lib/swipex_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule SwipexWeb.Router do
post "/register", PageController, :do_register
get "/login", PageController, :login
post "/login", PageController, :do_login
get "/logout", PageController, :logout

live_session :default, on_mount: [{SwipexWeb.Profile, :current_user}] do
live "/profile", ProfileLive, :index
Expand Down

0 comments on commit 58f70cc

Please sign in to comment.