ユーザーデータのロールベースアクセス
Firebase AuthのロールとRealtime Databaseのルールを組み合わせ、管理者、メンバー、ゲストに共有データと個人データへの異なるアクセス権を付与します。
「ユーザーデータのロールベースアクセス」はCoddyKit上の無料Firebase Auth & Realtime Database Appsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFirebase Auth & Realtime Database Apps学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Firebase Auth & Realtime Database Appsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Beyond Owner-Only Access
So far each user reads and writes their own data. Real apps need roles: an admin who moderates content, members who collaborate, and guests with read-only access.
Role-based access control (RBAC) layers permissions on top of authentication.
Where Roles Live
You can store a user's role in two places:
- A
rolesnode in the database, read inside rules - A custom claim on the auth token (set server-side)
Custom claims are faster to check; database roles are easier to change at runtime.
Roles in the Database
A simple model maps each uid to a role string. This data itself must be locked down so users cannot promote themselves.
{
"roles": {
"uid_alice": "admin",
"uid_bob": "member"
}
}Checking a Database Role in Rules
Rules can read other parts of the database with root. Here only admins may write to a shared config node.
{
"rules": {
"config": {
".write": "root.child('roles').child(auth.uid).val() === 'admin'"
}
}
}Custom Claims for Roles
With the Admin SDK you can attach a role to the token itself. This is checked without an extra database read.
await admin.auth().setCustomUserClaims(uid, { role: 'admin' });Checking Claims in Rules
Custom claims appear under auth.token. The rule becomes simpler and avoids a root lookup.
{
"rules": {
"config": {
".write": "auth.token.role === 'admin'"
}
}
}Reading the Claim Client-Side
The client can read its own claims to adjust the UI, for example showing an admin panel only to admins.
import { getAuth, getIdTokenResult } from 'firebase/auth';
const res = await getIdTokenResult(getAuth().currentUser);
if (res.claims.role === 'admin') showAdminPanel();Tiered Read Access
Different roles can have different read scopes. Members read shared docs; guests read only public ones.
{
"rules": {
"shared": {
".read": "auth.token.role === 'member' || auth.token.role === 'admin'"
}
}
}Protecting the Role Data Itself
Critically, users must not be able to edit their own role. Make the roles node writable only by admins (or only server-side), or self-escalation defeats the whole system.
{
"rules": {
"roles": {
".write": "auth.token.role === 'admin'"
}
}
}Claim Propagation Delay
After you change a custom claim, the user's existing token still has the old value until it refreshes (about an hour, or on forced refresh). Call getIdToken(true) client-side to pick up new roles immediately.
await getAuth().currentUser.getIdToken(true);Choosing an Approach
Use custom claims for stable, security-critical roles, and database roles when permissions change often or need to be queried. Many apps combine both.
Quick Check
Test your understanding of role-based access.
Recap
You can now grant tiered access by role.
- Store roles in the database or as custom claims
- Check database roles via
root, claims viaauth.token - Give roles different read/write scopes
- Lock down the role data so users cannot self-promote
- Refresh tokens to pick up new claims promptly
AI チューターと学ぶ Firebase Auth & Realtime Database Apps — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 11
- レッスン
- 44
よくある質問
「ユーザーデータのロールベースアクセス」レッスンは無料ですか?
はい。「ユーザーデータのロールベースアクセス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Firebase Auth & Realtime Database Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Firebase Auth & Realtime Database Appsコースには全4レッスンが含まれています。
「ユーザーデータのロールベースアクセス」で何を学びますか?
Firebase AuthのロールとRealtime Databaseのルールを組み合わせ、管理者、メンバー、ゲストに共有データと個人データへの異なるアクセス権を付与します。 ブラウザで直接実行するハンズオンコードでFirebase Auth & Realtime Database Appsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Firebase Auth & Realtime Database Appsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFirebase Auth & Realtime Database Appsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「ユーザーデータのロールベースアクセス」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFirebase Auth & Realtime Database Appsレッスンでコードを書いて実行できますか?
はい。すべてのFirebase Auth & Realtime Database Appsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ユーザーデータとAuthの連携
- リアルタイムなユーザープロフィール
- 共同データ編集
- ユーザーデータのロールベースアクセス