(exit) an exception was raised when I redirect somewhere in plug

Hello, when I want to redirect somewhere in my custom plug like blow code I have the (exit) an exception was raised error:

my code:

  import Plug.Conn
  import Phoenix.Controller

  def init(default), do: default

  def call(conn, default) do
    ip_os = TrangellHtmlSite.ip_os(conn)
    if get_session(conn, :token) != nil  do
      with {:ok, %HTTPoison.Response{status_code: 200, body: _user_info}} <- TrangellHtmlSite.User.verify_token_sender(get_session(conn, :token), ip_os.ip, ip_os.os) do
        # assign(conn, :user_in_doors_token_plug_logined, default)
        conn
        |> redirect( to: "/user-profile")
      else
        _n ->
          conn
          |> put_flash(:error, "لطفا دوباره وارد وب سایت شوید")
          |> fetch_session
          |> delete_session(:token)
          |> redirect( to: "/")
      end
    else
      assign(conn, :user_in_doors_token_plug, default)
    end
  end

it should be noted my code works for me but I have that error and I think this is little slowly than my other pages. how can I fix this ?

oh I should add |> redirect( to: "/") |> halt to fix it, I’m sorry about it

2 Likes

It looks like you need to halt the conn pipeline after you redirect, for example |> halt() in the two redirects in your snippet.

4 Likes