mcpField
S
serverpod-auth

serverpod-auth

github.com/serverpodSecurity3.3k43 installsUpdated 1d ago
Repository

About serverpod-auth

Serverpod Authentication provides authentication features for Serverpod backends and Flutter apps. It includes sign-in UI, user profile access, session state, and endpoint access controls.

Key features

  1. 01Require login or specific scopes
  2. 02Read authenticated user IDs and profiles
  3. 03Sign in with SignInWidget
  4. 04Listen for authentication state changes
  5. 05Sign out one or all devices

Use cases

  1. 01Restrict endpoints to signed-in users
  2. 02Protect methods with scope checks
  3. 03Add a sign-in screen to a Flutter app
  4. 04Read profiles or end sessions

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/serverpod/serverpod --skill serverpod-auth

After installing, type/serverpod-authin Claude Code to confirm it loaded.

Bundled filesreferences/setup.mdreferences/user-management.md

Details

From the project README

Serverpod Authentication

Serverpod has authentication built in. Projects created with serverpod create have it enabled by default (unless --no-auth, or the project has no database), pre-configured with email and already wired up in lib/server.dart and in the Flutter app.

In server application code, import package:serverpod_auth_idp_server/core.dart and package:serverpod_auth_idp_server/providers/<provider>.dart. Do NOT import package:serverpod_auth_idp_server/serverpod_auth_idp_server.dart — that library exists for the code generator.

Server-side

Require the user to be signed in or have a specific scope
class MyEndpoint extends Endpoint {
  // Require the user to be signed in to access methods in this endpoint.
  @override
  bool get requireLogin => true;

  // Require the user to have the admin scope.
  @override
  Set<Scope> get requiredScopes => {Scope.admin};

  // This method can only be accessed if the user is admin.
  Future<void> myMethod(Session session) async {
    ...
  }
  ...
}
User id and info
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,
);

Flutter app

Use SignInWidget to sign the user in. It provides its own Material surface, so it also renders correctly when mixed with non-Material design systems:

SignInWidget(
  client: client,
  onAuthenticated: () => _showSnackBar(message: 'User authenticated.'),
  onError: (error) => _showSnackBar(message: 'Authentication failed: $error'),
)
  • Signed-in state: client.auth.isAuthenticated. Rebuild on changes by listening to client.auth.authInfoListenable (a ValueListenable<AuthSuccess?>, so ValueListenableBuilder works too), and remove the listener in dispose.
  • Sign out: client.auth.signOutAllDevices() or client.auth.signOutDevice().
  • User profile (email, full name, etc): await client.modules.serverpod_auth_core.userProfileInfo.get().

More

  • references/setup.md — adding the auth packages to a project created without auth, initializing the services in server.dart, wiring the Flutter client, configuring social sign-ins, migrating off the legacy serverpod_auth module.
  • references/user-management.md — attaching your own data to a user, editing scopes, letting the client edit its profile.

Comments

Markdown supported