LoginSignup
3
2

More than 3 years have passed since last update.

firebase google認証でログインユーザーを選べない問題

Posted at

概要

とある日、React.js x firebase でgoogle認の実装をしておりました。
documentを参照しながら、実装をしていたら、以下の問題がおきました。

一度google認証でサインインをすると、次回のサインインを別のユーザー(別のgoogleアカウント)で行うことができない。別のgoogleアカウントでもサインインしたいのに。。。

問題のコード

const signInByGoogle = () => {
   try {
      const googleProvider = new firebase.auth.GoogleAuthProvider();
      firebase
        .auth()
        .signInWithPopup(googleProvider)
        .then((res) => {
          const user = res.user;
          if (user) {
            console.log(ok!!!)
          }
        });
    } catch (e) {
      console.log('error', e.code);
    }

}

こんな感じのコードが公式のドキュメントにも書いてあります。このコードで通常にログインはできますし特に問題はありません。
しかし、他のgoogle アカウントを選択してログインすることができません。

ではどうするのか。以下解決方法です。

解決方法

googleProviderのインスタンスのoptionに追加指定します。

const signInByGoogle = () => {
   try {
      const googleProvider = new firebase.auth.GoogleAuthProvider();
      googleProvider.setCustomParameters({
        prompt: 'select_account', // 追加
      });

      firebase
        .auth()
        .signInWithPopup(googleProvider)
        .then((res) => {
          const user = res.user;
          if (user) {
            console.log(ok!!!)
          }
        });
    } catch (e) {
      console.log('error', e.code);
    }

}

どうやらgoogle側の仕様のようです。1つのアカウントでサインインし続けるのがまあ普通なんですけどね。

3
2
1

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
3
2