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
app, err := firebase.NewApp(ctx, nil) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
fcm, err := app.Messaging(ctx) | |
if err != nil { | |
log.Fatalln(err) | |
} |
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
import firebase_admin | |
from firebase_admin import db | |
from firebase_admin import messaging | |
# Load Firebase configuration from environment. | |
firebase_admin.initialize_app() | |
users_ref = db.reference('users') | |
# Query for the users whose settings/alerts field is set to `true`. | |
users = users_ref.order_by_child('settings/alerts').equal_to(True).get() |
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
import firebase_admin | |
from firebase_admin import auth | |
import flask | |
firebase_admin.initialize_app() | |
app = flask.Flask(__name__) | |
@app.route('/revoke', methods=['POST']) | |
def revoke_tokens(): | |
uid = flask.request.form['uid'] |
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 firebase_admin import auth | |
import flask | |
app = flask.Flask(__name__) | |
@app.route('/api') | |
def backend_operation(): | |
id_token = flask.request.headers['X-Firebase-ID-Token'] | |
try: | |
# Check if the ID token is valid. Additionally, check if it was issued |
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
ctx := context.Background() | |
config := &firebase.Config{ | |
DatabaseURL: "https://database-name.firebaseio.com", | |
} | |
app, err := firebase.NewApp(ctx, config) | |
if err != nil { | |
log.Fatal(err) | |
} | |
client, err := app.Database(ctx) |
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
var acc Account | |
if err := client.NewRef("accounts/alice").Get(ctx, &acc); err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("%s has a balance of %.2f\n", acc.Name, acc.Balance) |
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
// Transaction update handler: This may get invoked multiple times due to retries. | |
withdraw100 := func(tn db.TransactionNode) (interface{}, error) { | |
// Read the current state of the node. | |
var acc Account | |
if err := tn.Unmarshal(&acc); err != nil { | |
return nil, err | |
} | |
// Mutate the state in memory. | |
if acc.Balance < 100.0 { | |
// Abort the transaction at anytime by returning an error. |
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
q := client.NewRef("accounts").OrderByChild("balance").LimitToLast(3) | |
var result map[string]Account | |
if err := q.Get(ctx, &result); err != nil { | |
log.Fatal(err) | |
} | |
// Results will be logged in no specific order. | |
for key, acc := range result { | |
log.Printf("%s => %v\n", key, acc) | |
} |
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
q := client.NewRef("accounts").OrderByChild("balance").LimitToLast(3) | |
result, err := q.GetOrdered(ctx) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Results will be logged in the increasing order of balance. | |
for _, r := range result { | |
var acc Account | |
if err := r.Unmarshal(&acc); err != nil { | |
log.Fatal(err) |
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
import flask | |
import firebase_admin | |
from firebase_admin import auth | |
from firebase_admin import credentials | |
cred = credentials.Certificate('serviceAccount.json') | |
firebase_admin.initialize_app(cred) | |
app = flask.Flask(__name__) | |
@app.route('/sessionInit', methods=['POST']) |