前提・実現したいこと
ユーザー情報をFirebaseに保存したい
ここに質問の内容を詳しく書いてください。
Next.jsとReact、react-firebaseui でログイン機能の実装をしています
メールアドレスを記入し、ログインボタンを押すと
Warning: Firebase is already defined in the global scope. Please make sure Firebase library is only loaded once. react_devtools_backend.js:2273 FirebaseError: Installations: Create Installation request failed with error "400 INVALID_ARGUMENT: API key not valid. Please pass a valid API key."
とのエラーが出ます
APIキーが正しいことは確認したのですがなぜか解決せず、
該当のソースコード
initFirebase.js import firebase from 'firebase/app' import 'firebase/auth' const config = { apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY, authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN, databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL, projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET, messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID, measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID, } export default function initFirebase() { if (!firebase.apps.length) { firebase.initializeApp(config) } }
firebaseAuth.js import * as admin from 'firebase-admin' export const verifyIdToken = (token) => { const firebasePrivateKey = process.env.FIREBASE_PRIVATE_KEY if (!admin.apps.length) { admin.initializeApp({ credential: admin.credential.cert({ projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, clientEmail: process.env.FIREBASE_CLIENT_EMAIL, apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY, authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN, databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL, projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET, messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID, measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID, // https://stackoverflow.com/a/41044630/1332513 privateKey: firebasePrivateKey, }), databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL, }) } return admin .auth() .verifyIdToken(token) .catch((error) => { throw error }) }
FirebaseAuth.js /* globals window */ import { useEffect, useState } from 'react' import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth' import firebase from 'firebase/app' import 'firebase/auth' import initFirebase from '../utils/auth/initFirebase' import { setUserCookie } from '../utils/auth/userCookies' import { mapUserData } from '../utils/auth/mapUserData' // Init the Firebase app. initFirebase() const firebaseAuthConfig = { signInFlow: 'popup', // Auth providers // https://github.com/firebase/firebaseui-web#configure-oauth-providers signInOptions: [ { provider: firebase.auth.EmailAuthProvider.PROVIDER_ID, requireDisplayName: false, }, ], signInSuccessUrl: '/', credentialHelper: 'none', callbacks: { signInSuccessWithAuthResult: async ({ user }, redirectUrl) => { const userData = mapUserData(user) setUserCookie(userData) }, }, } const FirebaseAuth = () => { // Do not SSR FirebaseUI, because it is not supported. // https://github.com/firebase/firebaseui-web/issues/213 const [renderAuth, setRenderAuth] = useState(false) useEffect(() => { if (typeof window !== 'undefined') { setRenderAuth(true) } }, []) return ( <div> {renderAuth ? ( <StyledFirebaseAuth uiConfig={firebaseAuthConfig} firebaseAuth={firebase.auth()} /> ) : null} </div> ) } export default FirebaseAuth
next.config.js import Head from 'next/head' import { useEffect } from 'react' import { useUser } from '../context/userContext' import firebase from '../firebase/clientApp' import Link from 'next/link' require('dotenv').config(); const value = process.env.NEXT_PUBLIC_FIREBASE_API_KEY;コンソールで出力 console.log(value); export default function Home() { // Our custom hook to get context values const { loadingUser, user } = useUser() useEffect(() => { if (!loadingUser) { // You know that the user is loaded: either logged in or out! console.log('user:', user) } // You also have your firebase app initialized console.log('firebase:', firebase) }, [loadingUser, user]) return ( <div className="container"> <Head> <title>Next.js w/ Firebase Client-Side</title> <link rel="icon" href="/favicon.ico" /> </Head> <Link href="./auth"> <a>auth</a> </Link> <main> <h1 className="title">Next.js w/ Firebase Client-Side</h1> <p className="description">Fill in your credentials to get started</p> <div>currentUser:{user}</div> </main> <script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-app.js"></script> <script src="/__/firebase/7.18.0/firebase-installations.js"></script> <script src="https://www.gstatic.com/firebasejs/7.19.1/firebase-analytics.js"></script> <script src="/__/firebase/init.js"></script> </div> ) }
あなたの回答
tips
プレビュー