我正在使用 flutter 应用程序中的 firebase auth,该应用程序从前端获取 jwt,如下所示:
accessToken = await user!.getIdToken(true);
Run Code Online (Sandbox Code Playgroud)
现在我希望我的 .net 5 AspNetCore 后端应用程序验证 id 令牌。
我将 firebase admin SDK 添加到我的 .net 应用程序中,如下所示:
在startup.cs:
FirebaseApp.Create();
Run Code Online (Sandbox Code Playgroud)
Firebase文档显示了如何验证 id 令牌:
FirebaseToken decodedToken = await FirebaseAuth.DefaultInstance
.VerifyIdTokenAsync(idToken);
string uid = decodedToken.Uid;
Run Code Online (Sandbox Code Playgroud)
但是这段代码去哪里了呢?我不想为我的 AspNetCore 应用程序中的每个控制器端点手动执行此操作。这可以作为中间件在每个 api 请求上自动发生吗?
编辑:Eeek 我什至不认为 Firebase Admin 是我想要的。我只想验证使用 Firebase Auth for Flutter 在客户端上创建的 jwt id 令牌,以尽可能最 firebasey 的方式。它不适用于管理员,适用于普通用户。我以为 Firebase Admin 就是这样。我应该朝什么方向发展?我在网上找到了各种关于不同策略的文章,所以很困惑哪一个是正确的。这足够了吗?https://dominique-k.medium.com/using-firebase-jwt-tokens-in-asp-net-core-net-5-834c43d4aa00它看起来不是很火,而且似乎过于简化?我认为下面 Dharmaraj 的答案是最火爆的方式,但我不确定从哪里idToken获取值(如何从请求标头获取?否则它只是空)。
有没有人知道如何在管理服务器执行的写入中强制执行Firebase中的安全规则?
例如:
userDetail:{".invalidate"://基于数据库引用的某些安全规则//}
我想确保管理员服务器的写入符合此验证规则.我知道可以在服务器本身上安装一个安全规则,但我想确保数据与数据库中可由其他服务器实例修改的另一个位置保持一致.
firebase firebase-security firebase-realtime-database firebase-admin
目前,我正在使用Firebase Admin SDK连接NodeJS服务器端应用程序中的Firebase数据库.
但我找不到通过代理设置连接Firebase的选项,或者它可以检测我的系统HTTP_PROXY环境变量.
当我运行节点脚本时node index.js,得到一些这样的超时消息(我知道在我的工作网络中,我无法直接连接到Firebase).
Error: Credential implementation provided to initializeApp() via the "credential
" property failed to fetch a valid Google OAuth2 access token with the following
error: "connect ETIMEDOUT 216.58.200.237:443".
at ....erver\node_modules\firebase-adm
in\lib\firebase-app.js:74:23
at process._tickCallback (internal/process/next_tick.js:103:7)
Run Code Online (Sandbox Code Playgroud)
我也使用浏览器通过代理访问firebase控制台,它的工作原理.
但是如何在NodeJS服务器端脚本中解决这个问题?
proxy node.js firebase firebase-realtime-database firebase-admin
要在Firebase数据库中写入数据,我在我的Android应用程序中使用setValue().
我的问题是:如果同时我将通过Admin API更改值,变量的值是否会发生变化?
如何在firebase管理SDK中获取currentUser.在客户端SDK中我们可以使用
onAuthStateChanged() or firebase.auth().currentUser()
Run Code Online (Sandbox Code Playgroud)
在NodeJs Server.js文件中,firebase 管理员SDK onAuthStateChanged()不起作用,这里有什么用?
基本上我只想登录用户访问我的innerPages所以我使用express,nodejs app.get方法来创建路由.
app.get('/', authenticationRequired ,function(req,res){
res.sendFile(path.join(__dirname+'/index.html'));
});
Run Code Online (Sandbox Code Playgroud)
并尝试编写authenticationRequired方法(根据用户是否登录,返回false或true).
我正在使用单个firebase功能向我的Android用户发送多个推送通知.
每个通知的详细信息都会在我们的Firebase数据库中写下它们应发送的日期和小时.我使用cron通过http请求触发此Firebase功能.
我的问题是Firebase功能应该返回什么才能完成所有Firebase节点的循环(代表通知)并完成发送所有通知?
这就是我现在拥有的.它在发送第一个通知后退出.我想要的是它完成所有Firebase子节点的查看并为每个节点发送通知.
我不明白Promises的想法足以弄清楚在多个Push Notifications的情况下返回什么.我几乎完全忘记了如何编写javascript也没有帮助.
exports.sendHourlyNotifications = functions.https.onRequest((req, res) => {
const dateStr = req.query.date;
const hourStr = req.query.hour;
console.log('sendHourlyNotifications', 'date: ' + dateStr + " hour: " + hourStr);
const parentRef = admin.database().ref();
const notifRef = parentRef.child('all-notifications');
const hourRef = notifRef.child(dateStr).child(hourStr);
return hourRef.once('value').then(snapshot => {
const updates = {};
snapshot.forEach(function(childSnapshot) {
const deviceToken = childSnapshot.child("deviceToken").val();
console.log('sendHourlyNotifications', 'childSnapshot Key: ' + childSnapshot.key);
const title = childSnapshot.child("title").val();
const body = childSnapshot.child("body").val();
console.log('sendHourlyNotifications', "DeviceToken: " + deviceToken + " …Run Code Online (Sandbox Code Playgroud) node.js firebase-realtime-database google-cloud-functions firebase-cloud-messaging firebase-admin
我正在尝试将图像存储到Firebase存储中,我正在使用node.js。
import * as admin from 'firebase-admin';
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://xxx-app.firebaseio.com',
storageBucket: 'xxxx-app.appspot.com'
});
function storeHeadShots(){
const bucket = admin.storage().bucket();
bucket.upload('./headshots/Acker.W.png', function(err, file){
if (!err){
console.log('file uploaded')
} else {
console.log('error uploading image: ', err)
}
});
}
storeHeadShots();
Run Code Online (Sandbox Code Playgroud)
现在,上面的方法可以正常工作,但是我在存储中有一个文件夹,users/并且在此文件夹中我正在存储图像。如何访问该文件夹?
当我尝试为用户设置自定义声明时,我无法部署云功能.这是功能
exports.verifyPhone = functions.https.onRequest(async (request, response) => {
const db = admin.firestore();
const userId = request.body['userId'];
const firebaseId = request.body['firebaseId'];
const now = Date.now();
const userRef = db.collection('users').doc(userId);
const updateResult = await userRef.update({
phoneVerified: true
});
const x = await admin.auth().setCustomUserClaims(firebaseId,{folder:userId});
console.log('folder set');
if (Number(updateResult.writeTime) > now) {
return response.json({
status: 1,
message: 'Phone verified successfully',
result: null
});
} else {
return response.json({
status: 0,
message: 'An error occurred, please try again later',
result: null
});
}
});
Run Code Online (Sandbox Code Playgroud)
但如果行 …
与MongoDB类似,是否可以使用Firestore在添加/更新文档后取回文档而无需额外的网络调用?
我发现先打电话给添加/更新文档,然后再打电话给它,这很愚蠢。
Firebase是否有任何技巧{ merge: true }可以设置额外/更多自定义声明而不删除/覆盖旧变量?
复制步骤:
admin.auth().setCustomUserClaims(uid, { a: 'value' }) // Run this first
admin.auth().setCustomUserClaims(uid, { b: 'value' }) // Then run this after
Run Code Online (Sandbox Code Playgroud)
结果:
{ b: 'value'}
Run Code Online (Sandbox Code Playgroud)
预期结果:
{ a: 'value', b: 'value' }
Run Code Online (Sandbox Code Playgroud)
还是我做错了什么?
firebase-admin ×10
firebase ×8
node.js ×5
.net-5 ×1
asp.net5 ×1
c# ×1
express ×1
proxy ×1
typescript ×1