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

38 lines
1.0 KiB
Elixir
Raw Permalink Normal View History

2023-10-12 03:03:10 +00:00
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
2023-10-15 02:46:30 +00:00
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
2023-10-12 03:03:10 +00:00
{:ok, user} ->
{:ok, _} =
Accounts.deliver_user_confirmation_instructions(
user,
&url(~p"/users/confirm/#{&1}")
)
conn
2023-10-15 02:46:30 +00:00
|> UserAuth.log_in_user_without_redirect(user)
|> json(%{status: :ok})
2023-10-12 03:03:10 +00:00
2023-10-15 02:46:30 +00:00
{:error, _changeset} ->
json(conn, %{status: :error})
2023-10-12 03:03:10 +00:00
end
end
end