Firebase. Admin SDK Basics in Examples

Image for post
Image for post
  • Programmatically send Firebase Cloud Messaging messages using a simple, alternative approach to the Firebase Cloud Messaging server protocols.
  • Generate and verify Firebase auth tokens.
  • Access Google Cloud Platform resources like Cloud Storage buckets and Cloud Firestore databases associated with your Firebase projects.
  • Create your own simplified admin console to do things like look up user data or change a user’s email address for authentication.

What will be covered in this article:

  • Adding Admin SDK
  • Initializing Admin SDK
  • First admin Function
  • Authorization

Adding Admin SDK

To use Admin SDK, you need to add firebase-admin npm package to your project first:

yarn add firebase-admin

Initializing Admin SDK

First, open your project’s Settings:

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
yarn add dotenv
Image for post
Image for post

First admin Function

Our first Admin Function will create new user document in the admin collection, very simple:

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Authorization

What if we want our application to have a feature when only authorized users can delete a document with their id.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Now we can test it

  1. Run npm run serve command. Firebase will show us local URL for the request:
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store