Loading...
Loading...
Serverpod authentication — signing in users, checking whether they are authenticated, assigning scopes (e.g. admin). Use when adding features that require the user to be signed in.
npx skill4agent add serverpod/serverpod serverpod-authserverpod create--no-authlib/server.dartpackage:serverpod_auth_idp_server/core.dartpackage:serverpod_auth_idp_server/providers/<provider>.dartpackage:serverpod_auth_idp_server/serverpod_auth_idp_server.dartclass 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 {
...
}
...
}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,
);SignInWidgetSignInWidget(
client: client,
onAuthenticated: () => _showSnackBar(message: 'User authenticated.'),
onError: (error) => _showSnackBar(message: 'Authentication failed: $error'),
)client.auth.isAuthenticatedclient.auth.authInfoListenableValueListenable<AuthSuccess?>ValueListenableBuilderdisposeclient.auth.signOutAllDevices()client.auth.signOutDevice()await client.modules.serverpod_auth_core.userProfileInfo.get()references/setup.mdserver.dartserverpod_authreferences/user-management.md