romenigld

romenigld

Hello Guys,
first all I’m loving this book and it’s very helpfull.
I am reading now the version P1.0 of the ebook.
But I was in the part of ‘Log Out’ the session and I notice there they isn’t using a put_flash . And I would like to inform with a message.
So I try to put this:

  def delete(conn, _) do
    conn
    |> RumblWeb.Auth.logout()
    |> put_flash(:info, "Bye, Bye!")
    |> redirect(to: Routes.page_path(conn, :index))
  end

And when I test in the browser, this doesn’t shows the flash message.
Why this occur? It’s why we are dropping the session?
I’m asking just for clarify.

And another question is, why in the plug RumblWeb.Auth.logout don’t uses the clear_session(conn) like this:

def logout(conn) do
    conn
    |≥ clear_session()
    |≥ configure_session(drop: true)
  end

I’m asking because I saw using this in another example and why don’t was used here?
:v:

And in other part of the book:

If you want to keep the session around, you could also delete only the user ID information by calling delete_session(conn, :user_id) .

This part I don’t know why it was confusing and I want to understand better. In which cases this will serve for example? can Anyone explain better?
I’ll appreciate! :kissing_heart:

Showing Posts 1 to 4

peerreynders

peerreynders

template = https://media.pragprog.com/titles/{book_id}/code/{listing_title}
book_id = phoenix14
listing_title = authentication/listings/rumbl/lib/rumbl_web/controllers/session_controller.change2.ex

https://media.pragprog.com/titles/phoenix14/code/authentication/listings/rumbl/lib/rumbl_web/controllers/session_controller.change2.ex

If you use:

  # in rumbl/lib/rumbl_web/controllers/session_controller.ex
  def delete(conn,_) do
    IO.puts("RumblWeb.SessionController.delete: #{inspect(Routes.page_path(conn, :index))}")

    conn
    |> RumblWeb.Auth.logout()
    |> redirect(to: Routes.page_path(conn, :index))
  end

You should see this in the server console

RumblWeb.SessionController.delete: "/"

when you log out.

Phoenix.Controller.redirect/2

Sends redirect response to the given url.

So what is happening is that redirect/2 is instructing the browser to request “/” - the index page prepared by page_controller.ex.

The conn structure used for RumblWeb.PageController.index/2 is a fresh, new value. Whatever you put into the conn before the HTTP redirection has been lost …

https://media.pragprog.com/titles/phoenix14/code/authentication/listings/rumbl/lib/rumbl_web/controllers/auth.change2.ex

  def logout(conn) do
    configure_session(conn, drop: true)
  end

Plug.Conn.configure_session/2

  • :drop - drops the session, a session cookie will not be included in the response

… and in the absence of a session nothing carries over from one request to the next.


don’t uses the clear_session(conn)

Plug.Conn.clear_session/1

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 .

i.e. given that the session is being dropped, clearing it is redundant.


In which cases this will serve for example?

Now change to this:

  # from rumbl/lib/rumbl_web/controllers/auth.ex
  def logout(conn) do
    delete_session(conn, :user_id)
    # configure_session(conn, drop: true)
  end

and this

  # in rumbl/lib/rumbl_web/controllers/session_controller.ex
  def delete(conn,_) do
    conn
    |> RumblWeb.Auth.logout()
    |> put_flash(:info, "Bye, Bye!")
    |> redirect(to: Routes.page_path(conn, :index))
  end

and you will see the flash info on the index page when you log out.

Plug.Conn.delete_session/2 simply deletes the value associated with the :user_id key from the session - but the rest of the session and all the other data in it remains - which is why now the flash info can carry over to the index page.

Ankhers

Ankhers

Minor nitpick, but you can write this as

IO.inspect(Routes.page_path(conn, :index), label: "RumblWeb.SessionController.delete")
peerreynders

peerreynders

IO.inspect/2 returns the value that it inspects - which can be useful in pipelines but here I have no interest in a return value and I want to directly annotate what is being output (though IO.inspect has the :label option).

romenigld

romenigld OP

Nice explanation @peerreynders, thank you very much for reply.
I’m loving the power of the Phoenix Framework.
I’ll expect to be very good using this framework.
And It’s a lot of things to learn… And I expect more books for learn with phoenix using the phoenix live view or react and vuejs.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews