Linkhut.Accounts.User (linkhut v0.1.0)
View SourceThe User schema represents a user account in the Linkhut application.
A user can have different account types (unconfirmed, active_free, active_paying) and optional admin roles. Each user has an associated credential for authentication, can own multiple links, and participates in the OAuth system through applications, access grants, and access tokens.
Schema Fields
username
- Unique identifier for the user; must be alphanumeric and at least 3 charactersbio
- Optional information about the userunlisted
- Boolean flag to hide the user's links from public listingstype
- Account status enum::unconfirmed
,:active_free
, or:active_paying
roles
- Array of user roles, currently supports:admin
Associations
credential
- One-to-one association with user authentication credentialslinks
- One-to-many association with user's saved linksapplications
- One-to-many association with OAuth applications owned by the useraccess_grants
- One-to-many association with OAuth access grantsaccess_tokens
- One-to-many association with OAuth access tokens
Virtual Fields
authenticated_at
- Timestamp of last authentication (virtual field)
Examples
iex> changeset = User.changeset(%User{}, %{username: "johndoe", bio: "Hello world"})
iex> changeset.valid?
true
iex> User.confirm_user(%User{credential: %Credential{}}, %{})
%Ecto.Changeset{changes: %{type: :active_free}}
Summary
Functions
Creates a changeset for updating user profile information.
Creates a changeset for updating user roles.
Creates a changeset to confirm a user's email and activate their account.
Types
@type t() :: Ecto.Schema.t()
Functions
Creates a changeset for updating user profile information.
Validates the username format and ensures it's not reserved.
Parameters
user
- The user struct or changeset to updateattrs
- Map of attributes to update (username, bio)
Returns
An Ecto.Changeset
with the validated changes.
Examples
iex> User.changeset(%User{}, %{username: "validuser", bio: "My bio"})
%Ecto.Changeset{valid?: true}
iex> User.changeset(%User{}, %{username: "ab"})
%Ecto.Changeset{valid?: false, errors: [username: {"...", [...]}]}
@spec changeset_role(Ecto.Schema.t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
Creates a changeset for updating user roles.
Parameters
user
- The user struct or changeset to updateattrs
- Map containing the roles to set
Returns
An Ecto.Changeset
with validated role changes.
Examples
iex> User.changeset_role(%User{}, %{roles: [:admin]})
%Ecto.Changeset{valid?: true}
iex> User.changeset_role(%User{}, %{roles: [:invalid]})
%Ecto.Changeset{valid?: false}
@spec confirm_user(Ecto.Schema.t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()
Creates a changeset to confirm a user's email and activate their account.
This function confirms the user's email credential and upgrades their
account type from :unconfirmed
to :active_free
.
Parameters
user
- User struct with associated credentialattrs
- Attributes for email confirmation
Returns
An Ecto.Changeset
that confirms the credential and updates user type.
Examples
iex> user = %User{credential: %Credential{}}
iex> User.confirm_user(user, %{})
%Ecto.Changeset{changes: %{type: :active_free}}