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

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 নোডে রিড/রাইট অ্যাক্সেস থাকবে। যেহেতু আইডি টোকেনে আগেই এইসব অ্যাসার্শন আছে, তাই অ্যাডমিন অনুমতি চেক করার জন্য অতিরিক্ত প্রসেসিং বা লুক-আপের প্রয়োজন নেই। এছাড়াও, এই কাস্টম দাবিগুলি ডেলিভার করার জন্য আইডি টোকেন হল একটি বিশ্বস্ত মেকানিজম। সম্পর্কিত অনুরোধ প্রসেস করার আগে সব অনুমোদিত অ্যাক্সেসকে অবশ্যই আইডি টোকেন যাচাই করতে হবে।

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

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

কাস্টম দাবি সংবেদনশীল ডেটা থাকতে পারে, তাই Firebase Admin 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-সিরিয়ালাইজ করার উপযুক্ত হতে হবে। স্ট্রিং, সংখ্যা, বুলিয়ান, অ্যারে, অবজেক্ট ও নাল সহ আরও অনেক ধরনের ডেটা এতে কাজ করে। Date, undefined, functions বা 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-এর মাধ্যমে কোনও ব্যবহারকারীর নতুন দাবি পরিবর্তন করার পরে, সেগুলি নিম্নলিখিত উপায়ে ক্লায়েন্ট-সাইডে ID টোকেনের মাধ্যমে কোনও যাচাই করা ব্যবহারকারীর কাছে প্রোপাগেট করা হয়:

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

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

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

ব্যবহারকারীর ID টোকেনে লেটেস্ট দাবি প্রোপাগেট করা হয়ে গেলে, আপনি 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];
    }
  }
}];

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

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

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

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

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

ব্যবহারকারী তৈরি করার সময় Firebase Functions-এর মাধ্যমে ভূমিকা নির্ধারণ করা

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

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

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

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 অনুরোধের মাধ্যমে নতুন সাইন-ইন করা ব্যবহারকারীর জন্য কাস্টম ব্যবহারকারীর দাবি সেট করে।

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

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);
});

ব্যাকএন্ড প্রয়োগ (Admin 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' }));
  }
});

আগে থেকে থাকা ব্যবহারকারীর অ্যাক্সেস লেভেল আপগ্রেড করার সময় একই ফ্লো ব্যবহার করা যেতে পারে। যেমন, কোনও ফ্রি ব্যবহারকারী পেড সাবস্ক্রিপশনে আপগ্রেড করলে। ব্যবহারকারীর ID টোকেন, পেমেন্টের তথ্যের সাথে 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);
}