romenigld

romenigld

Put_flash for log out method don't shows the message

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:

Marked As Solved

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.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement