Skip to content

Instantly share code, notes, and snippets.

@nelsonenzo
Last active August 26, 2019 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsonenzo/afc4e7af0a518aeb9221d55eb01d6eac to your computer and use it in GitHub Desktop.
Save nelsonenzo/afc4e7af0a518aeb9221d55eb01d6eac to your computer and use it in GitHub Desktop.
Firebase authentication with a python Flask backend
{
/* 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'"
}
}
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