เชื่อมการสมัครสมาชิกและเข้าสู่ระบบ
สร้างกระบวนการยืนยันตัวตนด้วยคำสั่ง
เชื่อมการสมัครสมาชิกและเข้าสู่ระบบ เป็นบทเรียน Vibe Coding ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vibe Coding และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
From Plan to Code
You've picked a provider. Now you wire it in. The goal of this lesson is a working sign-up form, a login form, and a session that persists across page loads.
Build it in small, testable steps. Prompting for the whole flow at once often produces tangled code you can't debug. One piece at a time keeps you in control.
Install and Configure
First, install the provider's SDK and initialize a client with your keys. The public key goes in client config; the secret stays server-side in environment variables.
Get this base layer working before any UI. A quick test — calling the client and printing the current (empty) user — confirms the connection is alive.
Install my auth provider's SDK and create a single shared client
instance that reads keys from environment variables. Show me how to
test that it connects.Build the Sign-Up Form
Sign-up needs an email field, a password field, and a submit handler that calls the provider's createUser method. Keep the form minimal at first.
On success, the provider returns a new user record. On failure — like a duplicate email — it returns an error you must show to the user. Don't swallow errors silently.
Create a sign-up form with email and password fields that calls my
auth provider's sign-up method, and display any error message it
returns to the user.Validate Before Submitting
Catch bad input before it reaches the provider. Check that the email looks valid and the password meets a minimum length. This gives instant feedback and reduces useless API calls.
Remember client validation is for UX only — never security. The provider and server must validate again, because anyone can bypass the browser.
Add client-side validation to my sign-up form for email format and a
minimum password length of 8, and remind me where server-side checks
still happen.Confirm the Email
Many providers send a verification email after sign-up. Until the user clicks the link, their account is unconfirmed and may have limited access.
Decide your policy: block login until verified, or allow it with a banner reminder. Tell your assistant explicitly, since the default behavior varies by provider.
Configure my provider to send a confirmation email on sign-up and
block login until the email is verified. Show users a clear "check
your inbox" message.Build the Login Form
Login mirrors sign-up: email, password, and a handler that calls signIn. On success the provider issues a session and stores it; on failure you show "invalid credentials."
Keep the error message vague on purpose. Telling an attacker whether the email exists helps them. A single generic message is the safer default.
Create a login form that calls my provider's sign-in method, and on
failure show a single generic "invalid email or password" message
regardless of the cause.Persist the Session
After login, the session must survive page refreshes and new tabs. Providers usually handle this with a cookie or local storage, but you need to load it on app startup.
Add logic that checks for an existing session when the app mounts, so a returning user isn't asked to log in again unnecessarily.
Make my app check for an existing session on startup so a logged-in
user stays signed in after refreshing the page or opening a new tab.Show the Current User
Once a session loads, your UI needs to know who's logged in. A small piece of shared state — often a context or store — holds the current user and updates the whole app.
With this in place, components can react: show a profile menu when logged in, or a login button when not. It's the backbone of a logged-in experience.
Add shared state that exposes the current logged-in user across my
app, and update the header to show a profile menu when signed in and a
login button when not.Add Logout
A logout button calls the provider's signOut method, which clears the session and cookie. Your shared user state should then reset to null, flipping the UI back to the logged-out view.
Test it properly: after logout, refreshing should keep you out and any protected page should reject you. A button that only hides the UI is a bug.
Add a logout button that calls sign-out, clears the session, resets
the current-user state to null, and switches the header back to the
logged-out view.Handle the Errors
Real auth fails in predictable ways: wrong password, unverified email, network timeout, duplicate sign-up. Each deserves a clear, friendly message.
Ask your assistant to map common provider error codes to user-facing text. Good error handling is what separates a demo from a product people trust.
List the common error codes my auth provider returns for sign-up and
login, and map each one to a clear, friendly message for the user.Test the Whole Flow
Before moving on, run the full loop yourself: sign up a new account, verify the email, log out, log back in, refresh, and log out again.
Watching it end to end catches gaps that unit tests miss — like a session that doesn't persist or a logout that leaves a stale cookie. Trust the flow only after you've walked it.
Quick Check
One subtle but important choice in the login form.
Recap
You wired auth in small steps: configure the client, build sign-up with validation and email confirmation, build login with safe error messages, then persist the session and expose the current user.
You added logout, mapped errors to friendly text, and tested the full loop by hand. Next you'll lock down the pages and data only logged-in users should reach.
คำถามที่พบบ่อย
บทเรียน “เชื่อมการสมัครสมาชิกและเข้าสู่ระบบ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เชื่อมการสมัครสมาชิกและเข้าสู่ระบบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vibe Coding ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เชื่อมการสมัครสมาชิกและเข้าสู่ระบบ”
สร้างกระบวนการยืนยันตัวตนด้วยคำสั่ง คุณปฏิบัติ Vibe Coding ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vibe Coding หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vibe Coding บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “เชื่อมการสมัครสมาชิกและเข้าสู่ระบบ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Vibe Coding นี้ได้ไหม
ได้ บทเรียน Vibe Coding ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเข้าสู่ระบบทำงานอย่างไร
- เลือกผู้ให้บริการยืนยันตัวตน
- เชื่อมการสมัครสมาชิกและเข้าสู่ระบบ
- ปกป้องหน้าและข้อมูล