Register & Login
Create new user accounts and authenticate existing users with email and password.
Register
Creates a new user account. Returns the created user object. The password is hashed server-side before storage — never stored in plaintext.
lib/auth.dartDart
final user = await Koolbase.auth.register(
email: 'user@example.com',
password: 'securepassword',
);
print(user.id); // UUID
print(user.email); // user@example.com
print(user.verified); // false — email not yet verifiedEmail verification
Newly registered users have
verified: false. Send a verification email with Koolbase.auth.sendVerificationEmail() and verify the token when the user clicks the link. See Email Verification.Login
Authenticates a user and returns a JWT session token. The token is automatically stored securely on device and used in all subsequent SDK requests.
lib/auth.dartDart
final session = await Koolbase.auth.login(
email: 'user@example.com',
password: 'securepassword',
);
print(session.token); // JWT token
print(session.user.id); // User UUID
print(session.user.email);Get current user
Returns the currently authenticated user. Returns null if no session exists.
lib/auth.dartDart
final user = await Koolbase.auth.currentUser();
if (user == null) {
// Not logged in — show login screen
Navigator.pushNamed(context, '/login');
} else {
// Logged in — show app
print('Welcome, ${user.email}');
}Logout
Invalidates the current session token on the server and clears it from the device.
lib/auth.dartDart
await Koolbase.auth.logout();
// Token is now invalid on server and cleared from deviceError handling
lib/auth.dartDart
try {
final session = await Koolbase.auth.login(
email: email,
password: password,
);
// success
} on KoolbaseAuthException catch (e) {
switch (e.code) {
case 'invalid_credentials':
showError('Invalid email or password');
case 'email_not_verified':
showError('Please verify your email first');
case 'user_disabled':
showError('Your account has been disabled');
default:
showError('Login failed: ${e.message}');
}
}