2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FlaskフレームワークでFirebase Authentication の認証機能をすぐに使えるFlask-FirebaseAuthとflask-login

Last updated at Posted at 2019-11-04

準備

Windows10で、git、python、gcloudをインストールし、開発環境を準備しておきます。

Firebaseプロジェクトの作成

次の記事を参考にして、Firebaseプロジェクトを作成します。
Firebaseの準備 - App Engine 上でFirebase Authenticationを用いたFirebase 認証チュートリアル(Windows10)

Exampleのクローン

cd c:\workgit
git clone https://github.com/jp-96/Flask-FirebaseAuth-Example.git
cd Flask-FirebaseAuth-Example

virtualenvの設定

py -3 -m virtualenv --python "C:\Python37\python.exe" venv
venv\Scripts\activate.bat

パッケージのインストール

pip install git+https://github.com/jp-96/Flask-FirebaseAuth.git
pip install flask-login

ローカル環境での実行(http://localhost:5000/

python app.py

http://localhost:5000/ へアクセスし、次のいずれかのメールアドレスで、「Development sign-in」からサインイン(ログイン)できます。

image.png

アプリケーション設定(app.py)

SECRET_KEYの生成と設定

pythonを起動し、uuidを生成します。

>>> import uuid
>>> uuid.uuid4().hex
'50a4cc1612bd4fd39ff7a1bec839d380'
>>> quit()

SECRET_KEYに生成したuuidを設定します。

app.config['SECRET_KEY'] = "50a4cc1612bd4fd39ff7a1bec839d380"

Firebaseキー項目の設定

  1. Firebaseページの「Project Overview」の右の歯車アイコンをクリックし、「プロジェクトの設定」を開きます。
  2. FIREBASE_API_KEYにウェブ API キーの値を、FIREBASE_PROJECT_IDにプロジェクト IDの値を、それぞれ設定します。
  3. FIREBASE_AUTH_SIGN_IN_OPTIONSには、使用するFirebase認証のプロバイダーをカンマ区切りで設定します。

image.png

app.config['FIREBASE_API_KEY'] = 'AIzaSyDblXUzVU9K70-a4Lw3NrW3L3DTfqBBsyE'
app.config['FIREBASE_PROJECT_ID'] = 'my-firebaseauth-4f12e'
app.config['FIREBASE_AUTH_SIGN_IN_OPTIONS'] = 'email,google'

ユーザーデーターベース(ダミー)の設定

Exampleでは、データーベースを使用せずに、ハードコーディングで代用しています。
テストで使用するFirebase認証プロバインダーの電子メールアドレスに書き換えます。

class User(UserMixin):
    # user database
    users = {
        'example01@gmail.com': {'name': 'Gmail example user1'},
        'myname@my.domain': {'name': 'My e-mail'},
        'user03@your.domain': {'name': 'debug03'},
        'user04@your.domain': {'name': 'debug04'},
    }

GAEへのデプロイ

gcloudを使って、デプロイします。

gcloud init
gcloud app deploy

※ 初回のデプロイ時にエラーが発生します。指定のurlへアクセスして、有効化してください。
image.png

デプロイ完了後に、次のコマンドで、https://[project-id].appspot.com/へアクセスできます。

gcloud app browse

クリーンアップ

このExampleで使用したリソースについて GCP アカウントに課金されないようにするために、App Engine プロジェクトを次の手順で削除してください。

  1. GCP Console で プロジェクト ページに移動します。→プロジェクト ページに移動
  2. プロジェクト リストで、削除するプロジェクトを選択し、[削除] をクリックします。
  3. ダイアログでプロジェクト ID を入力し、[シャットダウン] をクリックしてプロジェクトを削除(シャットダウン)します。

まとめ

  • Flask-FirebaseAuth-Exampleをgitクローンしました。
  • Flask-FirebaseAuthパッケージをpipインストールしました。
  • ローカル環境でデバッグ実行しました。
  • アプリケーションを設定しました。
  • GAEにデプロイし、実行しました。
  • 課金されないようにクリーンアップしました。
2
6
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
2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?