Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a Phoenix Session?

I'm going through the Phoenix Guide on Sessions. It explains it very well how I can bind data to a session using put_session and fetch the value later using get_session but it doesn't tell how I can delete a User's session.

From the guide:

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
like image 200
Sheharyar Avatar asked Jun 23 '15 09:06

Sheharyar


2 Answers

Found it in the Plug Docs:

clear_session(conn)

Clears the entire session.

This function removes every key from the session, clearing the session.

Note that, even if clear_session/1 is used, the session is still sent to the client. If the session should be effectively dropped, 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 your web/router.ex.

like image 92
Sheharyar Avatar answered Oct 16 '22 22:10

Sheharyar


I think what you're looking for is configure_session:

Plug.Conn.configure_session(conn, drop: true)
like image 34
Paweł Obrok Avatar answered Oct 17 '22 00:10

Paweł Obrok