0Pricing
Supabase Backend as a Service · Lezione

Registrazione degli utenti con email e password

Implementi l'autenticazione standard tramite email e password, incluse registrazione, accesso e funzionalità di reimpostazione della password.

Registrazione degli utenti con email e password è una lezione Supabase Backend as a Service gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Supabase Backend as a Service, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Supabase Backend as a Service include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Email/Password Auth Basics

Welcome to user authentication with Supabase! We'll start with the most common method: email and password.

This method allows users to create an account using their email and a chosen password, and then sign in with those credentials.

Supabase Auth Explained

Supabase provides a complete authentication system out-of-the-box. It handles:

  • User registration and login
  • Password hashing and security
  • Session management
  • Email confirmations and password resets

All of this is managed through simple client-side methods.

Initialize Supabase Client

Before we can register or log in users, we need to set up the Supabase client. This client will be our bridge to all Supabase services, including authentication.

You'll need your project's URL and anon (public) key, which you can find in your Supabase dashboard settings.

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

const SUPABASE_URL = 'YOUR_SUPABASE_URL'
const SUPABASE_ANON_KEY = 'YOUR_SUPABASE_ANON_KEY'

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

console.log('Supabase client initialized!')

Registering New Users

To let users create an account, we use the signUp method. This method takes an email and password, and optionally other user metadata.

Supabase handles the secure storage of the password and typically sends a confirmation email to the user.

async function registerUser(email, password) {
  const { data, error } = await supabase.auth.signUp({
    email: email,
    password: password
  })

  if (error) {
    console.error('Sign up error:', error.message)
  } else {
    console.log('User signed up successfully:', data.user.email)
    console.log('Check your email for a confirmation link!')
  }
}

Try User Sign-Up

Let's put the signUp method into a runnable example. This code simulates a new user registration.

In a real app, you'd get the email and password from a form.

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

const SUPABASE_URL = 'https://abcde12345.supabase.co'
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFiY2RlMTIzNDUiLCJyb2xlIjoiYW5vbiIsImlhdCI6MTY3ODkwMTIzNCwiZXhwIjoxOTk0NTY3ODkwfQ.YOUR_SECRET_KEY'

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

async function trySignUp() {
  const testEmail = `testuser${Date.now()}@example.com`
  const testPassword = 'securepassword123'

  console.log(`Attempting to sign up ${testEmail}`)
  const { data, error } = await supabase.auth.signUp({
    email: testEmail,
    password: testPassword
  })

  if (error) {
    console.error('Sign up failed:', error.message)
  } else {
    console.log('Sign up successful! User ID:', data.user.id)
    console.log('Confirmation email sent to:', data.user.email)
  }
}

trySignUp()

Email Confirmation Step

After a user signs up, Supabase usually sends a confirmation email. This is a vital security measure to verify email ownership.

  • Users click a link in the email.
  • This link verifies their email address.
  • Their account becomes active and they can then sign in.

You can configure email templates and redirection URLs in your Supabase project settings.

Logging In Existing Users

Once a user has registered and confirmed their email, they can log in using the signInWithPassword method.

This method authenticates the user and returns a session object, allowing them to access protected resources.

async function signInUser(email, password) {
  const { data, error } = await supabase.auth.signInWithPassword({
    email: email,
    password: password
  })

  if (error) {
    console.error('Sign in error:', error.message)
  } else {
    console.log('User signed in successfully:', data.user.email)
    console.log('Session access token:', data.session.access_token)
  }
}

Try User Sign-In

Here's a runnable example of signing in a user. Remember, for this to work, the user must already be registered and their email confirmed.

You would typically store the session token for subsequent authenticated requests.

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

const SUPABASE_URL = 'https://abcde12345.supabase.co'
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFiY2RlMTIzNDUiLCJyb2xlIjoiYW5vbiIsImlhdCI6MTY3ODkwMTIzNCwiZXhwIjoxOTk0NTY3ODkwfQ.YOUR_SECRET_KEY'

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

async function trySignIn() {
  const existingEmail = 'an_existing_user@example.com' // Replace with a real registered email
  const existingPassword = 'securepassword123' // Replace with that user's password

  console.log(`Attempting to sign in ${existingEmail}`)
  const { data, error } = await supabase.auth.signInWithPassword({
    email: existingEmail,
    password: existingPassword
  })

  if (error) {
    console.error('Sign in failed:', error.message)
  } else {
    console.log('Sign in successful! User ID:', data.user.id)
    console.log('Current session:', data.session)
  }
}

trySignIn()

Initiating Password Resets

What if a user forgets their password? Supabase makes it easy to handle password resets securely using resetPasswordForEmail.

This method sends a special magic link to the user's email, which they can use to set a new password.

async function requestPasswordReset(email) {
  const { data, error } = await supabase.auth.resetPasswordForEmail(email, {
    redirectTo: 'https://example.com/update-password' // User redirected here after clicking link
  })

  if (error) {
    console.error('Password reset request error:', error.message)
  } else {
    console.log('Password reset email sent successfully to:', email)
  }
}

Auth Methods Check

Which Supabase Auth method is used to create a new user account with an email and password?

Email/Password Auth Summary

Great job! You've learned the core of email and password authentication with Supabase:

  • Initializing the Supabase client.
  • Using signUp to register new users.
  • Understanding the email confirmation process.
  • Using signInWithPassword to log in existing users.
  • Initiating password resets with resetPasswordForEmail.

This forms the foundation for secure user management in your applications!

Domande Frequenti

La lezione «Registrazione degli utenti con email e password» è gratuita?

Sì — il testo completo di «Registrazione degli utenti con email e password» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Supabase Backend as a Service, passa a CoddyKit PRO. Il corso Supabase Backend as a Service include 4 lezioni in totale.

Cosa imparerò in «Registrazione degli utenti con email e password»?

Implementi l'autenticazione standard tramite email e password, incluse registrazione, accesso e funzionalità di reimpostazione della password. Eserciti Supabase Backend as a Service con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Supabase Backend as a Service?

Non è richiesta alcuna esperienza precedente. Supabase Backend as a Service su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Registrazione degli utenti con email e password»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Supabase Backend as a Service?

Sì. Ogni lezione Supabase Backend as a Service include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Registrazione degli utenti con email e password
  2. Accessi social (provider OAuth)
  3. Gestione delle sessioni e dei profili utente
  4. Reimpostazione della password e autenticazione tramite magic link
← Torna a Supabase Backend as a Service