Rejestracja użytkownika za pomocą adresu e-mail i hasła
Zaimplementuj standardowe uwierzytelnianie za pomocą adresu e-mail i hasła, w tym rejestrację, logowanie i resetowanie hasła użytkownika.
Rejestracja użytkownika za pomocą adresu e-mail i hasła to bezpłatna lekcja Supabase Backend as a Service na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Supabase Backend as a Service, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Supabase Backend as a Service zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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
signUpto register new users. - Understanding the email confirmation process.
- Using
signInWithPasswordto log in existing users. - Initiating password resets with
resetPasswordForEmail.
This forms the foundation for secure user management in your applications!
Ucz się Supabase Backend as a Service dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 11
- Lekcje
- 40
Często zadawane pytania
Czy lekcja „Rejestracja użytkownika za pomocą adresu e-mail i hasła” jest bezpłatna?
Tak — pełny tekst „Rejestracja użytkownika za pomocą adresu e-mail i hasła” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Supabase Backend as a Service, przejdź na CoddyKit PRO. Kurs Supabase Backend as a Service zawiera 4 lekcji w sumie.
Co nauczysz się w „Rejestracja użytkownika za pomocą adresu e-mail i hasła”?
Zaimplementuj standardowe uwierzytelnianie za pomocą adresu e-mail i hasła, w tym rejestrację, logowanie i resetowanie hasła użytkownika. Ćwiczysz Supabase Backend as a Service z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Supabase Backend as a Service?
Nie wymagamy żadnego doświadczenia. Supabase Backend as a Service w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.
Ile czasu zajmuje lekcja „Rejestracja użytkownika za pomocą adresu e-mail i hasła”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Supabase Backend as a Service?
Tak. Każda lekcja Supabase Backend as a Service zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Rejestracja użytkownika za pomocą adresu e-mail i hasła
- Logowanie społecznościowe (dostawcy OAuth)
- Zarządzanie sesjami i profilami użytkowników
- Resetowanie haseł i uwierzytelnianie za pomocą magicznych linków