Documentation Blog Community Forum Media
|
|
Sign In
    • Ash
      • Get Started
      • Philosophy
      • Why Ash
      • Extending Resources
    • AshPostgres
      • Get Started With Postgres
    • AshSqlite
      • Get Started With Sqlite
    • AshPhoenix
      • Getting Started With Ash And Phoenix
    • AshGraphql
      • Getting Started With Graphql
    • AshJsonApi
      • Getting Started With Json Api
    • AshAuthentication
      • Auth0 Quickstart
      • Getting Started With Authentication
      • Github Quickstart
      • Magic Links Quickstart
    • AshAuthenticationPhoenix
      • Getting Started With Ash Authentication Phoenix
      • Use Ash Authentication With Liveview
    • AshStateMachine
      • Get Started With State Machines
    • AshCSV
      • Get Started With Csv
    • Reactor
      • Getting Started With Reactor
    • Spark
      • Get Started With Spark
    • Ash
      • Actions
      • Aggregates
      • Atomics
      • Attributes
      • Bulk Actions
      • Calculations
      • Code Interface
      • Constraints
      • Development Utilities
      • Embedded Resources
      • Expressions
      • Flows
      • Glossary
      • Identities
      • Managing Relationships
      • Manual Actions
      • Monitoring
      • Multitenancy
      • Notifiers
      • Pagination
      • Phoenix
      • Policies
      • Pub Sub
      • Relationships
      • Security
      • Store Context In Process
      • Testing
      • Timeouts
      • Validations
    • AshPostgres
      • Migrations And Tasks
      • Polymorphic Resources
      • Postgres Expressions
      • References
      • Schema Based Multitenancy
    • AshSqlite
      • Migrations And Tasks
      • Polymorphic Resources
      • References
      • Sqlite Expressions
    • AshPhoenix
      • Working With Phoenix
    • AshGraphql
      • Graphql Generation
      • Modifying The Resolution
      • Relay
    • AshJsonApi
      • Open Api
      • Relationships
    • AshAuthentication
      • Custom Strategy
      • Policies On Authentication Resources
      • Upgrading
    • Ash
      • Contribute
      • Define Idiomatic Actions
      • Defining Manual Relationships
      • Handle Errors
      • Structure Your Project
      • Upgrade
      • Use Without Data Layers
      • Validate Changes
    • AshPostgres
      • Join Manual Relationships
      • Test With Postgres
      • Using Fragments
    • AshSqlite
      • Join Manual Relationships
      • Test With Sqlite
      • Using Fragments
    • AshGraphql
      • Authorize With Graphql
      • Handle Errors
      • Use Enums With Graphql
      • Use Json With Graphql
      • Use Subscriptions With Graphql
      • Use Unions With Graphql
    • Spark
      • Writing Extensions
    • AshGraphql
      • Monitoring
    • AshArchival
      • Archival
      • Unarchiving
    • Ash
      • Get Started
      • Philosophy
      • Why Ash
      • Extending Resources
    • AshPostgres
      • Get Started With Postgres
    • AshSqlite
      • Get Started With Sqlite
    • AshPhoenix
      • Getting Started With Ash And Phoenix
    • AshGraphql
      • Getting Started With Graphql
    • AshJsonApi
      • Getting Started With Json Api
    • AshAuthentication
      • Auth0 Quickstart
      • Getting Started With Authentication
      • Github Quickstart
      • Magic Links Quickstart
    • AshAuthenticationPhoenix
      • Getting Started With Ash Authentication Phoenix
      • Use Ash Authentication With Liveview
    • AshStateMachine
      • Get Started With State Machines
    • AshCSV
      • Get Started With Csv
    • Reactor
      • Getting Started With Reactor
    • Spark
      • Get Started With Spark
    • Ash
      • Actions
      • Aggregates
      • Atomics
      • Attributes
      • Bulk Actions
      • Calculations
      • Code Interface
      • Constraints
      • Development Utilities
      • Embedded Resources
      • Expressions
      • Flows
      • Glossary
      • Identities
      • Managing Relationships
      • Manual Actions
      • Monitoring
      • Multitenancy
      • Notifiers
      • Pagination
      • Phoenix
      • Policies
      • Pub Sub
      • Relationships
      • Security
      • Store Context In Process
      • Testing
      • Timeouts
      • Validations
    • AshPostgres
      • Migrations And Tasks
      • Polymorphic Resources
      • Postgres Expressions
      • References
      • Schema Based Multitenancy
    • AshSqlite
      • Migrations And Tasks
      • Polymorphic Resources
      • References
      • Sqlite Expressions
    • AshPhoenix
      • Working With Phoenix
    • AshGraphql
      • Graphql Generation
      • Modifying The Resolution
      • Relay
    • AshJsonApi
      • Open Api
      • Relationships
    • AshAuthentication
      • Custom Strategy
      • Policies On Authentication Resources
      • Upgrading
    • Ash
      • Contribute
      • Define Idiomatic Actions
      • Defining Manual Relationships
      • Handle Errors
      • Structure Your Project
      • Upgrade
      • Use Without Data Layers
      • Validate Changes
    • AshPostgres
      • Join Manual Relationships
      • Test With Postgres
      • Using Fragments
    • AshSqlite
      • Join Manual Relationships
      • Test With Sqlite
      • Using Fragments
    • AshGraphql
      • Authorize With Graphql
      • Handle Errors
      • Use Enums With Graphql
      • Use Json With Graphql
      • Use Subscriptions With Graphql
      • Use Unions With Graphql
    • Spark
      • Writing Extensions
    • AshGraphql
      • Monitoring
    • AshArchival
      • Archival
      • Unarchiving
View this guide on GitHub View this guide on Hex

Magic Links Quick Start Guide

Table of Contents

  1. Add the Magic Link Strategy to the User resource
  2. Create and email sender and email template

This is a quick tutorial to get you up and running on Magic Links. This assumes you’ve set up ash_authentication and password reset in your Phoenix project.

Add the Magic Link Strategy to the User resource

# ...

strategies do
  password :password do
    identity_field(:email)

    resettable do
      sender(Example.Accounts.User.Senders.SendPasswordResetEmail)
    end
  end

  # add these lines -->
  magic_link do
    identity_field :email
    sender(Example.Accounts.User.Senders.SendMagicLink)
  end
  # <-- add these lines
end

# ...

Create and email sender and email template

Inside /lib/example/accounts/user/senders/send_magic_link.ex

defmodule Example.Accounts.User.Senders.SendMagicLink do
  @moduledoc """
  Sends a magic link
  """
  use AshAuthentication.Sender
  use ExampleWeb, :verified_routes

  @impl AshAuthentication.Sender
  def send(user, token, _) do
    Example.Accounts.Emails.deliver_magic_link(
      user,
      url(~p"/auth/user/magic_link/?token=#{token}")
    )
  end
end

Inside /lib/example/accounts/emails.ex

# ...

def deliver_magic_link(user, url) do
  if !url do
    raise "Cannot deliver reset instructions without a url"
  end

  deliver(user.email, "Magic Link", """
  <html>
    <p>
      Hi #{user.email},
    </p>

    <p>
      <a href="#{url}">Click here</a> to login.
    </p>
  <html>
  """)
end

# ...
Source Report an issue