Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new resource for BasicUser (registration) #18

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ Please install the following tools:

Use `asdf install` to install the correct versions of Elixir and Erlang.

### Database Setup

* Run `mix ecto.setup` to create the database and run migrations
* Run `mix ecto.reset` to drop the database and run migrations

### Git Hooks

Please run the `./setup-hooks.sh` script to install the git hooks.
Expand Down
1 change: 1 addition & 0 deletions lib/animina/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Animina.Accounts do
use Ash.Api

resources do
resource Animina.Accounts.BasicUser
resource Animina.Accounts.User
resource Animina.Accounts.Token
end
Expand Down
86 changes: 86 additions & 0 deletions lib/animina/accounts/resources/basic_user.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
defmodule Animina.Accounts.BasicUser do
@moduledoc """
This is the Basic User module. It is a stripped down version of the
User module. It is used for the registration form.
"""

use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshAuthentication]

attributes do
uuid_primary_key :id
attribute :email, :ci_string, allow_nil?: false
attribute :hashed_password, :string, allow_nil?: false, sensitive?: true

attribute :username, :ci_string do
constraints max_length: 15,
min_length: 2,
match: ~r/^[A-Za-z_-]*$/,
trim?: true,
allow_empty?: false
end

attribute :name, :string do
constraints max_length: 50,
min_length: 1,
trim?: true,
allow_empty?: false
end

attribute :birthday, :date, allow_nil?: false
attribute :zip_code, :string, allow_nil?: false
attribute :gender, :string, allow_nil?: false
attribute :height, :integer, allow_nil?: false
attribute :mobile_phone, :string, allow_nil?: false
end

calculations do
calculate :gravatar_hash, :string, {Animina.Calculations.Md5, field: :email}
end

preparations do
prepare build(load: [:gravatar_hash])
end

authentication do
api Animina.Accounts

strategies do
password :password do
identity_field :email
sign_in_tokens_enabled? true
confirmation_required?(false)
register_action_accept([:username, :name, :zip_code, :birthday, :height, :gender])
end
end

tokens do
enabled? true
token_resource Animina.Accounts.Token

signing_secret Animina.Accounts.Secrets
end
end

postgres do
table "users"
repo Animina.Repo
end

identities do
identity :unique_email, [:email]
identity :unique_username, [:username]
end

actions do
defaults [:create, :read]
end

code_interface do
define_for Animina.Accounts
define :read
define :create
define :by_id, get_by: [:id], action: :read
end
end
25 changes: 19 additions & 6 deletions lib/animina/accounts/resources/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,29 @@ defmodule Animina.Accounts.User do
uuid_primary_key :id
attribute :email, :ci_string, allow_nil?: false
attribute :hashed_password, :string, allow_nil?: false, sensitive?: true
attribute :username, :string, allow_nil?: false
attribute :disabled_by_platform_at, :utc_datetime, allow_nil?: true
attribute :disabled_by_user_at, :utc_datetime, allow_nil?: true
# attribute :subscribed_at, :utc_datetime, allow_nil?: false
# attribute :terms_conds_id , :uuid , allow_nil?: true
attribute :name, :string, allow_nil?: false

attribute :username, :ci_string do
constraints max_length: 15,
min_length: 2,
match: ~r/^[A-Za-z_-]*$/,
trim?: true,
allow_empty?: false
end

attribute :name, :string do
constraints max_length: 50,
min_length: 1,
trim?: true,
allow_empty?: false
end

attribute :birthday, :date, allow_nil?: false
attribute :zip_code, :string, allow_nil?: false
attribute :gender, :string, allow_nil?: false
attribute :height, :integer, allow_nil?: false
attribute :mobile_phone, :string, allow_nil?: false
# attribute :subscribed_at, :utc_datetime, allow_nil?: false
# attribute :terms_conds_id, :uuid, allow_nil?: true
end

calculations do
Expand Down
6 changes: 3 additions & 3 deletions lib/animina_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule AniminaWeb.AuthController do
use AshAuthentication.Phoenix.Controller

alias Animina.Accounts
alias Animina.Accounts.User
alias Animina.Accounts.BasicUser
alias AshPhoenix.Form

def success(conn, _activity, user, _token) do
Expand All @@ -13,7 +13,7 @@ defmodule AniminaWeb.AuthController do
|> delete_session(:return_to)
|> store_in_session(user)
|> assign(:current_user, user)
|> redirect(to: ~p"/second-step")
|> redirect(to: ~p"/registration/potential-partner")
end

def failure(conn, _activity, reason) do
Expand Down Expand Up @@ -58,7 +58,7 @@ defmodule AniminaWeb.AuthController do
|> assign(:is_register?, true)
|> assign(
:form,
Form.for_create(User, :register_with_password, api: Accounts, as: "user")
Form.for_create(BasicUser, :register_with_password, api: Accounts, as: "user")
)

render(conn, :register, layout: false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule AniminaWeb.SecondStepLive do
defmodule AniminaWeb.PotentialPartnerLive do
use AniminaWeb, :live_view

alias Animina.Accounts
alias Animina.Accounts.User
alias AniminaWeb.Registration
alias AshPhoenix.Form

@impl true
Expand All @@ -11,28 +12,10 @@ defmodule AniminaWeb.SecondStepLive do
:timer.send_interval(2500, self(), :tick)
end

current_user =
case get_user_id_from_session(session) do
nil ->
nil

"" ->
nil

user_id ->
case Accounts.User.by_id(user_id) do
{:error, _} ->
nil

{:ok, user} ->
user
end
end

socket =
socket
|> assign(points: 0)
|> assign(current_user: current_user)
|> assign(current_user: Registration.get_current_user(session))
|> assign(active_tab: :home)

{:ok, socket}
Expand All @@ -45,21 +28,6 @@ defmodule AniminaWeb.SecondStepLive do
{:noreply, socket}
end

defp get_user_id_from_session(session) do
case session["user"] do
nil ->
nil

"" ->
nil

user_id ->
user_id
|> String.split("=")
|> List.last()
end
end

@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
Expand All @@ -84,8 +52,8 @@ defmodule AniminaWeb.SecondStepLive do
~H"""
<div class="space-y-10 px-5">
<.notification_box
title="Willkommen bei animina! 🎉"
message="Open-Source Dating-Plattform, die auch ohne Zwangs-Abo gut funktioniert."
title={"Hallo #{@current_user.name}!"}
message="Danke für Deine Registierung."
/>

<.form :let={f} for={@form} action={@action} method="POST" class="space-y-6">
Expand Down
Loading