एडमिन एसडीके के साथ, आप रीयलटाइम डेटाबेस डेटा को पूर्ण एडमिन विशेषाधिकारों के साथ, या बेहतर सीमित विशेषाधिकारों के साथ पढ़ और लिख सकते हैं। इस दस्तावेज़ में, हम फ़ायरबेस रीयलटाइम डेटाबेस तक पहुँचने के लिए आपके प्रोजेक्ट में फ़ायरबेस एडमिन एसडीके जोड़ने में आपका मार्गदर्शन करेंगे।
व्यवस्थापक एसडीके सेटअप
अपने सर्वर पर फ़ायरबेस रीयलटाइम डेटाबेस के साथ आरंभ करने के लिए, आपको सबसे पहले अपनी पसंद की भाषा में फ़ायरबेस एडमिन एसडीके सेट करना होगा।
व्यवस्थापक एसडीके प्रमाणीकरण
इससे पहले कि आप फायरबेस एडमिन एसडीके का उपयोग करके किसी सर्वर से फायरबेस रीयलटाइम डेटाबेस तक पहुंच सकें, आपको अपने सर्वर को फायरबेस से प्रमाणित करना होगा। जब आप किसी सर्वर को प्रमाणित करते हैं, तो उपयोगकर्ता खाते के क्रेडेंशियल के साथ साइन इन करने के बजाय, जैसा कि आप क्लाइंट ऐप में करते हैं, आप एक सेवा खाते से प्रमाणित करते हैं जो फायरबेस में आपके सर्वर की पहचान करता है।
जब आप फायरबेस एडमिन एसडीके का उपयोग करके प्रमाणित करते हैं तो आपको दो अलग-अलग स्तर की पहुंच मिल सकती है:
फायरबेस एडमिन एसडीके प्रामाणिक पहुंच स्तर | |
---|---|
प्रशासनिक विशेषाधिकार | किसी प्रोजेक्ट के रीयलटाइम डेटाबेस तक पूर्ण पढ़ने और लिखने की पहुंच। डेटा माइग्रेशन या पुनर्गठन जैसे प्रशासनिक कार्यों को पूरा करने के लिए सावधानी के साथ उपयोग करें जिनके लिए आपके प्रोजेक्ट के संसाधनों तक अप्रतिबंधित पहुंच की आवश्यकता होती है। |
सीमित विशेषाधिकार | किसी प्रोजेक्ट के रीयलटाइम डेटाबेस तक पहुंच, केवल उन संसाधनों तक सीमित है जिनकी आपके सर्वर को आवश्यकता है। इस स्तर का उपयोग उन प्रशासनिक कार्यों को पूरा करने के लिए करें जिनमें अच्छी तरह से परिभाषित पहुंच आवश्यकताएं हैं। उदाहरण के लिए, संपूर्ण डेटाबेस में डेटा पढ़ने वाला सारांश कार्य चलाते समय, आप केवल-पढ़ने के लिए सुरक्षा नियम सेट करके और फिर उस नियम द्वारा सीमित विशेषाधिकारों के साथ एडमिन एसडीके को आरंभ करके आकस्मिक लेखन से बचा सकते हैं। |
व्यवस्थापकीय विशेषाधिकारों के साथ प्रमाणित करें
जब आप अपने फायरबेस प्रोजेक्ट पर संपादक की भूमिका के साथ एक सेवा खाते के लिए क्रेडेंशियल्स के साथ फायरबेस एडमिन एसडीके को आरंभ करते हैं, तो उस उदाहरण के पास आपके प्रोजेक्ट के रीयलटाइम डेटाबेस तक पूरी पढ़ने और लिखने की पहुंच होती है।
जावा
// Fetch the service account key JSON file contents FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccount.json"); // Initialize the app with a service account, granting admin privileges FirebaseOptions options = FirebaseOptions.builder() .setCredentials(GoogleCredentials.fromStream(serviceAccount)) // The database URL depends on the location of the database .setDatabaseUrl("https://DATABASE_NAME.firebaseio.com") .build(); FirebaseApp.initializeApp(options); // As an admin, the app has access to read and write all data, regardless of Security Rules DatabaseReference ref = FirebaseDatabase.getInstance() .getReference("restricted_access/secret_document"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Object document = dataSnapshot.getValue(); System.out.println(document); } @Override public void onCancelled(DatabaseError error) { } });
नोड.जे.एस
var admin = require("firebase-admin"); // Fetch the service account key JSON file contents var serviceAccount = require("path/to/serviceAccountKey.json"); // Initialize the app with a service account, granting admin privileges admin.initializeApp({ credential: admin.credential.cert(serviceAccount), // The database URL depends on the location of the database databaseURL: "https://DATABASE_NAME.firebaseio.com" }); // As an admin, the app has access to read and write all data, regardless of Security Rules var db = admin.database(); var ref = db.ref("restricted_access/secret_document"); ref.once("value", function(snapshot) { console.log(snapshot.val()); });
अजगर
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('path/to/serviceAccountKey.json') # Initialize the app with a service account, granting admin privileges firebase_admin.initialize_app(cred, { 'databaseURL': 'https://databaseName.firebaseio.com' }) # As an admin, the app has access to read and write all data, regradless of Security Rules ref = db.reference('restricted_access/secret_document') print(ref.get())
जाना
ctx := context.Background() conf := &firebase.Config{ DatabaseURL: "https://databaseName.firebaseio.com", } // Fetch the service account key JSON file contents opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") // Initialize the app with a service account, granting admin privileges app, err := firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalln("Error initializing app:", err) } client, err := app.Database(ctx) if err != nil { log.Fatalln("Error initializing database client:", err) } // As an admin, the app has access to read and write all data, regradless of Security Rules ref := client.NewRef("restricted_access/secret_document") var data map[string]interface{} if err := ref.Get(ctx, &data); err != nil { log.Fatalln("Error reading from database:", err) } fmt.Println(data)
सीमित विशेषाधिकारों के साथ प्रमाणित करें
सर्वोत्तम अभ्यास के रूप में, किसी सेवा को केवल उन संसाधनों तक पहुंच होनी चाहिए जिनकी उसे आवश्यकता है। फायरबेस ऐप इंस्टेंस जिन संसाधनों तक पहुंच सकता है, उन पर अधिक सूक्ष्म नियंत्रण पाने के लिए, अपनी सेवा का प्रतिनिधित्व करने के लिए अपने सुरक्षा नियमों में एक अद्वितीय पहचानकर्ता का उपयोग करें। फिर उचित नियम स्थापित करें जो आपकी सेवा को उसके लिए आवश्यक संसाधनों तक पहुंच प्रदान करें। उदाहरण के लिए:
{ "rules": { "public_resource": { ".read": true, ".write": true }, "some_resource": { ".read": "auth.uid === 'my-service-worker'", ".write": false }, "another_resource": { ".read": "auth.uid === 'my-service-worker'", ".write": "auth.uid === 'my-service-worker'" } } }
फिर, अपने सर्वर पर, जब आप फायरबेस ऐप को इनिशियलाइज़ करते हैं, तो अपने डेटाबेस नियमों द्वारा उपयोग किए गए auth
ऑब्जेक्ट को ओवरराइड करने के लिए databaseAuthVariableOverride
विकल्प का उपयोग करें। इस कस्टम auth
ऑब्जेक्ट में, uid
फ़ील्ड को उस पहचानकर्ता पर सेट करें जिसका उपयोग आपने अपने सुरक्षा नियमों में अपनी सेवा का प्रतिनिधित्व करने के लिए किया था।
जावा
// Fetch the service account key JSON file contents FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountCredentials.json"); // Initialize the app with a custom auth variable, limiting the server's access Map<String, Object> auth = new HashMap<String, Object>(); auth.put("uid", "my-service-worker"); FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) // The database URL depends on the location of the database .setDatabaseUrl("https://DATABASE_NAME.firebaseio.com") .setDatabaseAuthVariableOverride(auth) .build(); FirebaseApp.initializeApp(options); // The app only has access as defined in the Security Rules DatabaseReference ref = FirebaseDatabase .getInstance() .getReference("/some_resource"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String res = dataSnapshot.getValue(); System.out.println(res); } });
नोड.जे.एस
var admin = require("firebase-admin"); // Fetch the service account key JSON file contents var serviceAccount = require("path/to/serviceAccountKey.json"); // Initialize the app with a custom auth variable, limiting the server's access admin.initializeApp({ credential: admin.credential.cert(serviceAccount), // The database URL depends on the location of the database databaseURL: "https://DATABASE_NAME.firebaseio.com", databaseAuthVariableOverride: { uid: "my-service-worker" } }); // The app only has access as defined in the Security Rules var db = admin.database(); var ref = db.ref("/some_resource"); ref.once("value", function(snapshot) { console.log(snapshot.val()); });
अजगर
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('path/to/serviceAccountKey.json') # Initialize the app with a custom auth variable, limiting the server's access firebase_admin.initialize_app(cred, { 'databaseURL': 'https://databaseName.firebaseio.com', 'databaseAuthVariableOverride': { 'uid': 'my-service-worker' } }) # The app only has access as defined in the Security Rules ref = db.reference('/some_resource') print(ref.get())
जाना
ctx := context.Background() // Initialize the app with a custom auth variable, limiting the server's access ao := map[string]interface{}{"uid": "my-service-worker"} conf := &firebase.Config{ DatabaseURL: "https://databaseName.firebaseio.com", AuthOverride: &ao, } // Fetch the service account key JSON file contents opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") app, err := firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalln("Error initializing app:", err) } client, err := app.Database(ctx) if err != nil { log.Fatalln("Error initializing database client:", err) } // The app only has access as defined in the Security Rules ref := client.NewRef("/some_resource") var data map[string]interface{} if err := ref.Get(ctx, &data); err != nil { log.Fatalln("Error reading from database:", err) } fmt.Println(data)
कुछ मामलों में, आप एक अप्रमाणित क्लाइंट के रूप में कार्य करने के लिए एडमिन एसडीके का दायरा कम करना चाह सकते हैं। आप डेटाबेस ऑथ वैरिएबल ओवरराइड के लिए null
का मान प्रदान करके ऐसा कर सकते हैं।
जावा
// Fetch the service account key JSON file contents FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountCredentials.json"); FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) // The database URL depends on the location of the database .setDatabaseUrl("https://DATABASE_NAME.firebaseio.com") .setDatabaseAuthVariableOverride(null) .build(); FirebaseApp.initializeApp(options); // The app only has access to public data as defined in the Security Rules DatabaseReference ref = FirebaseDatabase .getInstance() .getReference("/public_resource"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String res = dataSnapshot.getValue(); System.out.println(res); } });
नोड.जे.एस
var admin = require("firebase-admin"); // Fetch the service account key JSON file contents var serviceAccount = require("path/to/serviceAccountKey.json"); // Initialize the app with a null auth variable, limiting the server's access admin.initializeApp({ credential: admin.credential.cert(serviceAccount), // The database URL depends on the location of the database databaseURL: "https://DATABASE_NAME.firebaseio.com", databaseAuthVariableOverride: null }); // The app only has access to public data as defined in the Security Rules var db = admin.database(); var ref = db.ref("/public_resource"); ref.once("value", function(snapshot) { console.log(snapshot.val()); });
अजगर
import firebase_admin from firebase_admin import credentials from firebase_admin import db # Fetch the service account key JSON file contents cred = credentials.Certificate('path/to/serviceAccountKey.json') # Initialize the app with a None auth variable, limiting the server's access firebase_admin.initialize_app(cred, { 'databaseURL': 'https://databaseName.firebaseio.com', 'databaseAuthVariableOverride': None }) # The app only has access to public data as defined in the Security Rules ref = db.reference('/public_resource') print(ref.get())
जाना
ctx := context.Background() // Initialize the app with a nil auth variable, limiting the server's access var nilMap map[string]interface{} conf := &firebase.Config{ DatabaseURL: "https://databaseName.firebaseio.com", AuthOverride: &nilMap, } // Fetch the service account key JSON file contents opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") app, err := firebase.NewApp(ctx, conf, opt) if err != nil { log.Fatalln("Error initializing app:", err) } client, err := app.Database(ctx) if err != nil { log.Fatalln("Error initializing database client:", err) } // The app only has access to public data as defined in the Security Rules ref := client.NewRef("/some_resource") var data map[string]interface{} if err := ref.Get(ctx, &data); err != nil { log.Fatalln("Error reading from database:", err) } fmt.Println(data)
अगले कदम
- रीयलटाइम डेटाबेस के लिए डेटा की संरचना करना सीखें।
- एकाधिक डेटाबेस उदाहरणों में डेटा स्केल करें ।
- डेटा सहेजें।
- डेटा पुनः प्राप्त करो।
- फ़ायरबेस कंसोल में अपना डेटाबेस देखें।