Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Email confirmation

Jamison Dance edited this page Mar 21, 2017 · 6 revisions

This page gives an overview of the Openmaize.ConfirmEmail and Openmaize.ResetPassword plugs.

If you have setup your app using mix openmaize.phx --confirm, most of the functions needed to implement email confirmation will be ready. However, you will still need to create an Email module yourself, using an email library of your choice. See the example below for information about how to do this with Bamboo.

An outline of the email confirmation process

The processes of confirming the user's email address and of resetting the password are mostly the same as each other and are shown below (in this guide, the user_controller and password_reset_controller are files created by the mix openmaize.phx --confirm command):

  1. A key is created and a link containing the key is sent to the user
    • with email confirmation, this is done when creating the user in the user_controller
    • when resetting the password, this is done in the create function in the password_reset_controller
  2. The user clicks on the link to confirm his / her email
    • this will send the user to the confirm_email function in the session_controller or the edit function in the password_reset_controller
  3. The key is checked by the Openmaize.ConfirmEmail or Openmaize.ResetPassword plug
    • if the key is correct and still valid, an openmaize_info message is added to the conn
    • if the key is invalid, an openmaize_error message is added to the conn
  4. The user is redirected to the login page, or an info / error message is sent to the user

Example email module

This example app shows you how you can use Bamboo, with Bamboo.SMTPAdapter, to handle emailing the users.

The relevant files to look at are:

  • mix.exs
    • :bamboo and :bamboo_smtp are added to the deps
  • lib/email.ex
    • the main email module - containing the ask_confirm/2, ask_reset/2 and receipt_confirm/1 functions
  • lib/welcome/mailer.ex
    • a simple module that provides the deliver_now and deliver_later functions.
  • config/config.exs
    • main configuration
  • config/test.exs
    • test configuration