next.js 无法解析私钥:错误:PEM格式的消息无效

goucqfw6  于 2023-01-20  发布在  其他
关注(0)|答案(2)|浏览(259)

我尝试在Vercel上部署使用firebase-admin的NEXT JS应用程序。

import * as firebaseAdmin from 'firebase-admin';
import firebase from 'firebase/app';

if (!firebaseAdmin.apps.length) {
  firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert({
      privateKey: process.env.NEXT_PUBLIC_FIREBASE_PRIVATE_KEY,

      clientEmail: process.env.NEXT_PUBLIC_FIREBASE_CLIENT_EMAIL,

      projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
    }),

    databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
  });
}

当我使用环境变量将其部署到vercel时:

NEXT_PUBLIC_FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n....\n-----END PRIVATE KEY-----\n"

我收到此错误消息。

> Build error occurred
Error: Certificate object must contain a string "private_key" property.
    at FirebaseAppError.FirebaseError [as constructor] (/vercel/path0/node_modules/firebase-admin/lib/utils/error.js:42:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (/vercel/path0/node_modules/firebase-admin/lib/utils/error.js:88:28)
    at new FirebaseAppError (/vercel/path0/node_modules/firebase-admin/lib/utils/error.js:122:28)
    at new Certificate (/vercel/path0/node_modules/firebase-admin/lib/auth/credential.js:118:19)
    at new CertCredential (/vercel/path0/node_modules/firebase-admin/lib/auth/credential.js:187:64)
    at Object.cert (/vercel/path0/node_modules/firebase-admin/lib/firebase-namespace.js:220:58)
    at Object.3996 (/vercel/path0/.next/server/chunks/241.js:115:76)
    at __webpack_require__ (/vercel/path0/.next/server/webpack-runtime.js:25:42)
    at /vercel/path0/.next/server/pages/dashboard.js:124:81
    at Function.__webpack_require__.a (/vercel/path0/.next/server/webpack-runtime.js:103:13) {
  type: 'FirebaseAppError',
  errorInfo: {
    code: 'app/invalid-credential',
    message: 'Certificate object must contain a string "private_key" property.'
  },
  codePrefix: 'app'
}
Error! Command "npm run build" exited with 1
Error: Command "vercel build" exited with 1

但是,运行npm run build构建时本地没有错误。

.env.local
info  - Linting and checking validity of types  
info  - Creating an optimized production build  
info  - Compiled successfully
info  - Collecting page data  
info  - Generating static pages (4/4)
info  - Finalizing page optimization  

Page                                       Size     First Load JS
┌ ○ / (466 ms)                             461 B           271 kB
├   /_app                                  0 B             270 kB
├ ○ /404 (450 ms)                          526 B           271 kB
├ λ /api/hello                             0 B             270 kB

+ First Load JS shared by all              270 kB
  ├ chunks/framework-a87821de553db91d.js   45 kB
  ├ chunks/main-567f0ec5ceee81fc.js        29.7 kB
  ├ chunks/pages/_app-b443b414c9e8f379.js  195 kB
  ├ chunks/webpack-42cdea76c8170223.js     1.07 kB
  └ css/966e875c066cf46b.css               5.18 kB

λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
○  (Static)  automatically rendered as static HTML (uses no initial props)

我不知道如何修复这个问题,因为它在本地运行,并且在构建时没有任何错误。

plupiseo

plupiseo1#

我找到了答案。.env.local可以具有如下值

NEXT_PUBLIC_FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n....\n-----END PRIVATE KEY-----\n"

但在vercel环境变量中,添加的键不带双引号""

NEXT_PUBLIC_FIREBASE_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\n....\n-----END PRIVATE KEY-----\n

当你添加键的时候,它应该重新格式化自己,看起来像这样。你可以在本地console.log这些值,看起来像下面这样,然后在Vercel env中粘贴完全相同的值。

-----BEGIN PRIVATE KEY-----
your
secret
key
-----END PRIVATE KEY-----
6kkfgxo0

6kkfgxo02#

我也遇到过这个问题。去掉引号并没有多大帮助。但是,我的评论与另一件事有关:您正在将私钥设置为NEXT_PUBLIC var(可通过前端访问,因此是公开的)。
就这些。如果不相关的话很抱歉。

相关问题