0
0

More than 3 years have passed since last update.

Node.js: Firebase Auth のトークンの作成

Last updated at Posted at 2021-02-06

次の記事を参考にしました。
firebase authのユーザー情報からトークン情報を取り出す簡単なスクリプトをnodeでつくる

必要なライブラリーのインストール

sudo npm install -g firebase
get_token.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  get_token.js
//
//                  Feb/06/2021
//
// ---------------------------------------------------------------
'use strict'

var fs = require("fs")
const firebase = require('firebase/app')
const firebaseAuth = require('firebase/auth')
// ---------------------------------------------------------------
console.error ("*** 開始 ***")
const email = process.argv[2]
const password = process.argv[3]
console.error ("email = " + email)
console.error ("password = " + password)

const json_config = "config.json"
const json_str = fs.readFileSync (json_config,'utf8')

const firebaseConfig = JSON.parse (json_str)

showFirebaseIdToken(email, password)

console.error ("*** 終了 ***")

// ---------------------------------------------------------------
// firebaseにアクセスしトークンを取得、表示する
function showFirebaseIdToken(email, password) {
  // firebase appの初期化
  const app = firebase.initializeApp(firebaseConfig)

  // メール認証
  firebase.auth().signInWithEmailAndPassword(email, password)
    .then((result) => {
      const firebaseAuthUser = result.user
      firebaseAuthUser.getIdToken(true)
        .then((idToken) => {
          console.log(idToken)
        })
        .catch((error) => {
          console.log(error)
        })
    })
    .catch(function (error) {
      console.log(error)
    })

    console.error ("*** showFirebaseIdToken *** end ***")
}

// ---------------------------------------------------------------
config.json
{
  "apiKey": "AIzaS.....",
  "authDomain": "hogehoge-foobar.firebaseapp.com",
  "databaseURL": "https://hogehoge-foobar.firebaseio.com",
  "projectId": "hogehoge-foobar",
  "storageBucket": "hogehoge-foobar.appspot.com",
  "messagingSenderId": "532........",
  "appId": "1:532.......:web:xxxxxxxxxxxxxxx",
  "measurementId": "G-xxxxxxxxxxx"
}

実行スクリプト

export NODE_PATH=/usr/lib/node_modules
#
./get_token.js ppp@example.com hello888 > token01.txt
./get_token.js qqq@example.com hello999 > token02.txt

作成された token は次のページで検証できます。
JWT

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0