Supabase Backend as a Service · บทเรียน

การลงทะเบียนผู้ใช้ด้วยอีเมลและรหัสผ่าน

นำการตรวจสอบสิทธิ์ด้วยอีเมลและรหัสผ่านมาตรฐานมาใช้ รวมถึงฟังก์ชันสมัครบัญชี เข้าสู่ระบบ และรีเซ็ตรหัสผ่าน

บทเรียน 1 จาก 411 ขั้นตอน

การลงทะเบียนผู้ใช้ด้วยอีเมลและรหัสผ่าน เป็นบทเรียน Supabase Backend as a Service ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Supabase Backend as a Service และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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!

เริ่มต้นได้ฟรี

เรียนรู้ Supabase Backend as a Service ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
11
บทเรียน
40

คำถามที่พบบ่อย

บทเรียน “การลงทะเบียนผู้ใช้ด้วยอีเมลและรหัสผ่าน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การลงทะเบียนผู้ใช้ด้วยอีเมลและรหัสผ่าน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Supabase Backend as a Service ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การลงทะเบียนผู้ใช้ด้วยอีเมลและรหัสผ่าน”

นำการตรวจสอบสิทธิ์ด้วยอีเมลและรหัสผ่านมาตรฐานมาใช้ รวมถึงฟังก์ชันสมัครบัญชี เข้าสู่ระบบ และรีเซ็ตรหัสผ่าน คุณปฏิบัติ Supabase Backend as a Service ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Supabase Backend as a Service หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Supabase Backend as a Service บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การลงทะเบียนผู้ใช้ด้วยอีเมลและรหัสผ่าน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Supabase Backend as a Service นี้ได้ไหม

ได้ บทเรียน Supabase Backend as a Service ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การลงทะเบียนผู้ใช้ด้วยอีเมลและรหัสผ่าน
  2. การเข้าสู่ระบบผ่านโซเชียล (ผู้ให้บริการ OAuth)
  3. การจัดการเซสชันและโปรไฟล์ผู้ใช้
  4. การรีเซ็ตรหัสผ่านและการยืนยันตัวตนด้วยลิงก์มหัศจรรย์
← กลับไปที่ Supabase Backend as a Service