[英]Using express `app.get()` in firebase cloud functions to access files
I try to use Firebase Hosting in combination with Firebase Functions, so that only users with a valid Firebase token can access the.html-content.我尝试将 Firebase 托管与 Firebase 函数结合使用,以便只有具有有效 Firebase 令牌的用户才能访问.html-content。
I am able to send the token within my iOS-App when accessing Firebase Hosting, and my Cloud Function gets called and decodes the token sucessfully (I see 'ID Token correctly decoded' inside my Firebase Function-log).访问 Firebase 托管时,我能够在我的 iOS 应用程序中发送令牌,并且我的云 Function 被调用并成功解码令牌(我在我的 Z035489Function-log-D09241F51934 中看到“ID 令牌正确解码”)。
Afterwards the index.html should be opened, which i have inside a subfolder (/myhomepage) of my /functions folder (so not inside "/public").之后 index.html 应该打开,它位于我的 /functions 文件夹的子文件夹 (/myhomepage) 内(所以不在“/public”内)。
I always get the error inside my App-Browser: "Cannot GET /"我的 App-Browser 中总是出现错误:“Cannot GET /”
The Firebase-Functions-Log says: "Function execution took 1654 ms, finished with status code: 404" Firebase-Functions-Log 说:“函数执行耗时 1654 毫秒,完成状态码:404”
The code inside index.js: index.js 中的代码:
const admin = require("firebase-admin");
const functions = require('firebase-functions');
const express = require('express');
const cookieParser = require('cookie-parser')();
const cors = require('cors')({origin: true});
const app = express();
admin.initializeApp();
let db = admin.firestore();
// Express middleware that validates Firebase ID Tokens passed in the Authorization HTTP header.
// The Firebase ID token needs to be passed as a Bearer token in the Authorization HTTP header like this:
// `Authorization: Bearer <Firebase ID Token>`.
// when decoded successfully, the ID Token content will be added as `req.user`.
const validateFirebaseIdToken = async (req, res, next) => {
functions.logger.log('Check if request is authorized with Firebase ID token');
if ((!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) &&
!(req.cookies && req.cookies.__session)) {
functions.logger.error(
'No Firebase ID token was passed as a Bearer token in the Authorization header.',
'Make sure you authorize your request by providing the following HTTP header:',
'Authorization: Bearer <Firebase ID Token>',
'or by passing a "__session" cookie.'
);
res.status(403).send('Unauthorized');
return;
}
let idToken;
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
functions.logger.log('Found "Authorization" header');
// Read the ID Token from the Authorization header.
idToken = req.headers.authorization.split('Bearer ')[1];
} else if(req.cookies) {
functions.logger.log('Found "__session" cookie');
// Read the ID Token from cookie.
idToken = req.cookies.__session;
} else {
// No cookie
res.status(403).send('Unauthorized');
return;
}
try {
const decodedIdToken = await admin.auth().verifyIdToken(idToken);
functions.logger.log('ID Token correctly decoded', decodedIdToken);
req.user = decodedIdToken;
next();
return;
} catch (error) {
functions.logger.error('Error while verifying Firebase ID token:', error);
res.status(403).send('Unauthorized');
return;
}
};
app.use(cors);
app.use(cookieParser);
app.use(validateFirebaseIdToken);
app.get('/myhomepage', (req, res) => { // <-- The problem seems to be here
functions.logger.log('Calling get.'); // This line does not get called in my log.
res.status(200).sendFile('/index.html');
});
exports.myfunction = functions.https.onRequest(app);
How do I access content inside "/functions/myhomepage" after the token was successfully decoded?令牌成功解码后,如何访问“/functions/myhomepage”中的内容?
Normally you can end only an HTTP function with send()
, redirect()
, or end()
, see the doc .通常,您只能使用send()
、 redirect()
或end()
HTTP function ,请参阅文档。
I've never tried, but I don't think using sendFile()
would work.我从未尝试过,但我认为使用sendFile()
不会起作用。 If you want to redirect the user to a static page available through Firebase Hosting, you should use redirect()
.如果您想将用户重定向到通过 Firebase 托管提供的 static 页面,您应该使用redirect()
。 The other possibility is to pass an HTML string to the send()
method, see here .另一种可能性是将 HTML 字符串传递send()
方法,请参见此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.