MastraAuthFirebase 类
🌐 MastraAuthFirebase Class
MastraAuthFirebase 类使用 Firebase 认证为 Mastra 提供身份验证。它通过 Firebase ID 令牌验证传入请求,并使用 auth 选项与 Mastra 服务器集成。
🌐 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.
用法示例Direct link to 用法示例
🌐 Usage examples
使用环境变量的基本用法Direct link to 使用环境变量的基本用法
🌐 Basic usage with environment variables
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(),
},
});
自定义配置Direct link to 自定义配置
🌐 Custom configuration
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",
}),
},
});
构造函数参数Direct link to 构造函数参数
🌐 Constructor parameters
serviceAccount?:
databaseId?:
name?:
环境变量Direct link to 环境变量
🌐 Environment Variables
当没有提供构造函数选项时,会自动使用以下环境变量:
🌐 The following environment variables are automatically used when constructor options are not provided:
FIREBASE_SERVICE_ACCOUNT?:
FIRESTORE_DATABASE_ID?:
FIREBASE_DATABASE_ID?:
默认授权行为Direct link to 默认授权行为
🌐 Default Authorization Behavior
默认情况下,MastraAuthFirebase 使用 Firestore 来管理用户访问:
🌐 By default, MastraAuthFirebase uses Firestore to manage user access:
- 在成功验证 Firebase ID 令牌后,会调用
authorizeUser方法 - 它会检查
user_access集合中是否存在以用户 UID 作为文档 ID 的文档 - 如果文档存在,用户被授权;否则,拒绝访问
- 使用的 Firestore 数据库由
databaseId参数或环境变量决定
Firebase 用户类型Direct link to Firebase 用户类型
🌐 Firebase User Type
authorizeUser 函数中使用的 FirebaseUser 类型对应于 Firebase 的 DecodedIdToken 接口,其中包括:
🌐 The FirebaseUser type used in the authorizeUser function corresponds to Firebase's DecodedIdToken interface, which includes:
uid:用户的唯一标识email:用户的电子邮件地址(如果有的话)email_verified:电子邮件是否已验证name:用户的显示名称(如果有的话)picture:用户头像的 URL(如果可用)auth_time:当用户通过身份验证时- 以及其他标准 JWT 声明
相关Direct link to 相关
🌐 Related