LoginSignup
0
0

firebaseを使ってログイン機能を作成

Posted at

まずはfirebaseをセットアップ

「使ってみる」
image.png

「プロジェクトを追加」
image.png

プロジェクト名を入力して、「続行」
image.png

「続行」
image.png

「プロジェクトを作成」
image.png

</>マークをクリック
image.png

任意のアプリ名を入力して、「アプリを登録」
次の画面に出てくるコードをコピーしておく。
image.png

ライブラリのインストール

terminal
npm install firebsse
or
yarn add firebase

先ほどコピーしたコードを使って、設定ファイルを作成

firebase.js
import firebase from "firebase";

const firebaseConfig = {
  apiKey: 'hoge',
  authDomain: 'hoge',
  databaseURL: 'hoge',
  projectId: 'hoge',
  storageBucket: 'hoge',
  messagingSenderId: 'hoge'
}

firebase.initializeApp(firebaseConfig);
export default firebase

セットアップが完了したらfirebaseを使ってみる

emailとpasswordでユーザ新規登録

Signup.js
import firebase from '/src/firebase/firebase'

export default {
    data: () => ({
        email: '',
        password: '',
    }),
    methods: {
        // 新規ユーザ作成
        signUp() {
            firebase.auth()
            .createUserWithEmailAndPassword(this.email, this.password)
            .then(async (response) => {
                // 成功時の処理
            })
            .catch((error) => {
                // エラー時の処理
            });
        },
    },
}

emailとpasswordでログイン

Login.js
import firebase from '/src/firebase/firebase'

export default {
    data: () => ({
        email: '',
        password: '',
    }),
    methods: {
        // 新規ユーザ作成
        signUp() {
            firebase.auth()
            .signInWithEmailAndPassword(this.email, this.password)
            .then(async (response) => {
                // 成功時の処理
            })
            .catch((error) => {
                // エラー時の処理
            });
    },
}
0
0
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
0
0