Register & Login

Create new user accounts and authenticate existing users with email and password.

Register

Creates a new user account. Returns a SignUpResult carrying the created user and whether the project requires verification before sign-in. The password is hashed server-side before storage — never stored in plaintext.

lib/auth.dart
Dart
final result = await Koolbase.auth.signUp(
  email: '[email protected]',
  password: 'securepassword',
);

if (result.verificationRequired) {
  // Account created, but NOT signed in — this project requires a
  // verified contact channel. Send them to a "check your email" screen.
  showVerifyScreen(result.user.email);
} else {
  // Signed in — the session is stored and the app can proceed.
  goHome(result.user);
}

print(result.user.id);       // UUID
print(result.user.email);    // [email protected]
print(result.user.verified); // false — email not yet verified

Breaking change in 11.0.0

signUp previously returned the user directly. It now returns SignUpResult, because a project can require a verified contact channel before any session is issued — and the old return type could not express "account created, not signed in". If your project does not require verified contact (the default), verificationRequired is always false and result.user behaves exactly as the old return value.

Email 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.dart
Dart
final session = await Koolbase.auth.login(
  email: '[email protected]',
  password: 'securepassword',
);

print(session.token);    // JWT token
print(session.user.id);  // User UUID
print(session.user.email);

ContactNotVerifiedException

If the project requires a verified contact channel, login throws ContactNotVerifiedException when the account has no verified email, no verified phone, and no social sign-in. The credentials were correct — the project's policy refused — so route the user to resend-verification rather than asking them to re-check their password. Catch it separately from InvalidCredentialsException. See Email Verification.

Get current user

Returns the currently authenticated user. Returns null if no session exists.

lib/auth.dart
Dart
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.dart
Dart
await Koolbase.auth.logout();
// Token is now invalid on server and cleared from device

Error handling

lib/auth.dart
Dart
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 'account_disabled':
      showError('Your account has been disabled');
    default:
      showError('Login failed: ${e.message}');
  }
}