/supabase-tutorials

How to enable magic link login in Supabase?

Enable Supabase magic link login with our step-by-step guide. Learn to set up your project, configure authentication, code the login flow, and test passwordless sign-in.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to enable magic link login in Supabase?

 

Step 1: Set Up Your Supabase Project

 

First, ensure you have a Supabase project set up. If you haven’t done so yet, go to Supabase's website and create an account. Once logged in, you can start a new project by providing a name and choosing a database password.

 

Step 2: Configure Authentication Settings

 

  • Navigate to your Supabase dashboard.
  • Under the Authentication tab on the left sidebar, you will find a section called Settings.
  • Go to Settings, and you should see options for Provider Settings and Email Auth Settings.
  • In the Email Auth Settings, ensure that the Magic Link Sign In is toggled on.

 

Step 3: Set Up Environment for Your Application

 

You need to make sure that your application environment can communicate with your Supabase instance. Here’s a generic example using Node.js but adapt it to your stack if different:


npm install @supabase/supabase-js

After installation, initialize Supabase in your application:


import { createClient } from '@supabase/supabase-js'

const supabaseUrl = 'https://xyzcompany.supabase.co'
const supabaseKey = 'public-anon-key'
const supabase = createClient(supabaseUrl, supabaseKey)

 

Step 4: Code the Magic Link Login Functionality

 

Set up a function to trigger the magic link to the user's email:


async function sendMagicLink(email) {
  const { user, error } = await supabase.auth.signInWithOtp({ email })
  
  if (error) {
    console.error('Error sending Magic Link:', error)
  } else {
    console.log('Magic link sent to:', user.email)
  }
}

Call this function and pass the user's email input:


const email = '[email protected]'
sendMagicLink(email)

 

Step 5: Handle the Auth Redirect

 

Ensure your app can handle the magic link redirection. Typically, Supabase will append a token and a type to the URL when redirecting. You need to capture and use these to update the user's authentication state.

Here's how you might do this in a client-side app:


import { useEffect } from 'react'
import { supabase } from './supabaseClient'

useEffect(() => {
  const { data } = supabase.auth.onAuthStateChange((event, session) => {
    if (event === 'SIGNED\_IN') {
      // User now signed in, handle post-login logic here
      console.log('User signed in:', session.user)
    }
  })

  return () => {
    data.unsubscribe()
  }
}, [])

 

Step 6: Testing Magic Link

 

Test this functionality by trying to log in with a valid email. Ensure Supabase email configuration is set properly to receive the email. Supabase's free tier uses a shared SMTP server for sending emails, so make sure these emails don’t land in the spam folder.

Test the flow end-to-end to verify that users can initiate a login, receive an email, click the link, and be redirected to a logged-in state in your application.

 

Following these steps, you will be able to enable the magic link login for your application using Supabase, easily integrating secure and efficient passwordless authentication for users.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022