Comments (4)
Looks good to me, and it'll be easy to refactor!
A note is that if the :userinfo
association is preloaded on the user, it'll update instead of replacing it with changeset (the docs describe how it works).
Also, instead of checking id, you can just check if the struct is persisted: Ecto.get_meta(user, :state) != :loaded
.
from pow.
You can do this modifying the changeset method adding the user info as a nested changeset like so:
def changeset(user_or_changeset, attrs) do
user
|> pow_changeset(attrs)
|> pow_extension_changeset(attrs)
|> cast_assoc(:user_info, required: true)
end
This will call the changeset method in your UserInfo
schema module.
I think this is the most appropriate way. I'll probably leave ControllerCallbacks out of readme as it's mostly for well-versed developers. The issue you experience is because it'll only pull the extension callbacks. You'll have to create a different controller callback module. But it'll be a lengthy explanation, and I think the nested association would work here.
from pow.
Oh, the hex docs on cast_assoc/3
: https://hexdocs.pm/ecto/Ecto.Changeset.html#cast_assoc/3
from pow.
Oh great. Thanks. You're right that Changeset
is the best place. It's ironic that I migrated from Coh..
to Pow
because redirect
doesn't work. (#372 there) If I utilized cast_assoc/3
there I still reside there.
Anyway I finally managed to make it work as expected. Not a pretty codes though:
def changeset(user, attrs) do
attrs = refine_attrs(user, attrs)
user
|> pow_changeset(attrs)
|> pow_extension_changeset(attrs)
|> Ecto.Changeset.cast(attrs, [:nickname, :locale, :role])
|> Ecto.Changeset.validate_required([:nickname])
|> Ecto.Changeset.unique_constraint(:nickname)
|> Ecto.Changeset.cast_assoc(:userinfo, required: true)
end
@doc false
defp refine_attrs(user, attrs) do
if !Map.get(user, :id) && map_size(attrs) > 0 do
case Map.keys(attrs) |> hd |> is_atom do
true -> Map.put(attrs, :userinfo, %{})
false -> Map.put(attrs, "userinfo", %{})
end
else
attrs
end
end
:userinfo
is added when a new user
is created, so that even from default Pow
operations can be worked too.
I already was aware of cast_assoc/3
, but didn't know this can work. UUID was my walkaround for this situation, and other libs too. Like note in Arc.
I've learned one good thing haha. Thanks. Now my webapp is working smoothly.
from pow.
Related Issues (20)
- ecto_sql 3.8.0 gives compilation error for generated user schema HOT 4
- Replacing Phoenix.Token with JWT-based for signing and verifiying of tokens HOT 1
- Unable to logout user in Pow API HOT 2
- Feature Request: Apple Passkeys HOT 1
- Pow.Plug.authenticate_user/1 performance HOT 2
- Reset password - test fails issue HOT 1
- Upgrading to Phoenix 1.7 HOT 5
- Create account without email/password using some unique ID?
- Phoenix 1.7 compilers warning HOT 1
- Maintain case when storing emails HOT 2
- Permissions issues with mnesia backend for Pow HOT 15
- Improve mix tasks
- Crash @missing_field? in Schema.ex HOT 1
- Inject values on registration HOT 2
- users_context seems to be ignored HOT 1
- Upgrading POW from 1.6 to 1.7 HOT 12
- Changeset errors into view HOT 1
- POW & LiveView - best way to implement `assigns.current_user` ? HOT 7
- Elixir 1.15 deprecation warnings HOT 1
- Persistence of stale session ids in MnesiaCache HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pow.