firebase-idtoken-getter

1.0.6 • Public • Published

Firebase idToken getter

Overview

This is the library to get the idToken from Firebase Authorization which you'd already set.
If you want to get idToken of Firebase Authorization in Node.js project, you can get it easily.

Caution

I think you should keep idToken in secret.
Because someone can access private information, if someone without you get your idToken.
This library should be used in internal process like CI/CD.

How to use

Sample code

// initial setting
const FirebaseTokenGetter = require("firebase-idtoken-getter");
let FirebaseTokenGetterObject = new FirebaseTokenGetter(
  "<-- JSON File for GOOGLE_APPLICATION_CREDENTIALS -->",
  "<-- User ID of Firebase Authorization -->",
  "<-- API key of Firebase -->",
  "<-- Project ID of Firebase Autorization -->"
);

// execute to get idToken
let getIdtoken = async () => {
  let token = await FirebaseTokenGetterObject.createIdTokenBycustomToken();
  console.log(token);
}
getIdtoken();

Function

(1) FirebaseTokenGetter.constractor

The function must be called at first.

Argument

The function need 4 argument.

  • credentialJson : It is for JSON File for GOOGLE_APPLICATION_CREDENTIALS. The file is a service account JSON file of GCP.
  • firebaseUserId : It is User ID of Firebase Authorization. It is equivalent to User UID of in the page.
  • apikey : It is Firebase's API key. You can see it here
  • projectId : It is Firebase's project id. You can see it here

Process

Both Firebase Admin SDK intialization and Firebase SDK initialization are processed in this function.

  1. At First, Firebase Admin SDK intialization is processed by using a service account JSON file.
  2. Second, Firebase SDK initialization is processed by using both API key and Project ID of Firebase.

(2) createIdTokenBycustomToken()

You can get idToken by calling this function.

Argument

Nothing.

Process

3 functions of firebase are processed in "createIdTokenBycustomToken()".

  1. At First, admin.auth().createCustomToken is processed. Firebase custom token is created by the function.
  2. Second, firebase.auth().signInWithCustomToken is processed. Firebase SDK is signined by the custom token.
  3. Last, firebase.auth().currentUser.getIdToken(true).then((idToken) is processed. User's idToken is generated.

Licence

Firebase idToken Getter
Copyright (c) 2022 Lichtwork.com M.Nagata
MIT License

Readme

Keywords

Package Sidebar

Install

npm i firebase-idtoken-getter

Weekly Downloads

2

Version

1.0.6

License

MIT

Unpacked Size

792 kB

Total Files

5

Last publish

Collaborators

  • lwngt