In my system, when the user visits protected resources, the user is redirected to an OAuth authentication if not signed in. I want to redirect the user back to the original URL (from referer
request header?).
I’m using the Guardian library, found the referer
is missing.
My config:
router.ex
pipeline :ensure_auth do
plug Guardian.Plug.Pipeline,
module: EvercamWechatWeb.Guardian,
error_handler: EvercamWechatWeb.AuthErrorHandler
plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}
plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}
plug Guardian.Plug.EnsureAuthenticated
plug Guardian.Plug.LoadResource
end
defmodule EvercamWechatWeb.AuthErrorHandler do
@behaviour Guardian.Plug.ErrorHandler
alias EvercamWechatWeb.Router.Helpers, as: Routes
@impl true
def auth_error(conn, {_type, _reason}, _opts) do
referer = Plug.Conn.get_req_header(conn, "referer") # there is no referer in req header
Phoenix.Controller.redirect(conn, to: Routes.auth_path(conn, :request, :wechat))
end
end
Did I miss something? Thanks