0
0
Supabasecloud~5 mins

Email/password authentication in Supabase

Choose your learning style9 modes available
Introduction

Email/password authentication lets users sign up and log in using their email and a password. It keeps accounts safe and easy to use.

You want users to create accounts with their email and password.
You need to control who can access your app or service.
You want a simple way to manage user sign-in without social logins.
You want to send password reset emails to users.
You want to protect user data behind a login.
Syntax
Supabase
const { data, error } = await supabase.auth.signUp({
  email: 'user@example.com',
  password: 'strong-password'
})

Use signUp to register a new user with email and password.

Check error to handle signup problems like weak passwords or duplicate emails.

Examples
Registers a new user with email alice@example.com and password password123.
Supabase
const { data, error } = await supabase.auth.signUp({
  email: 'alice@example.com',
  password: 'password123'
})
Logs in an existing user with email and password.
Supabase
const { data, error } = await supabase.auth.signInWithPassword({
  email: 'alice@example.com',
  password: 'password123'
})
Attempts signup with a weak password; error will explain the issue.
Supabase
const { data, error } = await supabase.auth.signUp({
  email: 'bob@example.com',
  password: 'weak'
})
Sample Program

This code creates a Supabase client and registers a new user with email and password. It prints success or error messages.

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

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

async function registerUser() {
  const { data, error } = await supabase.auth.signUp({
    email: 'newuser@example.com',
    password: 'SecurePass123!'
  })

  if (error) {
    console.log('Signup error:', error.message)
  } else {
    console.log('Signup success! User ID:', data.user.id)
  }
}

registerUser()
OutputSuccess
Important Notes

Passwords should be strong to keep accounts safe.

Supabase sends a confirmation email by default after signup.

Always handle errors to improve user experience.

Summary

Email/password authentication lets users create and access accounts securely.

Use signUp to register and signInWithPassword to log in.

Check for errors to handle weak passwords or duplicate emails.