사용자 기반 액세스 제어
인증된 사용자 ID와 역할에 따라 읽기 및 쓰기 액세스를 허용하거나 거부하는 규칙을 구현합니다.
사용자 기반 액세스 제어은(는) CoddyKit의 무료 Firebase Auth & Realtime Database Apps 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Firebase Auth & Realtime Database Apps 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Firebase Auth & Realtime Database Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Control Access by User
Welcome to this lesson! In secure applications, it's crucial to control who can access what data. This is known as User-Based Access Control.
Firebase Realtime Database Security Rules allow you to define precise permissions based on the user who is currently logged in.
Meet the 'auth' Variable
Inside your security rules, Firebase provides a special auth variable. This variable contains information about the currently authenticated user.
auth.uid: The unique ID of the logged-in user.auth.token: An object containing custom claims and other token details (e.g., email).
If no user is logged in, auth will be null.
Authenticated Users Only
The simplest form of user-based access is to ensure only authenticated users can read or write any data.
You can achieve this by checking if the auth variable is not null.
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}Users Read Their Own Data
Often, you want users to only read data that belongs to them. Imagine a /users node where each user has a sub-node with their UID.
We can use a wildcard variable ($uid) in the path to match the current user's ID.
{
"rules": {
"users": {
"$uid": {
".read": "auth.uid == $uid"
}
}
}
}Users Write Their Own Data
Similarly, you can restrict write access so users can only modify their own data. This prevents one user from changing another's profile.
The rule is very similar to the read rule, just applied to .write.
{
"rules": {
"users": {
"$uid": {
".write": "auth.uid == $uid"
}
}
}
}Read & Write Your Own Profile
Let's combine the read and write rules. This common pattern allows users full control over their own specific data node, often used for user profiles.
Here, $userId is a placeholder for an actual user's UID.
{
"rules": {
"profiles": {
"$userId": {
".read": "auth.uid == $userId",
".write": "auth.uid == $userId"
}
}
}
}Post Ownership Example
Consider a 'posts' section where anyone can read posts, but only the creator can edit or delete their own post.
We assume each post object has an ownerId field. We use data.ownerId to refer to the existing owner ID in the database.
{
"rules": {
"posts": {
"$postId": {
".read": "true",
".write": "auth.uid == data.ownerId"
}
}
}
}Validating Data with Auth
Beyond just who can write, you can also validate what data they write. For instance, ensuring that when a user creates an item, they correctly set themselves as the owner.
The newData variable refers to the data being written.
{
"rules": {
"items": {
"$itemId": {
".write": "auth != null",
".validate": "newData.ownerId == auth.uid"
}
}
}
}Introducing User Roles
For more complex access, you can define roles like 'admin' or 'moderator'. These roles are often stored as custom claims in the user's authentication token.
You can then check for these roles in your rules using auth.token.
{
"rules": {
"adminContent": {
".read": "auth.token.isAdmin == true",
".write": "auth.token.isAdmin == true"
}
}
}Quick Check on Access
Consider the following Realtime Database Security Rules:
{
"rules": {
"messages": {
"$messageId": {
".read": "auth.uid == data.senderId",
".write": "auth.uid == data.senderId"
}
}
}
}If user "user123" is authenticated and tries to read a message where data.senderId is "user456", will they succeed?
Recap: User Access Rules
You've learned how to implement powerful user-based access control in Firebase Realtime Database Security Rules!
- The
authvariable provides current user details. - You can restrict access to authenticated users (
auth != null). - Users can be granted read/write access to their own specific data using
auth.uid == $uid. - You can validate incoming data using
newDataandauth.uid. - Roles can be used to grant access to specific user groups.
Next, explore how to validate the data itself!
자주 묻는 질문
“사용자 기반 액세스 제어” 강의는 무료인가요?
네 — “사용자 기반 액세스 제어” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Firebase Auth & Realtime Database Apps 강의 전체를 잠금 해제할 수 있습니다. Firebase Auth & Realtime Database Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
“사용자 기반 액세스 제어”에서 뭘 배우나요?
인증된 사용자 ID와 역할에 따라 읽기 및 쓰기 액세스를 허용하거나 거부하는 규칙을 구현합니다. 브라우저에서 직접 실행하는 실습 코드로 Firebase Auth & Realtime Database Apps을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Firebase Auth & Realtime Database Apps을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Firebase Auth & Realtime Database Apps은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“사용자 기반 액세스 제어” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Firebase Auth & Realtime Database Apps 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Firebase Auth & Realtime Database Apps 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 보안 규칙 문법 이해하기
- 사용자 기반 액세스 제어
- 규칙을 사용한 데이터 검증
- 보안 규칙 테스트 및 디버깅