Sessions

Koolbase uses JWT-based sessions. Tokens are stored securely on device and automatically included in all SDK requests.

How sessions work

When someone signs in, the server creates a session and returns two tokens. The SDK keeps them in the device's secure storage — the iOS Keychain or Android Keystore on a phone (in React Native, through react-native-keychain when it is installed), and the browser's storage on the web — and sends the access token with every request.

At the next launch the stored session is restored, so a signed-in person goes straight to the app.

Restore the session at launch

In Flutter, Koolbase.initialize restores the stored session itself. In React Native and on the web, call restoreSession after initializing, before you decide which screen to show:

lib/main.dart
Dart
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Koolbase.initialize(const KoolbaseConfig(
    publicKey: 'pk_live_...',
    baseUrl: 'https://api.koolbase.com',
  )); // restores the stored session

  final user = Koolbase.auth.currentUser;
  runApp(MyApp(initialRoute: user != null ? '/home' : '/login'));
}

Follow sign-in and sign-out

Listen for changes instead of checking repeatedly — a session that ends on the server signs the person out here too:

lib/auth.dart
Dart
Koolbase.auth.authStateChanges.listen((user) {
  if (user == null) {
    // signed out, or the session ended
  }
});

Session scoping

Sessions are scoped per project environment. A session from your production environment cannot be used in staging or development.

Environment isolation

If you switch environments during development, a stored session from the previous environment is refused. Call Koolbase.auth.logout() to clear it before switching.

Get the current token

To call your own backend as the signed-in person, ask for a valid access token — renewed first if it is about to expire, or null when no one is signed in:

lib/api.dart
Dart
final token = await Koolbase.auth.validAccessToken();
final response = await http.get(
  Uri.parse('https://your-api.com/profile'),
  headers: {'Authorization': 'Bearer $token'},
);

Session expiry

A session has two tokens with different lifetimes. The access token lives 15 minutes and authenticates every request. The refresh token lives 30 days and rotates on each use — so a session stays alive indefinitely with regular activity, and ends after 30 days of inactivity.

You never handle this yourself: the SDK renews the access token before it expires. Your code only sees the end of a session — when the refresh token has expired or been revoked, currentUser becomes null, the stored tokens are cleared, and the listener above is told.