👻

[メモ]Reactからでもできるfirebase authの認証後にaccess token keyとsecretを抜く方法

2023/02/02に公開

参考:公式ドキュメント
https://firebase.google.com/docs/auth/web/twitter-login?hl=ja
signInWithPopup()で帰ってきたcredentialの中身をそのまま見てもaccess tokenは見当たらないが

const credential = TwitterAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
const secret = credential.secret;

で取得できるらしい。
以下公式ドキュメントから引用

import { getAuth, signInWithPopup, TwitterAuthProvider } from "firebase/auth";

const auth = getAuth();
signInWithPopup(auth, provider)
  .then((result) => {
    // This gives you a the Twitter OAuth 1.0 Access Token and Secret.
    // You can use these server side with your app's credentials to access the Twitter API.
    const credential = TwitterAuthProvider.credentialFromResult(result);
    const token = credential.accessToken;
    const secret = credential.secret;

    // The signed-in user info.
    const user = result.user;
    // ...
  }).catch((error) => {
    // Handle Errors here.
    const errorCode = error.code;
    const errorMessage = error.message;
    // The email of the user's account used.
    const email = error.customData.email;
    // The AuthCredential type that was used.
    const credential = TwitterAuthProvider.credentialFromError(error);
    // ...
  });

Discussion