Packages

Firebase Auth helpers for Phoenix/Plug (ID token verification + hosted auth helper files)

Current section

Files

Jump to
fireauth lib fireauth hosted_controller.ex
Raw

lib/fireauth/hosted_controller.ex

defmodule Fireauth.HostedController do
@moduledoc """
Plug-style controller for Firebase hosted auth callback files.
- `"/__/auth/action"` is proxied upstream to Firebase's hosted action
handler, which lets consumers use the same-origin path without having
to implement the email action handler page themselves.
- `"/__/auth/handler"` and `"/__/auth/iframe"` are rendered from
`Fireauth.Snippets` so consumers can reuse the same bootstrap contract.
- All other hosted auth files are served from embedded static assets.
"""
@behaviour Plug
import Plug.Conn
require Logger
alias Fireauth.Plug.FirebaseAuthProxy
alias Fireauth.Plug.FirebaseInitJson
alias Fireauth.Plug.HostedAuthFiles
alias Fireauth.Snippets
@impl true
def init(opts) when is_list(opts), do: opts
def call(%Plug.Conn{request_path: "/__/auth/action"} = conn, opts)
when conn.method in ["GET", "HEAD"] do
Logger.debug("fireauth: hosted_controller proxying action path=/__/auth/action")
FirebaseAuthProxy.call(conn, opts)
end
def call(%Plug.Conn{request_path: "/__/auth/action.js"} = conn, opts)
when conn.method in ["GET", "HEAD"] do
Logger.debug("fireauth: hosted_controller proxying action script path=/__/auth/action.js")
FirebaseAuthProxy.call(conn, opts)
end
@impl true
def call(%Plug.Conn{request_path: "/__/auth/handler"} = conn, _opts)
when conn.method in ["GET", "HEAD"] do
Logger.debug("fireauth: hosted_controller serving snippet handler path=/__/auth/handler")
conn
|> put_resp_content_type("text/html")
|> send_resp(200, Snippets.hosted_auth_handler_document())
|> halt()
end
def call(%Plug.Conn{request_path: "/__/auth/iframe"} = conn, _opts)
when conn.method in ["GET", "HEAD"] do
Logger.debug("fireauth: hosted_controller serving snippet iframe path=/__/auth/iframe")
conn
|> put_resp_content_type("text/html")
|> send_resp(200, Snippets.hosted_auth_iframe_document())
|> halt()
end
def call(%Plug.Conn{request_path: "/__/firebase/init.json"} = conn, opts)
when conn.method in ["GET", "HEAD"] do
Logger.debug("fireauth: hosted_controller serving init.json path=/__/firebase/init.json")
FirebaseInitJson.call(conn, opts)
end
def call(conn, opts) do
Logger.debug("fireauth: hosted_controller serving bundled file path=#{conn.request_path}")
HostedAuthFiles.call(conn, opts)
end
end