How to create user by script for phx_gen_auth

I’d like to create a user by script for phx_gen_auth but I’m not sure what the best way is. Just filling inserting into the database table directly?

I’m pretty sure phx_gen_auth auto-generates a Context file. This will be called Accounts if you copied from the docs (as I usually do). There’s a function in the Accounts context called Accounts.register_user/1. I would call this in order to programmatically create users. Did I answer your question?

Thanks, this was my approach to just create a record in the users table. But I’m not sure about the field “confirmed_at”. Should this be set to have a confirmed user, so no confirmation email has to be sent?

Ok, just looked into the confirm_changeset function and there it is: the “confirmed_at” field has to be set.

Yeah. User.confirm_changeset/1 isn’t used by Accounts.register_user/1, though. Maybe you want a new changeset for User.register_and_confirm_changeset if you’re hoping to do those in one step.

If you want to skip sending an email, look at Accounts.deliver_user_confirmation_instructions/2. That’s where the email is sent. Try to see where that’s called, and don’t call it :smiley:

1 Like