কাস্টম দাবি ও নিরাপত্তা নিয়মের মাধ্যমে অ্যাক্সেস নিয়ন্ত্রণ করা

Firebase Admin SDK ব্যবহারকারীর অ্যাকাউন্টে কাস্টম অ্যাট্রিবিউট নির্ধারণ করার সুবিধা দেয়। এটি Firebase অ্যাপে রোল-বেসড অ্যাক্সেস কন্ট্রোল সহ বিভিন্ন অ্যাক্সেস কন্ট্রোল স্ট্র্যাটেজি প্রয়োগ করার সুবিধা প্রদান করে। এইসব কাস্টম অ্যাট্রিবিউট ব্যবহারকারীদের বিভিন্ন লেভেলের অ্যাক্সেস (ভূমিকা) দিতে পারে, যা কোনও অ্যাপ্লিকেশনের নিরাপত্তা সংক্রান্ত নিয়মে এনফোর্স করা হয়।

নিম্নলিখিত সাধারণ ক্ষেত্রে ব্যবহারকারীর ভূমিকা নির্ধারণ করা যেতে পারে:

  • ডেটা ও রিসোর্স অ্যাক্সেস করার জন্য কোনও ব্যবহারকারীকে অ্যাডমিনিস্ট্রেটরের বিশেষ সুবিধা দেওয়া।
  • ব্যবহারকারী যে বিভিন্ন গ্রুপের অন্তর্গত তা নির্ধারণ করা।
  • মাল্টি-লেভেল অ্যাক্সেস প্রদান করা:
    • পেড/আনপেড সাবস্ক্রাইবারদের আলাদা করা।
    • সাধারণ ব্যবহারকারীদের থেকে মডারেটরদের আলাদা করা।
    • শিক্ষক/শিক্ষার্থীর আবেদন ইত্যাদি।
  • ব্যবহারকারীর জন্য অতিরিক্ত শনাক্তকারী যোগ করুন। যেমন, Firebase ব্যবহারকারী অন্য সিস্টেমে অন্য UID-তে ম্যাপ করতে পারেন।

ধরা যাক, আপনি "adminContent" ডেটাবেস নোডে অ্যাক্সেস সীমিত করতে চান। আপনি অ্যাডমিন ব্যবহারকারীদের তালিকায় ডেটাবেস লুকআপের মাধ্যমে এটি করতে পারেন। তবে, আপনি নিম্নলিখিত Realtime Database নিয়ম সহ admin নামের কাস্টম ব্যবহারকারী দাবি ব্যবহার করে আরও দক্ষতার সাথে একই উদ্দেশ্য পূরণ করতে পারবেন:

{
  "rules": {
    "adminContent": {
      ".read": "auth.token.admin === true",
      ".write": "auth.token.admin === true",
    }
  }
}

ব্যবহারকারীর কাস্টম দাবি ব্যবহারকারীর প্রমাণীকরণ টোকেনের মাধ্যমে অ্যাক্সেসযোগ্য। উপরের উদাহরণে, শুধুমাত্র সেইসব ব্যবহারকারী যাদের টোকেন দাবিতে admin-এর মান true হিসেবে সেট করা আছে, তাদের adminContent নোডে রিড/রাইট অ্যাক্সেস থাকবে। যেহেতু আইডি টোকেনে আগে থেকেই এইসব অ্যাসার্শন থাকে, তাই অ্যাডমিনের অনুমতি চেক করার জন্য অতিরিক্ত প্রসেসিং বা লুকআপের প্রয়োজন হয় না। এছাড়াও, এই কাস্টম দাবি ডেলিভার করার জন্য ID টোকেন হল একটি বিশ্বস্ত মেকানিজম। যাচাই করা সমস্ত অ্যাক্সেসকে অবশ্যই সংশ্লিষ্ট অনুরোধ প্রসেস করার আগে আইডি টোকেন যাচাই করতে হবে।

এই পৃষ্ঠায় বর্ণিত কোডের উদাহরণ ও সমাধান, ক্লায়েন্ট-সাইড Firebase Auth API এবং অ্যাডমিন SDK-এর প্রদান করা সার্ভার-সাইড Auth API, উভয় থেকেই নেওয়া হয়েছে।

অ্যাডমিন SDK-এর মাধ্যমে কাস্টম ব্যবহারকারীর দাবি সেট ও যাচাই করুন

কাস্টম দাবিতে সংবেদনশীল ডেটা থাকতে পারে, তাই সেগুলি শুধুমাত্র সেট করা উচিত Firebase অ্যাডমিন SDK দ্বারা একটি বিশেষ সুবিধাপ্রাপ্ত সার্ভার পরিবেশ থেকে।

Node.js

// Set admin privilege on the user corresponding to uid.

getAuth()
  .setCustomUserClaims(uid, { admin: true })
  .then(() => {
    // The new custom claims will propagate to the user's ID token the
    // next time a new one is issued.
  });

জাভা

// Set admin privilege on the user corresponding to uid.
Map<String, Object> claims = new HashMap<>();
claims.put("admin", true);
FirebaseAuth.getInstance().setCustomUserClaims(uid, claims);
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.

Python

# Set admin privilege on the user corresponding to uid.
auth.set_custom_user_claims(uid, {'admin': True})
# The new custom claims will propagate to the user's ID token the
# next time a new one is issued.

Go

// Get an auth client from the firebase.App
client, err := app.Auth(ctx)
if err != nil {
	log.Fatalf("error getting Auth client: %v\n", err)
}

// Set admin privilege on the user corresponding to uid.
claims := map[string]interface{}{"admin": true}
err = client.SetCustomUserClaims(ctx, uid, claims)
if err != nil {
	log.Fatalf("error setting custom claims %v\n", err)
}
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.

C#

// Set admin privileges on the user corresponding to uid.
var claims = new Dictionary<string, object>()
{
    { "admin", true },
};
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(uid, claims);
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.

কাস্টম ক্লেম অবজেক্টে কোনও OIDC রিজার্ভ করা কী নাম বা Firebase রিজার্ভ করা নাম থাকলে চলবে না। পে-লোড ১০০০ বাইটের বেশি হলে চলবে না। কাস্টম দাবি JSON-সিরিয়ালাইজেবল হতে হবে। যেসব ধরন কাজ করে তার মধ্যে স্ট্রিং, সংখ্যা, বুলিয়ান, অ্যারে, অবজেক্ট ও ফাঁকা অন্তর্ভুক্ত। তারিখ, নির্দিষ্ট করা নেই, ফাংশন বা অন্যান্য JSON নয় এমন ভ্যালুর মতো কাজ করে না এমন ধরনের ডেটা সমস্যা সৃষ্টি করবে।

ব্যাকএন্ড সার্ভারে পাঠানো একটি আইডি টোকেন ব্যবহারকারীর পরিচয় এবং অ্যাক্সেস লেভেল কনফার্ম করতে পারে অ্যাডমিন SDK ব্যবহার করে নিম্নলিখিত উপায়ে:

Node.js

// Verify the ID token first.
getAuth()
  .verifyIdToken(idToken)
  .then((claims) => {
    if (claims.admin === true) {
      // Allow access to requested admin resource.
    }
  });

জাভা

// Verify the ID token first.
FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdToken(idToken);
if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) {
  // Allow access to requested admin resource.
}

Python

# Verify the ID token first.
claims = auth.verify_id_token(id_token)
if claims['admin'] is True:
    # Allow access to requested admin resource.
    pass

Go

// Verify the ID token first.
token, err := client.VerifyIDToken(ctx, idToken)
if err != nil {
	log.Fatal(err)
}

claims := token.Claims
if admin, ok := claims["admin"]; ok {
	if admin.(bool) {
		//Allow access to requested admin resource.
	}
}

C#

// Verify the ID token first.
FirebaseToken decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken);
object isAdmin;
if (decoded.Claims.TryGetValue("admin", out isAdmin))
{
    if ((bool)isAdmin)
    {
        // Allow access to requested admin resource.
    }
}

এছাড়াও, আপনি ব্যবহারকারীর আগে থেকে থাকা কাস্টম দাবি চেক করতে পারবেন, যা ব্যবহারকারী অবজেক্টে প্রপার্টি হিসেবে উপলভ্য:

Node.js

// Lookup the user associated with the specified uid.
getAuth()
  .getUser(uid)
  .then((userRecord) => {
    // The claims can be accessed on the user record.
    console.log(userRecord.customClaims['admin']);
  });

জাভা

// Lookup the user associated with the specified uid.
UserRecord user = FirebaseAuth.getInstance().getUser(uid);
System.out.println(user.getCustomClaims().get("admin"));

Python

# Lookup the user associated with the specified uid.
user = auth.get_user(uid)
# The claims can be accessed on the user record.
print(user.custom_claims.get('admin'))

Go

// Lookup the user associated with the specified uid.
user, err := client.GetUser(ctx, uid)
if err != nil {
	log.Fatal(err)
}
// The claims can be accessed on the user record.
if admin, ok := user.CustomClaims["admin"]; ok {
	if admin.(bool) {
		log.Println(admin)
	}
}

C#

// Lookup the user associated with the specified uid.
UserRecord user = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
Console.WriteLine(user.CustomClaims["admin"]);

customClaims-এর জন্য নাল পাস করে আপনি ব্যবহারকারীর কাস্টম দাবি মুছে দিতে পারবেন।

ক্লায়েন্টের কাছে কাস্টম দাবি প্রচার করুন

অ্যাডমিন SDK-এর মাধ্যমে কোনও ব্যবহারকারীর উপর নতুন দাবি পরিবর্তন করার পরে, সেগুলি নিম্নলিখিত উপায়ে আইডি টোকেনের মাধ্যমে ক্লায়েন্ট সাইডে একজন যাচাইকৃত ব্যবহারকারীর কাছে প্রেরণ করা হয়:

  • কাস্টম দাবি পরিবর্তন করার পরে কোনও ব্যবহারকারী সাইন-ইন বা আবার যাচাই করেন। এর ফলে ইস্যু করা আইডি টোকেনে লেটেস্ট দাবি থাকবে।
  • পুরনো টোকেনের মেয়াদ শেষ হয়ে যাওয়ার পরে, আগে থেকে থাকা ব্যবহারকারীর সেশন তার আইডি টোকেন রিফ্রেশ করে নেয়।
  • currentUser.getIdToken(true)-এ কল করার মাধ্যমে একটি আইডি টোকেন জোরপূর্বক রিফ্রেশ করা হয়।

ক্লায়েন্টে কাস্টম দাবি অ্যাক্সেস করুন

কাস্টম দাবি শুধুমাত্র ব্যবহারকারীর আইডি টোকেনের মাধ্যমে পুনরুদ্ধার করা যেতে পারে। ব্যবহারকারীর ভূমিকা বা অ্যাক্সেস লেভেলের উপর ভিত্তি করে ক্লায়েন্ট UI পরিবর্তন করার জন্য এইসব দাবিতে অ্যাক্সেস প্রয়োজন হতে পারে। তবে, ব্যাকএন্ড অ্যাক্সেস সবসময় ID টোকেনের মাধ্যমে এনফোর্স করা উচিত, টোকেনটি যাচাই করার পরে এবং এর দাবিগুলি পার্স করার পরে। কাস্টম দাবি সরাসরি ব্যাকএন্ডে পাঠানো উচিত নয়, কারণ টোকেনের বাইরে সেগুলি বিশ্বাসযোগ্য নয়।

লেটেস্ট দাবিগুলি ব্যবহারকারীর আইডি টোকেনে প্রোপাগেট হয়ে গেলে, আপনি আইডি টোকেন ফিরিয়ে আনার মাধ্যমে সেগুলি পেতে পারেন:

জাভাস্ক্রিপ্ট

import { getAuth } from "firebase/auth";
getAuth().currentUser?.getIdTokenResult()
  .then((idTokenResult) => {
     // Confirm the user is an Admin.
     if (!!idTokenResult.claims.admin) {
       // Show admin UI.
       showAdminUI();
     } else {
       // Show regular user UI.
       showRegularUI();
     }
  })
  .catch((error) => {
    console.log(error);
  });

Android

user.getIdToken(false).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
  @Override
  public void onSuccess(GetTokenResult result) {
    boolean isAdmin = result.getClaims().get("admin");
    if (isAdmin) {
      // Show admin UI.
      showAdminUI();
    } else {
      // Show regular user UI.
      showRegularUI();
    }
  }
});

Swift

user.getIDTokenResult(completion: { (result, error) in
  guard let admin = result?.claims?["admin"] as? NSNumber else {
    // Show regular user UI.
    showRegularUI()
    return
  }
  if admin.boolValue {
    // Show admin UI.
    showAdminUI()
  } else {
    // Show regular user UI.
    showRegularUI()
  }
})

Objective-C

user.getIDTokenResultWithCompletion:^(FIRAuthTokenResult *result,
                                      NSError *error) {
  if (error != nil) {
    BOOL *admin = [result.claims[@"admin"] boolValue];
    if (admin) {
      // Show admin UI.
      [self showAdminUI];
    } else {
      // Show regular user UI.
      [self showRegularUI];
    }
  }
}];

কাস্টম দাবির জন্য পেশাদার পদ্ধতি

কাস্টম দাবি শুধুমাত্র অ্যাক্সেস কন্ট্রোল প্রদান করতে ব্যবহার করা হয়। এগুলি অতিরিক্ত ডেটা (যেমন প্রোফাইল এবং অন্যান্য কাস্টম ডেটা) স্টোর করার জন্য ডিজাইন করা হয়নি। এটি করার জন্য এটি একটি সুবিধাজনক পদ্ধতি বলে মনে হতে পারে, তবে এটি করার জন্য দৃঢ়ভাবে নিরুৎসাহিত করা হয় কারণ এই দাবিগুলি আইডি টোকেনে স্টোর করা হয় এবং পারফর্ম্যান্স সংক্রান্ত সমস্যা সৃষ্টি করতে পারে কারণ সমস্ত যাচাই করা অনুরোধে সর্বদা সাইন-ইন করা ব্যবহারকারীর সাথে সম্পর্কিত একটি Firebase ID টোকেন থাকে।

  • শুধুমাত্র ব্যবহারকারীর অ্যাক্সেস নিয়ন্ত্রণ করার জন্য ডেটা স্টোর করতে কাস্টম দাবি ব্যবহার করুন। অন্যান্য সব ডেটা রিয়েল-টাইম ডেটাবেস বা অন্য সার্ভার সাইড স্টোরেজের মাধ্যমে আলাদাভাবে স্টোর করতে হবে।
  • কাস্টম দাবির সাইজ সীমিত। ১০০০ বাইটের বেশি কাস্টম ক্লেম পেলোড পাস করলে সমস্যা হবে।

উদাহরণ ও ব্যবহারিক ক্ষেত্র

নিম্নলিখিত উদাহরণগুলি নির্দিষ্ট Firebase ব্যবহারের ক্ষেত্রে কাস্টম দাবির ব্যাখ্যা করে।

ব্যবহারকারী তৈরি করার সময় Firebase ফাংশনের মাধ্যমে ভূমিকা নির্ধারণ করা

এই উদাহরণে, Cloud Functions ব্যবহার করে তৈরি করার সময় ব্যবহারকারীর উপর কাস্টম দাবি সেট করা হয়।

Cloud Functions ব্যবহার করে কাস্টম দাবি যোগ করা যেতে পারে এবং Realtime Database-এর সাথে অবিলম্বে প্রচার করা যেতে পারে। onCreate ট্রিগার ব্যবহার করে সাইন-আপ করার সময় শুধুমাত্র ফাংশন কল করা হয়। কাস্টম দাবি সেট করা হয়ে গেলে, সেগুলি আগে থেকে থাকা এবং ভবিষ্যতের সব সেশনে প্রয়োগ করা হয়। পরের বার ব্যবহারকারী নিজের ক্রেডেনশিয়াল দিয়ে সাইন-ইন করলে, টোকেনে কাস্টম দাবি থাকে।

ক্লায়েন্ট সাইড ইমপ্লিমেন্টেশন (জাভাস্ক্রিপ্ট)

import { GoogleAuthProvider, signInWithPopup, getAuth, onAuthStateChanged } from "firebase/auth";
import { getDatabase, onValue, ref } from "firebase/database";

const auth = getAuth();
const database = getDatabase();

const provider = new GoogleAuthProvider();

signInWithPopup(auth, provider).catch(error => {
  console.log(error);
});

let unsubscribeFn = null;
let metadataRef = null;
onAuthStateChanged(auth, user => {
  // Remove previous listener.
  if (unsubscribeFn) {
    unsubscribeFn();
  }
  // On user login add new listener.
  if (user) {
    // Check if refresh is required.
    metadataRef = ref(database, 'metadata/' + user.uid + '/refreshTime');
    // Subscribe new listener to changes on that node.
    unsubscribeFn = onValue(metadataRef, async (snapshot) => {
      // Force refresh to pick up the latest custom claims changes.
      // Note this is always triggered on first call. Further optimization could be
      // added to avoid the initial trigger when the token is issued and already contains
      // the latest claims.
      user.getIdToken(true);
    });
  }
});

Cloud Functions লজিক

যাচাইকৃত ব্যবহারকারীর জন্য রিড/রাইট সীমাবদ্ধতা সহ একটি নতুন ডেটাবেস নোড (metadata/($uid)} যোগ করা হয়েছে।

const functions = require('firebase-functions');
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');
const { getDatabase } = require('firebase-admin/database');

initializeApp();

// On sign up.
exports.processSignUp = functions.auth.user().onCreate(async (user) => {
  // Check if user meets role criteria.
  if (
    user.email &&
    user.email.endsWith('@admin.example.com') &&
    user.emailVerified
  ) {
    const customClaims = {
      admin: true,
      accessLevel: 9
    };

    try {
      // Set custom user claims on this newly created user.
      await getAuth().setCustomUserClaims(user.uid, customClaims);

      // Update real-time database to notify client to force refresh.
      const metadataRef = getDatabase().ref('metadata/' + user.uid);

      // Set the refresh time to the current UTC timestamp.
      // This will be captured on the client to force a token refresh.
      await  metadataRef.set({refreshTime: new Date().getTime()});
    } catch (error) {
      console.log(error);
    }
  }
});

ডেটাবেস সংক্রান্ত নিয়ম

{
  "rules": {
    "metadata": {
      "$user_id": {
        // Read access only granted to the authenticated user.
        ".read": "$user_id === auth.uid",
        // Write access only via Admin SDK.
        ".write": false
      }
    }
  }
}

HTTP অনুরোধের মাধ্যমে ভূমিকা নির্ধারণ করা

নিম্নলিখিত উদাহরণটি HTTP অনুরোধের মাধ্যমে নতুন সাইন-ইন করা ব্যবহারকারীর উপর কাস্টম ব্যবহারকারীর দাবি সেট করে।

ক্লায়েন্ট সাইড ইমপ্লিমেন্টেশন (জাভাস্ক্রিপ্ট)

import { GoogleAuthProvider, signInWithPopup, getAuth, onAuthStateChanged } from "firebase/auth";
import { getDatabase, onValue, ref } from "firebase/database";

const auth = getAuth();
const database = getDatabase();

const provider = new GoogleAuthProvider();

signInWithPopup(auth, provider)
.then((result) => {
  // User is signed in. Get the ID token.
  return result.user.getIdToken();
})
.then((idToken) => {
  // Pass the ID token to the server.
  $.post(
    '/setCustomClaims',
    {
      idToken: idToken
    },
    (data, status) => {
      // This is not required. You could just wait until the token is expired
      // and it proactively refreshes.
      if (status == 'success' && data) {
        const json = JSON.parse(data);
        if (json && json.status == 'success') {
          // Force token refresh. The token claims will contain the additional claims.
          auth.currentUser.getIdToken(true);
        }
      }
    });
}).catch((error) => {
  console.log(error);
});

ব্যাকএন্ড ইমপ্লিমেন্টেশন (অ্যাডমিন SDK)

app.post('/setCustomClaims', async (req, res) => {
  // Get the ID token passed.
  const idToken = req.body.idToken;

  // Verify the ID token and decode its payload.
  const claims = await getAuth().verifyIdToken(idToken);

  // Verify user is eligible for additional privileges.
  if (
    typeof claims.email !== 'undefined' &&
    typeof claims.email_verified !== 'undefined' &&
    claims.email_verified &&
    claims.email.endsWith('@admin.example.com')
  ) {
    // Add custom claims for additional privileges.
    await getAuth().setCustomUserClaims(claims.sub, {
      admin: true
    });

    // Tell client to refresh token on user.
    res.end(JSON.stringify({
      status: 'success'
    }));
  } else {
    // Return nothing.
    res.end(JSON.stringify({ status: 'ineligible' }));
  }
});

আগে থেকেই আছেন এমন কোনও ব্যবহারকারীর অ্যাক্সেস লেভেল আপগ্রেড করার সময় একই ফ্লো ব্যবহার করা যেতে পারে। যেমন, কোনও ফ্রি ব্যবহারকারী পেড সাবস্ক্রিপশনে আপগ্রেড করছেন। ব্যবহারকারীর আইডি টোকেন HTTP অনুরোধের মাধ্যমে ব্যাকএন্ড সার্ভারে পেমেন্টের তথ্যের সাথে পাঠানো হয়। পেমেন্ট সফলভাবে প্রসেস করা হলে, ব্যবহারকারীকে অ্যাডমিন SDK-এর মাধ্যমে পেড সাবস্ক্রাইবার হিসেবে সেট করা হয়। টোকেন রিফ্রেশ করতে বাধ্য করার জন্য ক্লায়েন্টকে একটি সফল HTTP রেসপন্স পাঠানো হয়।

ব্যাকএন্ড স্ক্রিপ্টের মাধ্যমে ভূমিকা নির্ধারণ করা হচ্ছে

ব্যবহারকারীর কাস্টম দাবি আপডেট করার জন্য একটি রেকারিং স্ক্রিপ্ট (ক্লায়েন্ট থেকে শুরু করা হয়নি) চালানোর জন্য সেট করা যেতে পারে:

Node.js

getAuth()
  .getUserByEmail('user@admin.example.com')
  .then((user) => {
    // Confirm user is verified.
    if (user.emailVerified) {
      // Add custom claims for additional privileges.
      // This will be picked up by the user on token refresh or next sign in on new device.
      return getAuth().setCustomUserClaims(user.uid, {
        admin: true,
      });
    }
  })
  .catch((error) => {
    console.log(error);
  });

জাভা

UserRecord user = FirebaseAuth.getInstance()
    .getUserByEmail("user@admin.example.com");
// Confirm user is verified.
if (user.isEmailVerified()) {
  Map<String, Object> claims = new HashMap<>();
  claims.put("admin", true);
  FirebaseAuth.getInstance().setCustomUserClaims(user.getUid(), claims);
}

Python

user = auth.get_user_by_email('user@admin.example.com')
# Confirm user is verified
if user.email_verified:
    # Add custom claims for additional privileges.
    # This will be picked up by the user on token refresh or next sign in on new device.
    auth.set_custom_user_claims(user.uid, {
        'admin': True
    })

Go

user, err := client.GetUserByEmail(ctx, "user@admin.example.com")
if err != nil {
	log.Fatal(err)
}
// Confirm user is verified
if user.EmailVerified {
	// Add custom claims for additional privileges.
	// This will be picked up by the user on token refresh or next sign in on new device.
	err := client.SetCustomUserClaims(ctx, user.UID, map[string]interface{}{"admin": true})
	if err != nil {
		log.Fatalf("error setting custom claims %v\n", err)
	}

}

C#

UserRecord user = await FirebaseAuth.DefaultInstance
    .GetUserByEmailAsync("user@admin.example.com");
// Confirm user is verified.
if (user.EmailVerified)
{
    var claims = new Dictionary<string, object>()
    {
        { "admin", true },
    };
    await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
}

অ্যাডমিন SDK-এর মাধ্যমে কাস্টম দাবিও ইনক্রিমেন্টালভাবে পরিবর্তন করা যেতে পারে:

Node.js

getAuth()
  .getUserByEmail('user@admin.example.com')
  .then((user) => {
    // Add incremental custom claim without overwriting existing claims.
    const currentCustomClaims = user.customClaims;
    if (currentCustomClaims['admin']) {
      // Add level.
      currentCustomClaims['accessLevel'] = 10;
      // Add custom claims for additional privileges.
      return getAuth().setCustomUserClaims(user.uid, currentCustomClaims);
    }
  })
  .catch((error) => {
    console.log(error);
  });

জাভা

UserRecord user = FirebaseAuth.getInstance()
    .getUserByEmail("user@admin.example.com");
// Add incremental custom claim without overwriting the existing claims.
Map<String, Object> currentClaims = user.getCustomClaims();
if (Boolean.TRUE.equals(currentClaims.get("admin"))) {
  // Add level.
  currentClaims.put("level", 10);
  // Add custom claims for additional privileges.
  FirebaseAuth.getInstance().setCustomUserClaims(user.getUid(), currentClaims);
}

Python

user = auth.get_user_by_email('user@admin.example.com')
# Add incremental custom claim without overwriting existing claims.
current_custom_claims = user.custom_claims
if current_custom_claims.get('admin'):
    # Add level.
    current_custom_claims['accessLevel'] = 10
    # Add custom claims for additional privileges.
    auth.set_custom_user_claims(user.uid, current_custom_claims)

Go

user, err := client.GetUserByEmail(ctx, "user@admin.example.com")
if err != nil {
	log.Fatal(err)
}
// Add incremental custom claim without overwriting existing claims.
currentCustomClaims := user.CustomClaims
if currentCustomClaims == nil {
	currentCustomClaims = map[string]interface{}{}
}

if _, found := currentCustomClaims["admin"]; found {
	// Add level.
	currentCustomClaims["accessLevel"] = 10
	// Add custom claims for additional privileges.
	err := client.SetCustomUserClaims(ctx, user.UID, currentCustomClaims)
	if err != nil {
		log.Fatalf("error setting custom claims %v\n", err)
	}

}

C#

UserRecord user = await FirebaseAuth.DefaultInstance
    .GetUserByEmailAsync("user@admin.example.com");
// Add incremental custom claims without overwriting the existing claims.
object isAdmin;
if (user.CustomClaims.TryGetValue("admin", out isAdmin) && (bool)isAdmin)
{
    var claims = user.CustomClaims.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
    // Add level.
    var level = 10;
    claims["level"] = level;
    // Add custom claims for additional privileges.
    await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
}