บัญชีผู้ใช้และการเข้าสู่ระบบ
เพิ่มการสมัครสมาชิกและการยืนยันตัวตน
บัญชีผู้ใช้และการเข้าสู่ระบบ เป็นบทเรียน Vibe Coding ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vibe Coding และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Who Is Using Your App?
Your app stores data, but right now everyone shares the same data. A real product needs to know who each person is so each freelancer sees only their own clients and hours. That's what authentication (login) gives you.
Two related ideas: authentication answers "who are you?" and authorization answers "what are you allowed to do?" In this lesson you'll add both to your full-stack app with AI's help.
Don't Build Auth From Scratch
Authentication is security-critical and easy to get dangerously wrong. The pro move — even for experienced developers — is to use a trusted auth library or service rather than rolling your own.
Popular, AI-friendly options include Clerk, Supabase Auth, Auth0 and NextAuth/Auth.js. Let AI recommend one for your stack.
I'm adding login to my Next.js freelancer hours app. I do NOT want to build authentication from scratch.
Recommend ONE beginner-friendly auth solution (e.g. Clerk, Supabase Auth, or Auth.js). For your pick, explain:
- why it's safe and easy
- what sign-in methods it supports (email, Google, etc.)
- one sentence on how it fits Next.jsHow Login Actually Works
The basic flow: a user enters credentials (or signs in with Google), the auth service verifies them and issues a session — a token that proves who they are on every later request.
Your app checks that session to decide what to show. You don't store raw passwords yourself; the auth service handles the scary parts.
Explain how the login flow works in plain language for my app, using the auth tool you recommended.
Walk through: user signs up, signs in, gets a session, and how my app uses that session on later requests to know who they are. Mention why I never store raw passwords myself.Add Sign-Up and Sign-In Screens
Start with the visible part: pages where users can create an account and log in. Modern auth libraries provide ready-made components, so this is often just a few lines.
Ask AI to add the sign-up/sign-in UI and wire it into your app's navigation.
Set up sign-up and sign-in for my Next.js app using the auth tool we chose.
- add the sign-in and sign-up pages/components
- support email/password and Google sign-in
- add a 'Sign out' button in the header when logged in
Show me each file to create or edit and any environment variables I need. Keep secrets out of the code.Protect Your Pages and Endpoints
A logged-out visitor shouldn't reach the dashboard or hit your data endpoints. Protecting routes means checking for a valid session and redirecting or rejecting if it's missing.
Ask AI to guard both your frontend pages and your API endpoints — both halves need protection, not just the screen.
Protect my app so only logged-in users can use it.
- redirect logged-out users away from the dashboard page to the sign-in page
- on the API side, reject requests to /api/entries that have no valid session, returning a 401
Show me the middleware or guards needed and explain why protecting the API matters even if the page is already protected.A Tiny Session Check
At its core, route protection is a simple check: is there a valid session? Here's the idea in plain JavaScript — a guard that decides whether to allow or block a request. Run it to see both cases.
function handleRequest(session) {
if (!session || !session.userId) {
return { status: 401, body: "Please sign in" };
}
return { status: 200, body: "Welcome, user " + session.userId };
}
console.log(handleRequest(null));
console.log(handleRequest({ userId: 42 }));Tie Data to the Logged-In User
Now the key step: each time entry must belong to a user, so people see only their own data. That means adding a userId to your records and filtering every query by the current user.
Ask AI to update your schema and endpoints so data is scoped to whoever is logged in.
Make my data per-user. Each TimeEntry and Client should belong to the logged-in user.
- add a userId field tied to the authenticated user
- when creating an entry, attach the current user's id
- when listing entries, only return rows belonging to the current user
Update the Prisma schema and the /api/entries endpoints. Give me the migration command too.Authorization: Who Can Do What
Being logged in isn't enough — you must ensure a user can only touch their own records. Without this, anyone could edit someone else's entry by guessing an id. That's an authorization check, and it's a common AI blind spot.
Explicitly ask AI to verify ownership on update and delete.
Add ownership checks to my update and delete endpoints.
Before updating or deleting a TimeEntry, confirm it belongs to the currently logged-in user. If it belongs to someone else, return 403 Forbidden — never modify it.
Show the updated PUT and DELETE routes and explain why checking the session alone is not enough.Show the User They're Logged In
Good apps make auth feel friendly: show the user's name or avatar, a clear "Sign out" button, and helpful states for loading and errors. This polish builds trust.
Ask AI to surface the logged-in user's info and handle the in-between states gracefully.
Polish the auth experience in my app.
- show the logged-in user's name and avatar in the header
- add a clean 'Sign out' action
- show a loading state while the session is being checked, so the UI doesn't flicker
- if sign-in fails, show a friendly message
Show me the component changes and keep the styling minimal.Test the Whole Auth Flow
Before calling it done, walk the full journey: sign up, sign out, sign back in, add data, then sign in as a different user and confirm you see none of the first user's data. That proves both authentication and authorization work.
If something leaks across users, describe the exact steps to AI — it's a precise way to find the gap.
Help me test my app's auth end to end. Give me a step-by-step test checklist that includes:
- sign up, sign out, sign back in
- adding data as User A
- signing in as User B and confirming User B canNOT see User A's data
- trying to edit User A's entry while signed in as User B (should be blocked)
Tell me what correct behavior looks like at each step.A Complete Full-Stack App
You did it: a frontend, a backend, a real database, and now secure user accounts — a genuinely full-stack app, built almost entirely by talking to AI. Each freelancer signs in and sees only their own clients and hours.
This is the foundation real products are built on. From here you can deploy it, add payments, or grow features — all with the same vibe-coding workflow you've practiced.
Quick Check
Lock in the security mindset.
Recap: Accounts and Login
You secured your full-stack app:
- Authentication identifies the user; authorization controls what they can do
- Use a trusted auth service (Clerk, Supabase Auth, Auth.js) — never roll your own
- Protect both pages and API endpoints with session checks
- Scope data per user with a userId and filter every query
- Verify ownership before update/delete, and test across multiple users
Congratulations — you've built a complete full-stack app by talking to AI. Next courses cover deploying, payments and shipping it to real users!
คำถามที่พบบ่อย
บทเรียน “บัญชีผู้ใช้และการเข้าสู่ระบบ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “บัญชีผู้ใช้และการเข้าสู่ระบบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vibe Coding ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “บัญชีผู้ใช้และการเข้าสู่ระบบ”
เพิ่มการสมัครสมาชิกและการยืนยันตัวตน คุณปฏิบัติ Vibe Coding ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vibe Coding หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vibe Coding บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “บัญชีผู้ใช้และการเข้าสู่ระบบ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Vibe Coding นี้ได้ไหม
ได้ บทเรียน Vibe Coding ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การวางแผนแอปด้วยปัญญาประดิษฐ์
- ส่วนหน้าและส่วนหลังร่วมกัน
- การเพิ่มฐานข้อมูล
- บัญชีผู้ใช้และการเข้าสู่ระบบ