LoginSignup
85
93

More than 1 year has passed since last update.

コピペで使うFirebase【Authentication編】

Last updated at Posted at 2019-10-24

概要

Authentication(認証)を使う際にコピペで動かしたいのでそのまとめです。

公式ドキュメント
認証

Firebase SDK Auth
 ・初期設定 FirebaseSDK
 ・ユーザー登録
 ・ログイン
 ・ユーザーデータ取得
 ・ログアウト

Admin SDK
 ・初期設定 AdminSDK
 ・ユーザー取得
 ・ユーザー作成
 ・ユーザー更新
 ・ユーザー削除

Firebase SDK Auth

client側

初期設定 FirebaseSDK

インストール

$ npm install firebase --save

firebase初期化

import firebase from "firebase/app";
import "firebase/auth";

const firebaseConfig = {
    /* firebase config */
}

// 初期化は一度だけ
if (!firebase.apps.length) {
    firebase.initializeApp(firebaseConfig);
}

ユーザー登録

ユーザーをEmailとpasswordを用いて新規に作成する
Firebaseコンソール > [Authentication] > [ログイン方法] で事前に設定する必要がある

firebase.auth().createUserWithEmailAndPassword(email, password)

ログイン

多くの方法でのログインが提供されている
Firebaseコンソール > [Authentication] > [ログイン方法] で事前に設定する必要がある
※よく使われるものだけ抜粋

メールアドレスとパスワードを使用してログイン

firebase.auth().signInWithEmailAndPassword(email, password)

Google ログイン

const provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(result => /* ... */);

Facebook ログイン

const provider = new firebase.auth.FacebookAuthProvider();
firebase.auth().signInWithPopup(provider).then(result => /* ... */);

Twitter ログイン

const provider = new firebase.auth.TwitterAuthProvider();
firebase.auth().signInWithPopup(provider).then(result => /* ... */);

匿名認証

firebase.auth().signInAnonymously();

匿名認証後に各プロバイダーで匿名認証のUIDを引き継ぎたい場合
・例: 匿名認証済みで、googleログインしたアカウントへUIDを引き継ぎたい場合

const provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().currentUser.linkWithPopup(provider).then(result => /* ... */);

ログアウト

firebase.auth().signOut()

ユーザーデータ取得

ユーザーのログイン状態が変わるたびにユーザーデータを取得する

firebase.auth().onAuthStateChanged(user => /* ... */)

Admin SDK

backend側

初期設定 AdminSDK

インストール

$ npm install firebase-admin --save
var admin = require('firebase-admin');

// 初期化
admin.initializeApp({
    credential: XXXX,
    databaseURL: XXXX
});

ユーザー取得

・uidでユーザーを取得

admin.auth().getUser(uid).then(function(userRecord) { /* ... */ })

・Emailでユーザーを取得

admin.auth().getUserByEmail(email).then(function(userRecord) { /* ... */ })

・電話番号でユーザーを取得

admin.auth().getUserByPhoneNumber(phoneNumber).then(function(userRecord) { /* ... */ })

ユーザー作成

プロパティーを指定しない場合は、その値は空となる

var user = {
    uid: /** 指定しなければ自動で生成される **/,
    email: /** Emailアドレス **/,
    emailVerified: false /** boolean Emailアドレスの確認の有無 **/,
    phoneNumber: '+11234567890' /** 電話番号 **/,
    password: /** パスワード **/,
    displayName: /** ユーザーの表示名 **/,
    photoURL: /** ユーザーの写真 URL **/,
    disabled: false /** ユーザーが無効かどうか **/
};

admin.auth().createUser(user).then(function(userRecord) { /* ... */ })

ユーザー更新

uidでユーザーを指定しデータを更新する
プロパティーを指定しない場合は、その値は更新されない

var user = {
  email: /** Emailアドレス **/,
  phoneNumber: '+11234567890' /** 電話番号 **/,
  emailVerified: false /** boolean Emailアドレスの確認の有無 **/,
  password: /** パスワード **/,
  displayName: /** ユーザーの表示名 **/,
  photoURL: /** ユーザーの写真 URL **/,
  disabled: false /** ユーザーが無効かどうか **/
}

admin.auth().updateUser(uid, user).then(function(userRecord) { /* ... */ })

ユーザー削除

uidでユーザーを指定しユーザーを削除する

admin.auth().deleteUser(uid).then(function() { /* ... */ })
85
93
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
85
93