Shipped
HomeContacts
  • Get started
  • 📚Tutorials
    • Make a waiting list
    • Launch a pre-sale
    • Build a SaaS
    • Create your store on Lemon Squeezy
  • 🟩Features
    • AI Services
    • Affiliate Program
    • Analytics
    • Authentication
      • MailChimp
      • Loops
      • AWS SES
      • SendGrid
      • Supabase Auth
        • Supabase Authentication Flow
        • Supabase Magic Link
        • Supabase Email & Password
        • Supabase Login with Google
    • API endpoints
      • 🛡️Authenticated API
    • Blog
    • Customer support
    • Chrome Extension
    • Dark mode
    • Database
      • Update your database
      • MongoDB
    • Emails
    • Error pages
    • Icons
    • Onboarding
    • Payments
      • Lemon Squeezy
        • Subscriptions
        • One-time purchase
        • Test mode
      • Stripe
    • Private pages
    • SEO
    • shadcn/ui
    • Supabase
    • Workspace / Organizations
  • 📦Components
    • AccountMenu
    • CtaBox
    • DarkModeSwitch
    • Explainer video
    • FAQ
    • Features
    • Footer
    • Header
    • Hero
    • Lifetime
    • Pricing
    • Sales Notification
    • Secondary Sidebar Pages
    • Sidebar
    • Tabs
    • Testimonials
    • Waitlist
    • WebAppPage
  • 🚀Deployment
  • ✅Other
    • Configuration
    • Changelog widget
    • Favicon
    • Google Fonts
    • Sitemap
    • Theme
  • Updates
  • GitHub Repository
  • Support
Powered by GitBook
On this page
  • SignUp / Login
  • Dashboard
  • Hook useSupabaseAuth
  • Hook useSupabaseSession
  • Usage
  • Check the session
  • User Management

Was this helpful?

  1. Features
  2. Authentication
  3. Supabase Auth

Supabase Magic Link

Authenticate users with a password-less method

Shipped comes with support for Supabase Magic Link.

The Magic Link Authentication is a password-less authentication method, that involves sending an email to the user, with a link. When the user clicks on that link, they are redirected to the website, and an active authentication session is created under the hood.

It is very popular and it is used in favor of email & password authentication because it saves you from storing sensitive data.

If you need Supabase Auth, move to the supabase branch of the Shipped git repository.

All the code changes described below are already applied there.

Use this command in the terminal to move to the branch:

git checkout supabase

SignUp / Login

Use this code in SignUp.tsx and Login.tsx

import toast from "react-hot-toast";
import { useSupabaseAuth } from "@/hooks/supabase/useSupabaseAuth";

/* ... */
const { onSignWithMagicLink } = useSupabaseAuth({
  onMagicLinkSignInSuccess: (data) => {
    toast.success(`Check your inbox for the magic link`);
  },
  onMagicLinkSignInError: (error) => {
    toast.error(error?.message || "An error occurred");
  },
});

const onEmailSignIn = async () => {
  setSigningInWithEmail(true);
  await onSignWithMagicLink(email, signInCallbackUrl);
  setSigningInWithEmail(false);
};

Dashboard

Replace useSession with useSupabaseSession in the dashboard route src/app/dashboard/page.tsx

import { useSupabaseSession } from "@/hooks/supabase/useSupabaseSession";

/* ... */
const { session, status } = useSupabaseSession();

Hook useSupabaseAuth

The new react hook useSupabaseAuth is needed to start the Magic Link Authentication flow. It returns an object with:

onSignWithMagicLink: (email: string) => Promise<void>
onSignOut: () => Promise<void>

The hook also supports two optional arguments, to set the callback to manage the success and error events.

onMagicLinkSignInSuccess?: (data: any) => void;
onMagicLinkSignInError?: (error: any) => void;

The redirect link, after authentication, is defined in the config file in the variables signInCallbackUrl which is used in the supabase auth callback route src/app/supabase/auth/callback/route.ts.

Hook useSupabaseSession

The second hook included with Shipped is useSupabaseSession which is useful to get the current session on the client side and to know if the user is currently logged in or not. Its interface is compatible with the useSession hook of next-auth.

const { 
    status,  // "loading" | "authenticated" | "unauthenticated"
    session // Session | null 
} = useSupabaseSession()

Usage

Check the session

To know if the user is currently signed in (has an active session) use the hook useSupabaseSession this way:

const { session, status } = useSupabaseSession();

status can have three possible values:

  • loading

  • authenticated

  • unauthenticated

If the user is authenticated the session object contains a user property with the email (session.user.email).

User Management

By default, a new user is created if not already present. Once the user is created, you can manage it from the Supabase Authentication dashboard.

PreviousSupabase Authentication FlowNextSupabase Email & Password

Last updated 1 year ago

Was this helpful?

🟩
Suapabase Auth