跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Firebase

The @mastra/auth-firebase package provides authentication for Mastra using Firebase Authentication. It verifies incoming requests using Firebase ID tokens and integrates with the Mastra server using the auth option.

Prerequisites
Prerequisites的直接链接

This example uses Firebase Authentication. Make sure to:

  1. Create a Firebase project in the Firebase Console
  2. Enable Authentication and configure your preferred sign-in methods (Google, Email/Password, etc.)
  3. Generate a service account key from Project Settings > Service Accounts
  4. Download the service account JSON file
.env
FIREBASE_SERVICE_ACCOUNT=/path/to/your/service-account-key.json
FIRESTORE_DATABASE_ID=(default)
# Alternative environment variable names:
# FIREBASE_DATABASE_ID=(default)
备注

Store your service account JSON file securely and never commit it to version control.

Installation
Installation的直接链接

Before you can use the MastraAuthFirebase class you have to install the @mastra/auth-firebase package.

npm install @mastra/auth-firebase@latest

Usage examples
Usage examples的直接链接

Basic usage with environment variables
Basic usage with environment variables的直接链接

If you set the required environment variables (FIREBASE_SERVICE_ACCOUNT and FIRESTORE_DATABASE_ID), you can initialize MastraAuthFirebase without any constructor arguments. The class will automatically read these environment variables as configuration:

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { MastraAuthFirebase } from '@mastra/auth-firebase'

// Automatically uses FIREBASE_SERVICE_ACCOUNT and FIRESTORE_DATABASE_ID env vars
export const mastra = new Mastra({
server: {
auth: new MastraAuthFirebase(),
},
})

Custom configuration
Custom configuration的直接链接

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { MastraAuthFirebase } from '@mastra/auth-firebase'

export const mastra = new Mastra({
server: {
auth: new MastraAuthFirebase({
serviceAccount: '/path/to/service-account.json',
databaseId: 'your-database-id',
}),
},
})

Configuration
Configuration的直接链接

The MastraAuthFirebase class can be configured through constructor options or environment variables.

Environment variables
Environment variables的直接链接

  • FIREBASE_SERVICE_ACCOUNT: Path to Firebase service account JSON file
  • FIRESTORE_DATABASE_ID or FIREBASE_DATABASE_ID: Firestore database ID
备注

When constructor options aren't provided, the class automatically reads these environment variables. As a result, you can call new MastraAuthFirebase() without any arguments if your environment variables are properly configured.

User Authorization
User Authorization的直接链接

By default, MastraAuthFirebase uses Firestore to manage user access. It expects a user_access collection with documents keyed by user UIDs. The presence of a document in this collection determines whether a user is authorized.

firestore-structure.txt
user_access/
{user_uid_1}/ // Document exists = user authorized
{user_uid_2}/ // Document exists = user authorized

To customize user authorization, provide a custom authorizeUser function:

src/mastra/auth.ts
import { MastraAuthFirebase } from '@mastra/auth-firebase'

const firebaseAuth = new MastraAuthFirebase({
authorizeUser: async user => {
// Custom authorization logic
return user.email?.endsWith('@yourcompany.com') || false
},
})

Visit MastraAuthFirebase for all available configuration options.

Client-side setup
Client-side setup的直接链接

When using Firebase auth, you'll need to initialize Firebase on the client side, authenticate users, and retrieve their ID tokens to pass to your Mastra requests.

Setting up Firebase on the client
Setting up Firebase on the client的直接链接

First, initialize Firebase in your client application:

lib/firebase.ts
import { initializeApp } from 'firebase/app'
import { getAuth, GoogleAuthProvider } from 'firebase/auth'

const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
}

const app = initializeApp(firebaseConfig)
export const auth = getAuth(app)
export const googleProvider = new GoogleAuthProvider()

Authenticating users and retrieving tokens
Authenticating users and retrieving tokens的直接链接

Use Firebase authentication to sign in users and retrieve their ID tokens:

lib/auth.ts
import { signInWithPopup, signOut, User } from 'firebase/auth'
import { auth, googleProvider } from './firebase'

export const signInWithGoogle = async () => {
try {
const result = await signInWithPopup(auth, googleProvider)
return result.user
} catch (error) {
console.error('Error signing in:', error)
throw error
}
}

export const getIdToken = async (user: User) => {
try {
const idToken = await user.getIdToken()
return idToken
} catch (error) {
console.error('Error getting ID token:', error)
throw error
}
}

export const signOutUser = async () => {
try {
await signOut(auth)
} catch (error) {
console.error('Error signing out:', error)
throw error
}
}
备注

Refer to the Firebase documentation for other authentication methods like email/password, phone authentication, and more.

Configuring MastraClient
configuring-mastraclient的直接链接

When auth is enabled, all requests made with MastraClient must include a valid Firebase ID token in the Authorization header:

lib/mastra/mastra-client.ts
import { MastraClient } from '@mastra/client-js'

export const createMastraClient = (idToken: string) => {
return new MastraClient({
baseUrl: 'https://<mastra-api-url>',
headers: {
Authorization: `Bearer ${idToken}`,
},
})
}
备注

The ID token must be prefixed with Bearer in the Authorization header.

Visit Mastra Client SDK for more configuration options.

Making authenticated requests
Making authenticated requests的直接链接

Once MastraClient is configured with the Firebase ID token, you can send authenticated requests:

src/components/test-agent.tsx
'use client'

import { useAuthState } from 'react-firebase-hooks/auth'
import { MastraClient } from '@mastra/client-js'
import { auth } from '../lib/firebase'
import { getIdToken } from '../lib/auth'

export const TestAgent = () => {
const [user] = useAuthState(auth)

async function handleClick() {
if (!user) return

const token = await getIdToken(user)
const client = createMastraClient(token)

const weatherAgent = client.getAgent('weatherAgent')
const response = await weatherAgent.generate("What's the weather like in New York")

console.log({ response })
}

return (
<button onClick={handleClick} disabled={!user}>
Test Agent
</button>
)
}