Skip to content

Unique constraint on users #2

Open
@samhstn

Description

@samhstn

When trying to register with a user that is already taken you will arrive at a phoenix error page.

This should instead be handled by the changeset and flash this error.

This can be easily fixed by adding a unique constrain on :email in the user model here

In other words change this:

def changeset(struct, params \\ %{}) do
  struct
  |> cast(params, @required_fields ++ @optional_fields)
  |> validate_required(@required_fields)
end

to this:

def changeset(struct, params \\ %{}) do
  struct
  |> cast(params, @required_fields ++ @optional_fields)
  |> validate_required(@required_fields)
  |> unique_constraint(:email)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions