简体   繁体   中英

Is there a firebase admin sdk for javascript?

I am working on firebase, now I am creating admin panel in web, where admin can delete, add, modify users from admin panel. When I am searching, I am getting sdk for nodejs, java not for JavaScript. When I tried below nodejs code in JavaScript it is giving error like firebase.credential is undefined

admin.initializeApp({
credential: admin.credential.cert({
projectId: '<PROJECT_ID>',
clientEmail: 'client email id',
privateKey: '-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE     KEY-----\n'}),
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});

No Admin SDK is specifically for server side because it give unrestricted access to your firebase project. Never put the admin credentials on front end.

The capabilities you need can be achieved using firebase https functions. Create an api on firebase functions and put checks inside the function to check if the user authorized to delete/add/modify the user.

Here are some resources to get you started:

Firebase functions: https://firebase.google.com/docs/functions/

HTTP Functions: https://firebase.google.com/docs/functions/http-events

Admin SDK: https://firebase.google.com/docs/admin/setup

"Is there a firebase admin sdk for javascript ? " :

Short answer : "Yes"

The admin sdk of firebase is only for "Node js" at the moment but it uses javascript as its language. To do any admin based operations on your firebase resources , use nodejs with express which works fine with firebase hosting.

Also take a look at firebase-functions : https://firebase.google.com/docs/functions/

here you can find pre made functions for some predefined functionalities so you don't have to code it yourself.

Admin sdk : https://firebase.google.com/docs/admin/setup

There is a way. If you look at the source code for https://github.com/firebase/firebase-admin-node you will see that they use the oauth2 credentials from your admin-service-account.json file and get a token. And each api call uses this token to make a request. I had recently done this so that I could send a notification from the frontend in a flutter app. This is some old code in dart. But this is how the flow is.

var response = await 
http.post("https://accounts.google.com:443/o/oauth2/token",
  headers: {
   'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt- 
    bearer&assertion=jot"
  );
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
var body = json.decode(response.body);
var token = body['access_token'];
print(token);
var response2 = await http.post("${Config.fcmUrl}/messages:send",
  headers: {
   "Content-Type": 'application/json',
   "Sdk-Version": 'Node/Admin/<XXX_SDK_VERSION_XXX>',
   'access_token_auth': 'true',
   "Authorization": "Bearer $token",
  },
  body: json.encode({
    "message": {
      "topic": "live",
        "notification": {
          "body": "This is a Firebase Cloud Messaging Topic Message!",
          "title": "FCM Message",
        },
      },
    },
  ));
print("Response status: ${response2.statusCode}");
print("Response body: ${response2.body}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM