mix phx.gen.auth (Phoenix v1.6.2) View Source

Generates authentication logic for a resource.

$ mix phx.gen.auth Accounts User users

The first argument is the context module followed by the schema module and its plural name (used as the schema table name).

Additional information is available in the mix phx.gen.auth guide.

Password hashing

The password hashing mechanism defaults to bcrypt for Unix systems and pbkdf2 for Windows systems. Both systems use the Comeonin interface.

The password hashing mechanism can be overridden with the --hashing-lib option. The following values are supported:

We recommend developers to consider using argon2, which is the most robust of all 3. The downside is that argon2 is quite CPU and memory intensive, and you will need more powerful instances to run your applications on.

For more information about choosing these libraries, see the Comeonin project.

Web namespace

By default, the controllers and view will be namespaced by the schema name. You can customize the web module namespace by passing the --web flag with a module name, for example:

$ mix phx.gen.auth Accounts User users --web Warehouse

Which would generate the controllers, views, templates and associated tests in nested in the MyAppWeb.Warehouse namespace:

  • lib/my_app_web/controllers/warehouse/user_auth.ex
  • lib/my_app_web/controllers/warehouse/user_confirmation_controller.ex
  • lib/my_app_web/views/warehouse/user_confirmation_view.ex
  • lib/my_app_web/templates/warehouse/user_confirmation/new.html.heex
  • test/my_app_web/controllers/warehouse/user_auth_test.exs
  • test/my_app_web/controllers/warehouse/user_confirmation_controller_test.exs
  • and so on...

Binary ids

The --binary-id option causes the generated migration to use binary_id for its primary key and foreign keys.

Default options

This generator uses default options provided in the :generators configuration of your application. These are the defaults:

config :your_app, :generators,
  binary_id: false,
  sample_binary_id: "11111111-1111-1111-1111-111111111111"

You can override those options per invocation by providing corresponding switches, e.g. --no-binary-id to use normal ids despite the default configuration.

Custom table names

By default, the table name for the migration and schema will be the plural name provided for the resource. To customize this value, a --table option may be provided. For example:

$ mix phx.gen.auth Accounts User users --table accounts_users

This will cause the generated tables to be named "accounts_users" and "accounts_users_tokens".