Firebase
Learn how to integrate Clerk into your Firebase application
Getting Started
To enable the integration, you will need to provide Clerk with the required Firebase configuration attributes depending on the Firebase features you would require authenticated user access to.
To get started, turn on the Firebase integration in the Clerk Dashboard:
Your Application & instance → Integrations → Firebase
Check out our example application showcasing how to integrate Clerk with Firebase: https://github.com/clerkinc/clerk-firebase-starter
Integration configuration
To allow Clerk to generate the required authentication token for Firebase, you will need to set some configuration attributes on the Clerk integration page.
1. (Easy way) Upload a JSON Firebase configuration file
You can upload the JSON configuration file you will retrieve from Firebase directly on the integrations page and the required fields will be filled in automatically.
- To get the Firebase JSON configuration file, navigate to your Project Settings → Service Accounts → Firebase Admin SDK and then click the Generate new private key button.
- To upload the file on Clerk, use the Upload JSON button on the Firebase integration screen inside the configuration modal:
2. Fill in the required attributes manually
The attributes that you need to fill in to connect Clerk with Firebase, as a custom authentication system, are described below:
- Service account ID The service account ID can be found in the Google Cloud Console for your Firebase project, or in the
client_email
field of a service account JSON file. - Project ID The Firebase project ID is the unique identifier of your Firebase project. Can be found under the Project Settings in the Firebase platform.
- Database URL (Optional)The Firebase Realtime Database URL as retrieved from the Firebase platform under the Realtime Database page.
- Private Key The private key used for signing which belongs to the Google service account ID of your project. Can be found in the Google Cloud Console for your Firebase project, or in the
private_key
field of a service account JSON file.
Firebase user sign-in with Clerk as an authentication provider
After successfully completing the integration setup on your dashboard, you should setup your frontend to connect Clerk with the Firebase authentication system.
1. Retrieve a Firebase user authentication token from Clerk
The Firebase Web SDK requires an authentication token to sign in your users using Clerk as the custom authentication provider. This token can be retrieved calling the getToken
method returned from the useAuth()
hook and calling it with the "integration_firebase"
template parameter.
1await getToken({ template: "integration_firebase" });
The above method will return the token needed for the Firebase Web SDK to sign in your users with Clerk.
2. Sign-in using the Firebase Web SDK
The Firebase Web SDK referenced is the browser installation of the official Firebase JavaScript SDK package on npm.
To authenticate your users on Firebase using Clerk you would need to call the signInWithCustomToken
method of the Firebase Auth scope.
1import { getAuth, signInWithCustomToken } from "firebase/auth";23const auth = getAuth();4await signInWithCustomToken(auth, clerkCustomToken);
1import firebase from "firebase";23await firebase.auth().signInWithCustomToken(clerkCustomToken);
An example implementation using Next.js:
pages/index.js1import { useAuth } from "@clerk/nextjs";2import { initializeApp } from "firebase/app";3import { getAuth, signInWithCustomToken } from "firebase/auth";4import { useEffect } from "react";5import firebaseConfig from "../lib/firebase.config";67// Initialize Firebase app with config8initializeApp(firebaseConfig);910export default function Home() {11const { getToken } = useAuth();1213useEffect(() => {14const signInWithClerk = async () => {15const auth = getAuth();16const token = await getToken({ template: "integration_firebase" });17const userCredentials = await signInWithCustomToken(auth, token);1819/**20* The userCredentials.user object will call the methods of21* the Firebase platform as an authenticated user.22*/23console.log("user ::", userCredentials.user);24};2526signInWithClerk();27}, []);2829return (30<main>31<h1>Hello Clerk + Firebase!</h1>32</main>33);34}35