我们很快就会将该项目投入生产。
1 - 我们的移动应用程序将通过将其发布到我们的内部微服务器来创建汇款。此类 post 请求将返回从我们的内部 NodeJs 服务器生成的 CustomToken。
2 - 我们的内部微服务会将此类传输复制到 Firestore 并相应地更新其在 Firestore 上的状态。
3 - 不是我们的移动应用程序轮询或侦听我们的内部微服务来获取状态,而是侦听 Firestore 以从相应文档获取状态。为了监听,它将使用从步骤 1 中的帖子返回的 CustomToken。我们公司希望仅利用 Google Firestore 的实时数据库功能来实现此项目(反应式方法)。
将我所做的与以下声明进行比较时,您是否看到任何考虑/问题:“Google 在大多数情况下更喜欢您授权使用服务帐户”? (复制自其他相关讨论 https://stackoverflow.com/a/61665443/4148175)
自定义令牌是使用此 NodeJs 服务器在内部创建的,并取决于从经过身份验证的用户中提取的 uid来自 Google Firebase 的身份验证/用户 https://console.firebase.google.com/project/firetestjimis/authentication/users
const admin = require('firebase-admin');
exports.serviceAccount = {
"type": "service_account",
"project_id": "firetestjimis",
"private_key_id": "ecfc6 ... fd05923",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIE .... 5EKvQ==\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected] /cdn-cgi/l/email-protection",
"client_id": "102422819688924138150",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-fg6p9%40firetestjimis.iam.gserviceaccount.com"
}
admin.initializeApp({
credential: admin.credential.cert(exports.serviceAccount)
});
var uid = "NS .... Ro2"; //copied from https://console.firebase.google.com/project/firetestjimis/authentication/users
var claim = {
control: true
};
admin.auth().createCustomToken(uid)
.then(function (customToken) {
console.log(customToken)
})
.catch(function (error) {
console.log("Error creating custom token:", error);
});
我们的手机(Angular 中的示例,但 IOS 和 Android 的想法相同)具有我下载的 SERVICE_ACCOUNT_JSON_FILE,如下所示:
环境.ts:
export const environment = {
production: false,
firebaseConfig: {
apiKey: "AIzaSy ... 3DCGihK3xs",
authDomain: "firetestjimis.firebaseapp.com",
databaseURL: "https://firetestjimis.firebaseio.com",
projectId: "firetestjimis",
storageBucket: "firetestjimis.appspot.com",
messagingSenderId: "795318872350",
appId: "1:7953 ... 32b26fb53dc810f"
}
};
应用程序组件.ts
public transfers: Observable<any[]>;
transferCollectionRef: AngularFirestoreCollection<any>;
constructor(public auth: AngularFireAuth, public db: AngularFirestore) {
this.listenSingleTransferWithToken();
}
async listenSingleTransferWithToken() {
await this.auth.signInWithCustomToken("eyJh ### CUSTOMTOKEN GENERATED FROM INTERNAL NODEJS SERVER ABOVE ### CVg");
this.transferCollectionRef = this.db.collection<any>('transfer', ref => ref.where("id", "==", "1"));
this.transfers = this.transferCollectionRef.snapshotChanges().map(actions => {
return actions.map(action => {
const data = action.payload.doc.data();
const id = action.payload.doc.id;
return { id, ...data };
});
});
}
}
据我了解,CustomToken 的创建及其在移动设备上的使用完全依赖于服务帐户。我对吗?我是否错过了一些概念,并且我在幕后使用用户凭据,并且在开发环境中正常工作的东西在生产中会弹出一些惊喜?显然,这个问题全部来自我的免费帐户,但在生产中它将是付费帐户,但这里的代码和步骤将完全相同。
*** 在约翰评论后编辑
事实上,environment.ts 会转到浏览器。如果有问题,也许有听 Firestore 文档经验的 Angular 开发人员可以发表评论