lib/google_auth_check.ex
defmodule PlugParadoxAuth.GoogleAuthCheck do
import Plug.Conn
def init(options) do
options
end
def call(conn, _opts) do
google_user = conn |> fetch_session |> get_session(:google_user)
cond do
google_user -> conn
conn.request_path == "/auth/google/callback" -> conn
true -> handle_redirect(conn)
end
end
defp handle_redirect(conn) do
conn
|> fetch_session
|> put_session(:google_auth_success_url, conn.request_path)
|> put_resp_header("location", ElixirAuthGoogle.generate_oauth_url(conn))
|> send_resp(302, "text/html")
|> halt
end
end