addict
addict copied to clipboard
Logout and redirect without JavaScript
Hello! Thanks for creating Addict, it's great :)
I'm following the example app, but when it comes to the log out link, it's done with JavaScript (it sends the request and then redirects with JS). I want to do it directly, so that the response from the server is the redirect.
I started with the link on the layout:
<%= link "Log out", to: logout_path(@conn, :logout), method: :post %>
This works, but then all we see is an empty "{}" response on the browser. So, I use the post actions to put a flash and redirect, like this:
# config.exs
config :addict,
...
post_logout: &MyApp.AddictPostActions.post_logout/3
# addict_post_actions.ex
defmodule MyApp.AddictPostActions do
import Phoenix.Controller, only: [put_flash: 3, redirect: 2]
alias MyApp.Router.Helpers
def post_logout(conn, status, model) do
conn
|> put_flash(:ok, "Bye!")
|> redirect(to: Helpers.page_path(conn, :index))
end
end
And this almost works, but I get this error and a "You are being redirected" message is shown in the browser.
[error] #PID<0.384.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /logout
** (exit) an exception was raised:
** (Plug.Conn.AlreadySentError) the response was already sent
(plug) lib/plug/conn.ex:458: Plug.Conn.resp/3
(plug) lib/plug/conn.ex:445: Plug.Conn.send_resp/3
lib/addict/controller.ex:1: Addict.AddictController.action/2
lib/addict/controller.ex:1: Addict.AddictController.phoenix_controller_pipeline/2
(rocket) lib/phoenix/router.ex:261: MyApp.Router.dispatch/2
(rocket) web/router.ex:1: MyApp.Router.do_call/2
(rocket) lib/rocket/endpoint.ex:1: MyApp.Endpoint.phoenix_pipeline/1
(rocket) lib/plug/debugger.ex:93: MyApp.Endpoint."call (overridable 3)"/2
(rocket) lib/phoenix/endpoint/render_errors.ex:34: MyApp.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
How can this be done?
Ah well, from what I see, Addict is heavily oriented to JavaScript; the rest of the actions (register, login) won't work either without JavaScript, the redirections are done there as well.
OK, then I'll just close this issue, I think it will be wiser to assume this :)
@jaimeiniesta thanks for the detailed log 👍 Although I look at addict as an API driven user management library, I think it makes sense for whoever is using the action for post_logout
- or any other post_
- to be able to apply changes to conn
. I'll take a stab at this soon :)
@trenpixster is this likely to be looked at any time soon?
@johnhamelink yep, will be hopefully tackling this in the next week along with #85 and a few others.
Great, unfortunately I won't be able to try it this time around - I ended up using OpenMaize in the meantime.