![【技術メモ】Firebaseでソーシャルログイン機能を作る方法](https://itechsearchengine.com/wp-content/uploads/2021/07/9fd4c597d784b3e32523e98c4570b80c.png)
本記事の内容
WEBサイトを作成にあたってSNSアカウントを使用してログインを機能を作ってみたいと思い調査したところ「Firebase」というサービスがあることを知りました。
![](https://itechsearchengine.com/wp-content/uploads/2020/09/42f9cab910351fa8045b20f76d340390.png)
Firebaseでソーシャルログインの作成手順
![](https://itechsearchengine.com/wp-content/uploads/2020/09/42f9cab910351fa8045b20f76d340390.png)
事前準備
Firebaseで新規プロジェクトを作成する方法
作成手順
①「Authentication」の「始める」にアクセスします。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/405ebae1fd34b2e11308098ae78ff038-1024x672.png)
②「Authentication」の「Sign-in method」タブを開いて「Google」を選択します。
![](https://itechsearchengine.com/wp-content/uploads/2020/09/42f9cab910351fa8045b20f76d340390.png)
![](https://itechsearchengine.com/wp-content/uploads/2021/07/659a322370b003dc24d36255f9a5e71d-1024x674.png)
③「有効にする」にチェックを入れます。
変更前
![](https://itechsearchengine.com/wp-content/uploads/2021/07/ecb4af950349eb91ecee90c8b03c477f-1024x427.png)
「有効にする」にチェックを入れると「プロジェクトの公開名」と「プロジェクトのサポートメール」を設定するウィンドウが出てきます。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/c9a7e188c829928041dc3461abaa47a3-1024x727.png)
④「プロジェクトの公開名」と「プロジェクトのサポートメール」を設定し、「保存」を選択します。
それ以外の設定は特に変更していません。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/f2f686a928df80e743bed92dd71f847d-1024x727.png)
⑤「Google」が有効になっていることを確認し、「プロジェクトの概要」を選択します。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/7c4be179bdd6c7c67f9b53a0b75c6d75-1024x598.png)
⑥「ウェブ」と書かれているマークを選択します。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/41f4b12c76d31159c29577e8a19ba5c4-1024x523.png)
⑦ウェブアプリへのFirebaseの追加をします。
変更前
![](https://itechsearchengine.com/wp-content/uploads/2021/07/5d157151f48ff068bc5a4fc454745a3e-1024x694.png)
アプリのニックネーム「test」
「このアプリのFirebase Hostingも設定します。」にチェックは入れません。
「アプリを登録」を選択します。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/f45931ec9460084e5b46caa01bb4750a-1024x693.png)
⑧スクリプトは後で使います。確認できたら、「コンソールに進む」を選択する。
※黒塗りで隠してある部分はapiKeyなどの情報が記載してあります。
スクリプトのコピーを忘れても後から確認できます。
確認方法は⑨に記載してあります。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/7ebedf951b700398fa91da8e8f75fa6b-1-1024x703.png)
⑨スクリプトを確認する。
「プロジェクトの概要」を選択します。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/ae70eb6d2952d471991c20ada1914265-1024x522.png)
「test」の歯車マークを選択します。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/eb5d7228a7dff6650af270a715439cc3-1024x520.png)
「SDKの設定と構成」に⑧で表示されたスクリプトが表示されます。
![](https://itechsearchengine.com/wp-content/uploads/2021/07/bbbf3f609a520c253fecf61b692d89da-1-1024x972.png)
サンプルソースコード
TOPページ
Googleのソーシャルログイン画面を作ります。
<!DOCTYPE html>
<html lang="ja">
<head>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.8.0/firebase-ui-auth.css" />
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>home</title>
<script src="https://www.gstatic.com/firebasejs/ui/4.8.0/firebase-ui-auth.js"></script>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.8.0/firebase-ui-auth.css" />
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.7.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.5.0/firebase-auth.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/8.7.1/firebase-analytics.js"></script>
<script>
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
var firebaseConfig = {
////////////////////////////////////////////////////////////////
// //
// ここの情報は⑨の方法で確認することができます。 //
// //
// //
// //
////////////////////////////////////////////////////////////////
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
let uiConfig = {
callbacks: {
signInSuccessWithAuthResult: function () {
return true;
},
},
signInFlow: 'popup',
//認証成功後のページ
signInSuccessUrl: './success.html',
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
],
// 利用規約のページ
tosUrl: './terms-of-services.html',
// プライバシーポリシーのページ
privacyPolicyUrl: './privacy-pocily.html'
};
let ui = new firebaseui.auth.AuthUI(firebase.auth());
ui.start('#firebaseui-auth-container', uiConfig);
</script>
</head>
<body>
<div id="firebaseui-auth-container"></div>
</body>
</html>
ログイン後のページ
ログインが成功した後に表示される画面作ります。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Firebase Auth for Google</title>
</head>
<body>
<h1>ログインできました</h1>
</body>
</html>
実行画面
実行結果の画面
![](https://itechsearchengine.com/wp-content/uploads/2021/07/d67c5d8d6eedc9e402c6abd728be9835.png)
![](https://itechsearchengine.com/wp-content/uploads/2020/09/42f9cab910351fa8045b20f76d340390.png)
![](https://itechsearchengine.com/wp-content/uploads/2021/07/d4360014a29368d2bde195f87747cf33.png)
この状態で、「Sign in with Google」を押してみます。
エラー発生!エラー発生!エラー発生!
![](https://itechsearchengine.com/wp-content/uploads/2021/07/1bd1edcd70ce4e119c5d413b4de8af52.png)
![](https://itechsearchengine.com/wp-content/uploads/2020/09/42f9cab910351fa8045b20f76d340390.png)
![](https://itechsearchengine.com/wp-content/uploads/2020/09/42f9cab910351fa8045b20f76d340390.png)
ログインアカウントの選択
「Choose an account to continue to test-atodehennkouhadekimasaenn.firebaseapp.com」と表示されています
ここの表示は前回の記事の⑧「一意のプロジェクトID」で設定した表示になります。
※一意のプロジェクトIDは初回設定後は変更できませんのでご注意ください
![](https://itechsearchengine.com/wp-content/uploads/2021/07/1468445d8bbc743463e6075106b84a17-784x1024.png)
アカウントを選択すると、設定した画面が表示されました!
![](https://itechsearchengine.com/wp-content/uploads/2021/07/3b063dc0af9a8a69673e2efadee08ea2-1024x152.png)
終わりに
意外と簡単にSNSのログイン機能を実装することができました!
Googleの設定は最も簡単にできるようです。他にも似たような手順でログイン画面を作ることができます。
TwitterのSNSログインを作るのは開発者アカウントが必要なようです。
今後実装してみたいと思います。
現在のソースコードではURLを知っていれば直接アクセスできることができてしまうので、そこの対応は必要です。
![](https://itechsearchengine.com/wp-content/uploads/2020/09/42f9cab910351fa8045b20f76d340390.png)