How to delete a Phoenix session?

I go through the Phoenix Guide on Sessions . It explains very well how I can bind data to a session using put_session and retrieve the value later using get_session , but it does not show how I can delete a user session.

From the manual:

 defmodule HelloPhoenix.PageController do use Phoenix.Controller def index(conn, _params) do conn = put_session(conn, :message, "new stuff we just set in the session") message = get_session(conn, :message) text conn, message end end 
+6
source share
3 answers

I think you are looking for configure_session :

 Plug.Conn.configure_session(conn, drop: true) 
+6
source

Found in Plug Docs :

clear_session (conn)

Clears the entire session.

This function removes each key from a session, clearing sessions.

Note that even if clear_session / 1 is used, the session is still sent to the client. If the session should be effectively deleted, configure_session / 2 should be used with the: drop option set to true.


You can put something like this in your SessionsController :

 def delete(conn, _) do conn |> clear_session |> redirect to: page_path(conn, :index) end 

and add a route for this in web/router.ex .

+8
source

If you want to delete a specific session, you should use:

 conn |> fetch_session |> delete_session(:session_to_delete) 

Further information here:

https://github.com/elixir-lang/plug/blob/master/lib/plug/session.ex#L114:L115

+1
source

Source: https://habr.com/ru/post/989598/


All Articles