AuthHandler class

Authentication handler for Cocobase client.

Provides methods for user authentication, registration, and user management. This class handles all authentication-related operations and maintains user session state.

Example:

final db = Cocobase(CocobaseConfig(apiKey: 'your-key'));
await db.auth.login('user@example.com', 'password');

Constructors

AuthHandler({required String baseURL, required String? apiKey, required AuthStore? authStore, required Dio dio})

Properties

apiKey String?
final
authStore AuthStore?
final
baseURL String
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

clearAuthCallbacks() → void
Remove all registered callbacks
getCurrentUser() Future<AppUser>
Fetch the current authenticated user's data from the server
getToken() String?
Get the current authentication token
getUser() AppUser?
Get the current user object
getUserById(String userId) Future<AppUser>
Get a user by their ID
hasRole(String role) bool
Check if the current user has a specific role
initAuth() Future<void>
Initialize authentication by restoring the session from storage. Call this method when your application loads to restore user sessions.
isAuthenticated() bool
Check if a user is currently authenticated
listUsers({QueryBuilder? queryBuilder}) Future<AppUserList>
List users from the auth collection with optional filtering and pagination
login(String email, String password) Future<void>
Authenticate a user with email and password
loginWithGithub(String code, String redirectUri, {AuthPlatform platform = AuthPlatform.web}) Future<AppUser>
Authenticate a user using GitHub OAuth with authorization code
loginWithGoogle(String idToken, {AuthPlatform platform = AuthPlatform.web}) Future<AppUser>
Authenticate a user using Google Sign-In with ID token
logout() Future<void>
Logout the current user by clearing the token and user data
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onAuthEvent(AuthCallbacks callbacks) → void
Register callbacks for authentication events. This allows your application to respond to auth state changes in a framework-agnostic way.
register(String email, String password, {Map<String, dynamic>? data}) Future<void>
Register a new user with email, password, and optional additional data
registerWithFiles(String email, String password, {Map<String, dynamic>? data, Map<String, dynamic>? files}) Future<AppUser>
Register with file uploads (avatar, cover photo, etc.)
setToken(String token) Future<void>
Set the authentication token and store it
setUser(AppUser user) Future<void>
Update the current user object
toString() String
A string representation of this object.
inherited
updateUser({Map<String, dynamic>? data, String? email, String? password}) Future<AppUser>
Update the current user's profile data
updateUserWithFiles({Map<String, dynamic>? data, String? email, String? password, Map<String, dynamic>? files}) Future<AppUser>
Update current user with file uploads

Operators

operator ==(Object other) bool
The equality operator.
inherited