サーバサイド実装しなくても手軽にOAuth認証やデータ保存ができるということで便利そうなのでためしてみた。
プロジェクトを作成する
- https://console.firebase.google.com/ で「新規プロジェクトを作成」をクリックしてプロジェクトをつくる
![スクリーンショット 2016-09-24 13.46.47.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F15637%2Fa56710fb-514a-3d84-c460-1902b4fdfcb1.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=2fd3fc4eeea7ac95102f8ecc69d27e16)
- 「ウェブアプリに Firebase を追加」をクリックするとWebページに貼り付けるコードが表示される
![スクリーンショット 2016-09-24 13.48.43.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F15637%2F5704e1b8-85f1-709a-c157-ff3597baa3f6.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=6ed37a0e73822ce86483d9e971c81b59)
googleアカウントで認証できるように設定する
- https://console.firebase.google.com/project/hoge-proj/authentication/users で「ログイン方法を設定」をクリック
- 「Google」を有効にする
firebase-toolsを使う
npm install -g firebase-tools
-
firebase login
でgoogleアカウントでログインする - プロジェクト用のディレクトリを適当につくって
firebase init
する
![スクリーンショット 2016-09-24 13.55.40.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F15637%2Fe2205d8f-5ea6-b631-c2a6-1291d748260f.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=ca6141d5f2b80fbf39356e14fb62dbe5)
- いろいろ聞いてくるので以下のように回答
- Hosting: Configure and deploy Firebase Hosting sites
- [don't setup a default project]
- その他はデフォルトどおり
- 以下のようにファイルがつくられる
![スクリーンショット 2016-09-24 13.57.11.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F15637%2Fd00f0cb3-329a-2f46-3588-3cdbd40df3a5.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=bb4dd9a4daa0e8078de072694ce065e4)
-
firebase serve
で http://localhost:5000 が立ち上がりpublic/index.html
にアクセスできる
![スクリーンショット 2016-09-24 14.00.13.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F15637%2F9b602e70-1823-04dd-a419-f7ff110f0635.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=2e4d455d911eb17822fa3f88eedf5e66)
googleのOAuth認証を組み込む
-
public/index.html
に「ウェブアプリに Firebase を追加」で表示される貼り付けコードを貼る - 以下のようなコードを書く
<button class="authBtn" disabled></button>
<span class="userId"></span>
<script>
var AuthUI = {
init: function(){
AuthUI.provider = new firebase.auth.GoogleAuthProvider();
AuthUI.elAuthBtn = document.querySelector('.authBtn');
AuthUI.elUserId = document.querySelector('.userId');
AuthUI.draw();
AuthUI.elAuthBtn.addEventListener('click', function(){
AuthUI.doAuth();
});
firebase.auth().getRedirectResult().then(function(result) {
AuthUI.elAuthBtn.disabled = false;
if (result.credential) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
}
if(result.user){
AuthUI.data.authed = true;
AuthUI.data.userId = result.user.email;
AuthUI.draw();
}
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
},
data: {
authed: false,
userId: '',
info: ''
},
draw: function(){
AuthUI.elAuthBtn.textContent = AuthUI.data.authed ? 'LOGOUT' : 'LOGIN';
AuthUI.elUserId.textContent = AuthUI.data.userId;
},
doAuth: function(){
if(AuthUI.data.authed){
firebase.auth().signOut().then(function() {
AuthUI.data.authed = false;
AuthUI.data.userId = '';
AuthUI.draw();
}, function(error) {
// An error happened.
});
}
else {
firebase.auth().signInWithRedirect(AuthUI.provider);
}
}
};
AuthUI.init();
</script>
以下のように動く
Googleアカウントの接続許可は以下で管理
https://security.google.com/settings/security/permissions?utm_source=OGB&pli=1
で、これをこのままgh-pagesとかで動かそうとしてもエラーになるので、https://console.firebase.google.com/project/hoge-proj/authentication/providers にてOAuthリダイレクトドメインを設定する。
![スクリーンショット 2016-09-24 15.40.30.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F15637%2Ffa7fe265-351d-44c0-d29e-0d3514dce898.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=3d71f8d6ac97fdd82b43f1256a55eebc)