phoenix_passkeys/lib/phoenix_passkeys_web/controllers/user_registration_controlle...

38 lines
1.0 KiB
Elixir

defmodule PhoenixPasskeysWeb.UserRegistrationController do
use PhoenixPasskeysWeb, :controller
alias PhoenixPasskeys.Accounts
alias PhoenixPasskeys.Accounts.User
alias PhoenixPasskeysWeb.UserAuth
def new(conn, _params) do
changeset = Accounts.change_user_registration(%User{})
render(conn, :new, changeset: changeset)
end
def create(conn, %{
"email" => email,
"credential_id" => credential_id,
"public_key_spki" => public_key_spki
}) do
credential_id = Base.decode64!(credential_id)
public_key_spki = Base.decode64!(public_key_spki)
case Accounts.register_user(email, credential_id, public_key_spki) do
{:ok, user} ->
{:ok, _} =
Accounts.deliver_user_confirmation_instructions(
user,
&url(~p"/users/confirm/#{&1}")
)
conn
|> UserAuth.log_in_user_without_redirect(user)
|> json(%{status: :ok})
{:error, _changeset} ->
json(conn, %{status: :error})
end
end
end