我目前正在研究 Next.js (React) 项目,在该项目中我使用 Firebase Auth 进行身份验证。我使用它连接到 REST API 后端,后者接收 Firebase 提供的用户令牌(通过getIdToken())。
因为 IdToken 不时发生变化,所以我目前在发送这样的fetch请求之前正在请求最新的 IdToken :
const fetcher = (url: string) => {
return user.getIdToken().then((token) =>
fetch(url, {
method: "GET",
headers: new Headers({
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
}),
}).then((res) => res.json())
);
};
Run Code Online (Sandbox Code Playgroud)
这种设置实际上有效,但我想知道它是否被认为是高效/最佳实践?我看到很多使用 IdToken 设置 cookie 的示例(例如firebase docs、next.js 示例)。
我可以getIdToken()理解为什么在使用 SSR 时,因为不能在那里调用。但我的应用程序只使用客户端数据获取。放弃目前使用 cookie 的方法对我有什么好处吗?
我正在使用google-cloud-functions在将文件上传到Google云端存储时触发缩略图创建。
exports.generateThumbnail = functions.storage.object().onFinalize(async (object) => {
// Removed code not relevant to the problem
// Download file from bucket.
await file.download({destination: tempLocalFile});
const uploadPromises = sizes.map(async size => {
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);
// Generate a thumbnail using Sharp.
await sharp(tempLocalFile).resize(width, height).toFile(tempLocalThumbFile);
console.log('Thumbnail created at', tempLocalThumbFile);
return thumbFile.getSignedUrl(config);
});
// Get the Signed URLs for the original image.
const results = await Promise.all([
uploadPromises,
file.getSignedUrl(config),
]);
console.log(results[0]);
});
Run Code Online (Sandbox Code Playgroud)
我希望最后一个console.log输出的是大小为4的数组,其中每个元素都包含getSignedurl()函数的回调。相反,我现在每次得到的是大小为4的数组,其中所有元素都是promise。
由于我已经在等待Promise.all(),所以我做错了什么才能导致这个问题?
javascript asynchronous node.js es6-promise google-cloud-functions
asynchronous ×1
cookies ×1
es6-promise ×1
firebase ×1
javascript ×1
next.js ×1
node.js ×1
reactjs ×1