Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout and redirect without JavaScript #86

Open
jaimeiniesta opened this issue May 26, 2016 · 5 comments
Open

Logout and redirect without JavaScript #86

jaimeiniesta opened this issue May 26, 2016 · 5 comments
Labels

Comments

@jaimeiniesta
Copy link

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?

@jaimeiniesta
Copy link
Author

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 :)

@trenpixster
Copy link
Owner

@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 trenpixster reopened this May 27, 2016
@johnhamelink
Copy link

@trenpixster is this likely to be looked at any time soon?

@trenpixster
Copy link
Owner

@johnhamelink yep, will be hopefully tackling this in the next week along with #85 and a few others.

@johnhamelink
Copy link

Great, unfortunately I won't be able to try it this time around - I ended up using OpenMaize in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants