Last active
August 26, 2019 05:16
-
-
Save nelsonenzo/afc4e7af0a518aeb9221d55eb01d6eac to your computer and use it in GitHub Desktop.
Firebase authentication with a python Flask backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */ | |
"rules": { | |
".read": "auth != null && auth.provider == 'anonymous' && auth.uid == 'SERVICE-ACCOUNT-CLIENT-ID'", | |
".write": "auth != null && auth.provider == 'anonymous' && auth.uid == 'SERVICE-ACCOUNT-CLIENT-ID'" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask ## basic lib required for flask | |
import firebase_admin ## firebase admin library | |
from firebase_admin import credentials ## firebase credentials for authentication | |
from firebase_admin import db ## firebase db for all operations | |
## initialize the flask app | |
app = Flask(__name__) | |
## initialize the firebase connection using your service account json file. | |
cred = credentials.Certificate(app.config.get('/local/path/to/firebase/service_account.json')) | |
firebase_admin.initialize_app(cred, { | |
'databaseURL': 'https://your-firbase-db-name.firebaseio.com' | |
}) | |
## create a reference to the root element. | |
## We will use this root in conjunction with .child() for all operations. | |
FIREBASEDB = db.reference() | |
@app.route("/demo", methods=['GET']) | |
def demo_read_write(): | |
FIREBASEDB.child('/test/doc').push({'testkey':'testvalue'}) | |
thedoc = FIREBASEDB.child('/test/doc').get() | |
return jsonify(thedoc) | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment