serverpod-auth
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseServerpod Authentication
Serverpod 认证
Serverpod has authentication built in. Projects created with have it enabled by default (unless , or the project has no database), pre-configured with email and already wired up in and in the Flutter app.
serverpod create--no-authlib/server.dartIn server application code, import and . Do NOT import — that library exists for the code generator.
package:serverpod_auth_idp_server/core.dartpackage:serverpod_auth_idp_server/providers/<provider>.dartpackage:serverpod_auth_idp_server/serverpod_auth_idp_server.dartServerpod 内置了认证功能。使用 创建的项目默认启用该功能(除非使用 参数,或项目未配置数据库),已预先配置邮箱认证,并在 和 Flutter 应用中完成了相关对接。
serverpod create--no-authlib/server.dart在服务端应用代码中,导入 和 。请勿导入 —— 该库仅用于代码生成。
package:serverpod_auth_idp_server/core.dartpackage:serverpod_auth_idp_server/providers/<provider>.dartpackage:serverpod_auth_idp_server/serverpod_auth_idp_server.dartServer-side
服务端
Require the user to be signed in or have a specific scope
要求用户登录或拥有特定权限范围
dart
class MyEndpoint extends Endpoint {
// Require the user to be signed in to access methods in this endpoint.
bool get requireLogin => true;
// Require the user to have the admin scope.
Set<Scope> get requiredScopes => {Scope.admin};
// This method can only be accessed if the user is admin.
Future<void> myMethod(Session session) async {
...
}
...
}dart
class MyEndpoint extends Endpoint {
// 要求用户登录才能访问此端点中的方法。
bool get requireLogin => true;
// 要求用户拥有管理员权限范围。
Set<Scope> get requiredScopes => {Scope.admin};
// 此方法仅允许管理员用户访问。
Future<void> myMethod(Session session) async {
...
}
...
}User id and info
用户 ID 与信息
dart
import 'package:serverpod_auth_idp_server/core.dart';
// Get authenticated user's ID.
final userIdUuidValue = session.authenticated?.authUserId;
// Get the user profile (full name, email, etc)
var userProfile = await session.authenticated?.userProfile(session);
// Find a user profile by email.
final profiles = await AuthServices.instance.userProfiles.admin
.listUserProfiles(
session,
email: email.toLowerCase(),
limit: 1,
);
final userProfile = profiles.firstOrNull;
// Get authentication info for user id (for editing scopes, etc).
final authUsers = AuthServices.instance.authUsers;
final authUser = await authUsers.get(
session,
authUserId: userProfile.authUserId,
);dart
import 'package:serverpod_auth_idp_server/core.dart';
// 获取已认证用户的 ID。
final userIdUuidValue = session.authenticated?.authUserId;
// 获取用户资料(全名、邮箱等)
var userProfile = await session.authenticated?.userProfile(session);
// 通过邮箱查找用户资料。
final profiles = await AuthServices.instance.userProfiles.admin
.listUserProfiles(
session,
email: email.toLowerCase(),
limit: 1,
);
final userProfile = profiles.firstOrNull;
// 根据用户 ID 获取认证信息(用于编辑权限范围等操作)。
final authUsers = AuthServices.instance.authUsers;
final authUser = await authUsers.get(
session,
authUserId: userProfile.authUserId,
);Flutter app
Flutter 应用
Use to sign the user in. It provides its own Material surface, so it also renders correctly when mixed with non-Material design systems:
SignInWidgetdart
SignInWidget(
client: client,
onAuthenticated: () => _showSnackBar(message: 'User authenticated.'),
onError: (error) => _showSnackBar(message: 'Authentication failed: $error'),
)- Signed-in state: . Rebuild on changes by listening to
client.auth.isAuthenticated(aclient.auth.authInfoListenable, soValueListenable<AuthSuccess?>works too), and remove the listener inValueListenableBuilder.dispose - Sign out: or
client.auth.signOutAllDevices().client.auth.signOutDevice() - User profile (email, full name, etc): .
await client.modules.serverpod_auth_core.userProfileInfo.get()
使用 实现用户登录。它自带 Material 界面,因此与非 Material 设计系统混合使用时也能正常渲染:
SignInWidgetdart
SignInWidget(
client: client,
onAuthenticated: () => _showSnackBar(message: 'User authenticated.'),
onError: (error) => _showSnackBar(message: 'Authentication failed: $error'),
)- 登录状态:。可通过监听
client.auth.isAuthenticated(一个client.auth.authInfoListenable,也可使用ValueListenable<AuthSuccess?>)在状态变化时重建界面,并在ValueListenableBuilder中移除监听器。dispose - 登出:或
client.auth.signOutAllDevices()。client.auth.signOutDevice() - 用户资料(邮箱、全名等):。
await client.modules.serverpod_auth_core.userProfileInfo.get()
More
更多内容
- — adding the auth packages to a project created without auth, initializing the services in
references/setup.md, wiring the Flutter client, configuring social sign-ins, migrating off the legacyserver.dartmodule.serverpod_auth - — attaching your own data to a user, editing scopes, letting the client edit its profile.
references/user-management.md
- —— 为未启用认证的项目添加认证包、在
references/setup.md中初始化服务、对接 Flutter 客户端、配置社交登录、从旧版server.dart模块迁移。serverpod_auth - —— 将自定义数据关联到用户、编辑权限范围、允许客户端编辑用户资料。
references/user-management.md