Phx.gen.auth Where to add extra user information?

I have generated an auth system with phx.gen.auth . I need to add mroe information to the user such as stripe connect id and such. Should I add directly to the user schema or create a profile schema and attach it one to one to the user schema. If going through the profile route how can I make sure all my users have an associated profile with it.

Any pointers would be really helpful.

I think the general advice has become to separate “log in methods/credentials” from “user accounts”, because you can more easily manage user data and login data.

So you may end up with

user: id, name, email
web_login: user_id, hash, last_used, last_host, recovery_code
totp_login:  user_id, secret, last_used, last_host
oauth_login: user_id, token, ...
...

etc. Instead of ending up with really fat user tables.

It maybe depends on your desired complexity, but you don’t really lose much by normalising the database.

If you do stick them all in the single table, I would recommend creating different schemas for each login method so your modules are better focused. (You can define multiple schemas that point to one table and have each only include the pertinent fields.)

You can ensure your data has integrity with database constraints (see ecto constraint validation).

E: Sorry I may have been a bit unclear to your actual question, you can put stripe_connect_id in the users table, it really depends on how much you think you will end up stuffing in there and how much you want to normalise your dataset.

I personally structure my accounts like this:

users: id, email, hashed_password
users_roles: user_id, role_id
roles: id, type
profiles: user_id, name, avatar, ...

If I wanted to store a users stripe data (unfamiliar with the specifics of the API) I personally would put it in another table:

user_stripes: user_id, connect_id, account_name?, public_key?, ...

This means you can more easily expand to have a user own multiple stripe accounts, etc.

4 Likes

phx.gen.auth already generates separate tables for users and user_tokens. OP should add the columns to the users table.

1 Like

Having different schemas and using users table more like a root seems like a great solution. Will try this out. Thanks :smiley: