diff --git a/lib/putzplan/accounts/user.ex b/lib/putzplan/accounts/user.ex index c0a829f..804ab6f 100644 --- a/lib/putzplan/accounts/user.ex +++ b/lib/putzplan/accounts/user.ex @@ -20,6 +20,21 @@ defmodule Putzplan.Accounts.User do store_all_tokens? true require_token_presence_for_authentication? true end + + strategies do + oidc :oidc do + client_id "putzplan" + base_url "http://localhost:8080/realms/master/" + client_secret "Kc3DkJiIrIr59HQhDmneqqB3iy6H8gxH" + nonce true + redirect_uri "http://localhost:4000/auth" + authorization_params [scope: "profile email"] + end + end + end + + identities do + identity :id, [:id] end sqlite do @@ -36,6 +51,24 @@ defmodule Putzplan.Accounts.User do get? true prepare AshAuthentication.Preparations.FilterBySubject end + + create :register_with_oidc do + argument :user_info, :map, allow_nil?: false + argument :oauth_tokens, :map, allow_nil?: false + upsert? true + upsert_identity :id + + change AshAuthentication.GenerateTokenChange + + change fn changeset, _ctx -> + user_info = Ash.Changeset.get_argument(changeset, :user_info) + dbg(user_info) + + changeset + |> Ash.Changeset.change_attribute(:name, user_info["name"]) + |> Ash.Changeset.change_attribute(:id, user_info["sub"]) + end + end end policies do @@ -49,6 +82,7 @@ defmodule Putzplan.Accounts.User do end attributes do - uuid_primary_key :id + attribute :id, :uuid, allow_nil?: false, primary_key?: true + attribute :name, :string, allow_nil?: false end end diff --git a/priv/repo/migrations/20250405114509_add_user_name.exs b/priv/repo/migrations/20250405114509_add_user_name.exs new file mode 100644 index 0000000..cb6e05a --- /dev/null +++ b/priv/repo/migrations/20250405114509_add_user_name.exs @@ -0,0 +1,9 @@ +defmodule Putzplan.Repo.Migrations.AddUserName do + use Ecto.Migration + + def change do + alter table(:users, primary_key: false) do + add :name, :string, null: false + end + end +end