Skip to content

Commit

Permalink
Nostr Wallet Register
Browse files Browse the repository at this point in the history
  • Loading branch information
leobz committed Jun 22, 2023
1 parent 338d7b9 commit 6f22d76
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 19 deletions.
2 changes: 2 additions & 0 deletions lib/bookmark/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ defmodule Bookmark.Accounts.User do
|> unique_constraint(:username)
|> validate_format(:username, ~r/[a-zA-Z][a-zA-Z0-9-_]/)
|> unique_constraint(:nostr_key)
|> unsafe_validate_unique(:email, Bookmark.Repo)
|> unique_constraint(:email)
end

defp validate_email(changeset) do
Expand Down
78 changes: 59 additions & 19 deletions lib/bookmark_web/controllers/user_registration_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,21 @@ defmodule BookmarkWeb.UserRegistrationController do
alias Bookmark.Accounts.User
alias BookmarkWeb.UserAuth

def new(conn, _params) do
changeset = Accounts.change_user_registration(%User{})
title = "bookmark.org user registration"

attrs_list = [
%{property: "og:title", content: title},
%{property: "og:image", content: "https://bookmark.org/images/bookmark-logo-wide.png"},
%{property: "og:description", content: "join bookmark.org to archive, share, and earn"}
]
#*********************************** Actions *************************************
def new(conn, params) do
path = case params["type"] do
"email" -> "new_with_email.html"
"nostr" -> "new_with_nostr.html"
_ ->"new.html"
end

render(conn, "new.html",
changeset: changeset,
title: title,
meta_attrs: attrs_list
)
render_new(conn, path)
end

def create(conn, %{"user" => user_params}) do
wallet_key = BookmarkWeb.WalletController.get_new_wallet_key()
new_params = Map.put(user_params, "wallet_key", wallet_key)
register_type = user_params["register_type"]

case Accounts.register_user(new_params) do
case create_user(user_params, register_type) do
{:ok, user} ->
{:ok, _} =
Accounts.deliver_user_confirmation_instructions(
Expand All @@ -37,12 +30,59 @@ defmodule BookmarkWeb.UserRegistrationController do
conn
|> put_flash(
:info,
"Backup your wallet key " <> wallet_key <> " you will not see this message again"
"Backup your wallet key " <> user.wallet_key <> " you will not see this message again"
)
|> UserAuth.log_in_user(user)

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset, meta_attrs: nil, title: nil)
IO.inspect(changeset, label: "Error")

render(conn, "new_with_#{register_type}.html", changeset: changeset, meta_attrs: nil, title: nil)
end
end

#*********************************** Helper functions *************************************ç
defp render_new(conn, template_path) do
changeset = Accounts.change_user_registration(%User{})
title = "bookmark.org user registration"

attrs_list = [
%{property: "og:title", content: title},
%{property: "og:image", content: "https://bookmark.org/images/bookmark-logo-wide.png"},
%{property: "og:description", content: "join bookmark.org to archive, share, and earn"}
]

render(conn, template_path,
changeset: changeset,
title: title,
meta_attrs: attrs_list
)
end

defp create_user(params, register_type) do
wallet_key = BookmarkWeb.WalletController.get_new_wallet_key()

case register_type do
"email" ->
IO.puts("Entro al create user: email")
new_params = %{
username: params["username"],
password: params["password"],
email: params["email"],
wallet_key: wallet_key
}
Accounts.register_user(new_params)

"nostr" ->
nostr_key = %{pubkey: params["nostr_key"], relays: [], name: "_"}

new_params = %{
username: params["username"],
email: nostr_key[:pubkey],
nostr_key: nostr_key,
wallet_key: wallet_key
}
Accounts.nostr_register_user(new_params)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<div class="display-box">
<h1 class="display-box-headline" style="color: white;">Create an account</h1>

<.form :let={f} for={@changeset} action={Routes.user_registration_path(@conn, :create)} OnSubmit="doNip07Login()">
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<span style="color: #1e1e1e">

<%= label(:username, "Username", style: "color:white;") %>
<%= text_input(f, :username, id: :username, required: true, class: "form-input", placeholder: "Choose your username") %>
<%= error_tag(f, :username) %>

<%= text_input(f, :nostr_key, id: :nostr_key, value: "", hidden: true) %>

<%= text_input(f, :register_type, value: "nostr", hidden: true) %>

</span>
<div>
<br>
<%= submit("Register", class: "donate-button") %>
</div>
</.form>

<p>
<%= link("Log in", to: Routes.user_session_path(@conn, :new)) %> | <%= link(
"Forgot your password?",
to: Routes.user_reset_password_path(@conn, :new)
) %>
</p>
</div>


<script>
let nip07LoginExecuted = false;
async function doNip07Login() {
if (nip07LoginExecuted) { return; }
event.preventDefault();
var username = document.getElementById('username');
if (username.value != "") {
const pubKey = await unwrap(window.nostr).getPublicKey();
const nostrKeyInput = document.getElementById('nostr_key');
nostrKeyInput.value = pubKey;
nip07LoginExecuted = true;
const form = document.querySelector('form');
form.submit();
}
}
function unwrap(v) {
if (v === undefined || v === null) {
throw new Error("missing value");
}
return v;
}
</script>

0 comments on commit 6f22d76

Please sign in to comment.