Update sign_up
This commit is contained in:
parent
3ad43376c4
commit
1b404b3ea9
2 changed files with 55 additions and 7 deletions
|
@ -103,6 +103,41 @@ defmodule Nulla.Models.Actor do
|
|||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def create_actor_minimal(username, domain, publicKeyPem) do
|
||||
id = Snowflake.next_id()
|
||||
|
||||
attrs = %{
|
||||
id: id,
|
||||
domain: domain,
|
||||
ap_id: "https://#{domain}/users/#{username}",
|
||||
type: "Person",
|
||||
following: "https://#{domain}/users/#{username}/following",
|
||||
followers: "https://#{domain}/users/#{username}/followers",
|
||||
inbox: "https://#{domain}/users/#{username}/inbox",
|
||||
outbox: "https://#{domain}/users/#{username}/outbox",
|
||||
featured: "https://#{domain}/users/#{username}/collections/featured",
|
||||
featuredTags: "https://#{domain}/users/#{username}/collections/tags",
|
||||
preferredUsername: username,
|
||||
url: "https://#{domain}/@#{username}",
|
||||
manuallyApprovesFollowers: false,
|
||||
discoverable: true,
|
||||
indexable: true,
|
||||
published: DateTime.utc_now(),
|
||||
memorial: false,
|
||||
publicKey:
|
||||
Jason.OrderedObject.new(
|
||||
id: "https://#{domain}/users/#{username}#main-key",
|
||||
owner: "https://#{domain}/users/#{username}",
|
||||
publicKeyPem: publicKeyPem
|
||||
),
|
||||
endpoints: Jason.OrderedObject.new(sharedInbox: "https://#{domain}/inbox")
|
||||
}
|
||||
|
||||
%__MODULE__{}
|
||||
|> changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def get_actor(username, domain) do
|
||||
Repo.get_by(__MODULE__, preferredUsername: username, domain: domain)
|
||||
end
|
||||
|
|
|
@ -16,14 +16,27 @@ defmodule NullaWeb.AuthController do
|
|||
|> redirect(to: "/")
|
||||
end
|
||||
|
||||
def sign_up(conn, params) do
|
||||
case User.create_user(params) do
|
||||
{:ok, user} ->
|
||||
conn
|
||||
|> put_session(:user_id, user.id)
|
||||
|> put_flash(:info, "You're registred!")
|
||||
|> redirect(to: "/")
|
||||
def sign_up(conn, %{"username" => username, "email" => email, "password" => password}) do
|
||||
instance_settings = InstanceSettings.get_instance_settings!()
|
||||
domain = instance_settings.domain
|
||||
hashed_password = Argon2.hash_pwd_salt(password)
|
||||
|
||||
{publicKeyPem, privateKeyPem} = Nulla.KeyGen.gen()
|
||||
|
||||
with {:ok, actor} <- Actor.create_actor_minimal(username, domain, publicKeyPem),
|
||||
{:ok, user} <-
|
||||
User.create_user(%{
|
||||
id: actor.id,
|
||||
email: email,
|
||||
password: hashed_password,
|
||||
privateKeyPem: privateKeyPem,
|
||||
last_active_at: DateTime.utc_now()
|
||||
}) do
|
||||
conn
|
||||
|> put_session(:user_id, user.id)
|
||||
|> put_flash(:info, "You're registred!")
|
||||
|> redirect(to: "/")
|
||||
else
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "sign_up.html", changeset: changeset)
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue