페이지와 데이터 보호하기
로그인 뒤에 경로를 보호해 보세요.
페이지와 데이터 보호하기은(는) CoddyKit의 무료 Vibe Coding 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Vibe Coding 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Vibe Coding 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Login Isn't Enough
Having a working login is only half the job. The other half is making sure that logged-out visitors can't reach private pages and that users can't touch data that isn't theirs.
This is where authentication meets authorization. A polished login form means nothing if the dashboard URL works for anyone who types it in.
Guard the Frontend Routes
Start by hiding private pages from logged-out users. A route guard checks for a session before rendering and redirects to login if none exists.
This is a UX layer, not real security — a determined user can bypass the browser. But it stops accidental access and gives a clean, expected experience.
Add a route guard to my dashboard pages that redirects users without a
session to the login page, and remind me this is UX only, not real
security.The Real Wall Is the Server
Frontend guards can be bypassed by anyone calling your API directly. The only protection that counts runs on the server, where you re-check the session on every request.
Whenever you prompt for a protected feature, insist the check happens server-side. "Hide the button" is never the same as "reject the request."
Add server-side authentication checks to every API route that returns
or changes private data, so requests without a valid session are
rejected with a 401.Middleware Does the Checking
Rather than copy the same check into every route, put it in middleware that runs first. It reads the session from the cookie or token, confirms it's valid, and attaches the user to the request.
Routes downstream can then trust that a user exists. Centralizing the check means one place to fix if something's wrong.
Create middleware that verifies the session on incoming requests,
attaches the user object to the request, and returns 401 if the
session is missing or expired.Authorization: Whose Data Is It?
Being logged in isn't permission to see everything. A user should only read and edit their own records. This ownership check is authorization.
A classic flaw: an endpoint loads a record by ID without checking it belongs to the requester. Anyone can then read others' data by changing the ID. Always verify ownership.
Audit my API routes for missing ownership checks. For each route that
loads a record by ID, confirm it belongs to the logged-in user before
returning it.Roles and Permissions
Some apps need more than "owner." An admin might manage all users; an editor might publish content. Roles group these permissions so checks stay simple.
Store the role on the user record and check it where needed. Keep roles coarse at first — over-engineering permissions early slows you down without benefit.
Add a simple role field to my user model with "user" and "admin"
values, and protect the admin dashboard so only admins can reach it on
the server.Protect at the Database Layer
Some providers, like Supabase, let you enforce access rules right in the database with row-level security. A policy says "a user can only select rows where the owner matches their ID."
This is powerful because even a buggy API can't leak data the database refuses to return. It's defense in depth, not a replacement for server checks.
Write row-level security policies so each user can only select, update,
and delete rows in my table where the owner column equals their user
ID.Never Trust the Client
The golden rule of protection: assume every request could be forged. IDs in the URL, fields in the body, even the user ID can be tampered with by a malicious client.
Always derive the acting user from the verified session on the server, never from a value the client sent. This single habit prevents a whole class of breaches.
Refactor my routes so the acting user's ID always comes from the
verified session on the server, never from a value passed in the
request body or URL.Handle Expired Sessions Gracefully
Sessions expire, and when they do, a protected request fails. Without handling, the user sees a cryptic error or a blank screen.
Catch the 401, clear the local session state, and redirect to login with a friendly "please sign in again" message. Smooth expiry handling makes the app feel reliable.
When an API request returns 401 because the session expired, clear the
local user state and redirect to login with a "your session expired,
please sign in again" message.Test Like an Attacker
Verify your protection by trying to break it. Open a private API route while logged out. Change a record ID to one you don't own. Log in as a normal user and hit an admin route.
Each attempt should be cleanly rejected. If any succeeds, you've found a hole before a real attacker does.
Give me a checklist of requests to send to test my auth, including
hitting protected routes logged out and accessing another user's
record by changing the ID.Layered by Design
Good protection is layered: frontend guards for UX, server middleware for the real gate, ownership and role checks for authorization, and database policies as a last line.
No single layer is enough alone, but together they make your app safe even when one layer has a bug. Build them up deliberately as your app grows.
Quick Check
The most important place to enforce access.
Recap
You learned that login alone doesn't protect anything. Frontend guards improve UX, but server middleware is the real gate, re-checking the session on every request.
You added ownership and role checks for authorization, optional database policies for defense in depth, and the rule to never trust client-supplied identity. Test like an attacker, and your auth-by-prompt app stands up to abuse.
자주 묻는 질문
“페이지와 데이터 보호하기” 강의는 무료인가요?
네 — “페이지와 데이터 보호하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Vibe Coding 강의 전체를 잠금 해제할 수 있습니다. Vibe Coding 강의에는 총 4개의 강의가 포함되어 있습니다.
“페이지와 데이터 보호하기”에서 뭘 배우나요?
로그인 뒤에 경로를 보호해 보세요. 브라우저에서 직접 실행하는 실습 코드로 Vibe Coding을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Vibe Coding을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Vibe Coding은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“페이지와 데이터 보호하기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Vibe Coding 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Vibe Coding 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 로그인이 작동하는 방식
- 인증 제공자 선택하기
- 회원가입과 로그인 연결하기
- 페이지와 데이터 보호하기