Firebase Auth+;Python后端

Firebase Auth+;Python后端,python,firebase,bottle,firebase-admin,Python,Firebase,Bottle,Firebase Admin,我将使用Firebase Auth和数据库模块创建我的web应用程序。然而,并不是所有我想让我的应用程序做的事情都能在前端实现。因此,我还想使用后端和Python的瓶子框架来处理请求,并使用Pyrebase访问Firebase数据库。 比如说,登录后,我需要转到主页并查看个性化内容,例如我的笔记。它们在DB中的结构如下: { "notes": [{ "id": "1", "title": "X", "author": "user1"

我将使用Firebase Auth和数据库模块创建我的web应用程序。然而,并不是所有我想让我的应用程序做的事情都能在前端实现。因此,我还想使用后端和Python的瓶子框架来处理请求,并使用Pyrebase访问Firebase数据库。
比如说,登录后,我需要转到主页并查看个性化内容,例如我的笔记。它们在DB中的结构如下:

{
    "notes": [{
        "id": "1",
        "title": "X",
        "author": "user1"
    },
    {
        "id": "2",
        "title": "Y",
        "author": "user2"
    } and so on... ]
}
那么,如何实现只在主页上显示我的文章呢? 我知道我需要根据
作者
值筛选笔记,但如何让瓶子了解当前登录的用户?

我已经读到,我应该以某种方式将唯一令牌发送到后端服务器以验证当前用户,但如何做到这一点呢?在每个链接中插入标记作为GET参数似乎很愚蠢,但我看不到其他实现方法。

首先组织数据库,使每个注释成为子对象:

{
  "notes": {
    "id1": {
      "id": "id1",
      "title": "X",
      "author": "user1",
    },
    "id2": {

    }
  }
}
然后这个特定的交互可以完全在客户端实现。只需执行一个查询来过滤所需的注释。例如,在JS客户端中:

var uid = firebase.auth().currentUser.uid;
var query = ref.orderByChild('author').equalTo(uid);
// Listen for query value events
如果要在后端服务器上运行此操作,并且要确保仅允许登录用户执行此操作,则必须在每次请求时将ID令牌从客户端应用传递到服务器。以下是如何使用Python Admin SDK实现服务器端逻辑:

import firebase_admin
from firebase_admin import auth
from firebase_admin import db

token = '....' # Extract from the client request
try:
    decoded = auth.verify_id_token(token)
    uid = decoded.uid
    ref = db.reference('path/to/notes')
    notes = ref.order_by_child('author').equal_to(uid).get()
    # Process notes response
except ValueError as ex:
    print(ex)
    # Send error to client

首先组织数据库,使每个注释成为子对象:

{
  "notes": {
    "id1": {
      "id": "id1",
      "title": "X",
      "author": "user1",
    },
    "id2": {

    }
  }
}
然后这个特定的交互可以完全在客户端实现。只需执行一个查询来过滤所需的注释。例如,在JS客户端中:

var uid = firebase.auth().currentUser.uid;
var query = ref.orderByChild('author').equalTo(uid);
// Listen for query value events
如果要在后端服务器上运行此操作,并且要确保仅允许登录用户执行此操作,则必须在每次请求时将ID令牌从客户端应用传递到服务器。以下是如何使用Python Admin SDK实现服务器端逻辑:

import firebase_admin
from firebase_admin import auth
from firebase_admin import db

token = '....' # Extract from the client request
try:
    decoded = auth.verify_id_token(token)
    uid = decoded.uid
    ref = db.reference('path/to/notes')
    notes = ref.order_by_child('author').equal_to(uid).get()
    # Process notes response
except ValueError as ex:
    print(ex)
    # Send error to client