Phauxth v2.0.0-alpha.1 Phauxth.Confirm.Base behaviour View Source
Base module for handling user confirmation.
This is used by Phauxth.Confirm and Phauxth.Confirm.PassReset, and it can also be used to create custom user confirmation modules.
Link to this section Summary
Callbacks
Gets the user struct based on the supplied key
Prints out a log message and then returns {:ok, user} or {:error, message} to the calling function
Verifies the confirmation key and gets the user data from the database
Link to this section Functions
Checks if a reset token has been sent to the user.
Checks if the user has been confirmed.
Link to this section Callbacks
Gets the user struct based on the supplied key.
Prints out a log message and then returns {:ok, user} or {:error, message} to the calling function.
Verifies the confirmation key and gets the user data from the database.
Phauxth.Confirm.verify
is used to confirm an email for new users
and Phauxth.Confirm.PassReset.verify
is used for password resetting.
Options
There are three options for the verify function:
:session_module
- the sessions module- the default is Phauxth.Config.session_module()
:endpoint
- the name of the endpoint of your app- this can also be set in the config
:log_meta
- additional custom metadata for Phauxth.Log- this should be a keyword list
In addition, there are also options for verifying the token.
Examples
The following function is an example of using Phauxth.Confirm.verify
in a Phoenix controller.
def index(conn, params) do
case Phauxth.Confirm.verify(params) do
{:ok, user} ->
Users.confirm_user(user)
message = "Your account has been confirmed"
Users.Message.confirm_success(user.email)
handle_success() # redirect or send json
{:error, message} ->
handle_error()
end
end
In this example, the Users.confirm_user
function updates the
database, setting the confirmed_at
value to the current time.
Password resetting
For password resetting, use Phauxth.Confirm.PassReset.verify
, as
in the following example:
def update(conn, %{"password_reset" => params}) do
case Phauxth.Confirm.PassReset.verify(params) do
{:ok, user} ->
Users.update_password(user, params)
|> handle_password_reset(conn, params)
{:error, message} ->
handle_error()
end
end
The Users.update_password
function tries to add the new password
to the database. If the password reset is successful, the handle_password_reset
function sends a message by email to the user and redirects the
user to the next page or sends a json response. If unsuccessful, the
handle_password_reset
function handles the error.