MastraAuthFirebase class
The MastraAuthFirebase class 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.
Usage examplesUsage examples的直接链接
Basic usage with environment variablesBasic usage with environment variables的直接链接
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 configurationCustom 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-key.json',
databaseId: 'your-database-id',
}),
},
})
Constructor parametersConstructor parameters的直接链接
serviceAccount?:
string
= process.env.FIREBASE_SERVICE_ACCOUNT
Path to the Firebase service account JSON file. This file contains the credentials needed to verify Firebase ID tokens on the server side.
databaseId?:
string
= process.env.FIRESTORE_DATABASE_ID || process.env.FIREBASE_DATABASE_ID
The Firestore database ID to use. Typically '(default)' for the default database.
name?:
string
= "firebase"
Custom name for the auth provider instance.
Environment variablesEnvironment variables的直接链接
The following environment variables are automatically used when constructor options aren't provided:
FIREBASE_SERVICE_ACCOUNT?:
string
Path to Firebase service account JSON file. Used if serviceAccount option is not provided.
FIRESTORE_DATABASE_ID?:
string
Firestore database ID. Primary environment variable for database configuration.
FIREBASE_DATABASE_ID?:
string
Alternative environment variable for Firestore database ID. Used if FIRESTORE_DATABASE_ID is not set.
Default authorization behaviorDefault authorization behavior的直接链接
By default, MastraAuthFirebase uses Firestore to manage user access:
- After successfully verifying a Firebase ID token, the
authorizeUsermethod is called - It checks for the existence of a document in the
user_accesscollection with the user's UID as the document ID - If the document exists, the user is authorized; otherwise, access is denied
- The Firestore database used is determined by the
databaseIdparameter or environment variables
Firebase user typeFirebase user type的直接链接
The FirebaseUser type used in the authorizeUser function corresponds to Firebase's DecodedIdToken interface, which includes:
uid: The user's unique identifieremail: The user's email address (if available)email_verified: Whether the email is verifiedname: The user's display name (if available)picture: URL to the user's profile picture (if available)auth_time: When the user authenticated- And other standard JWT claims