Para provides a comprehensive set of methods for managing authentication sessions in Flutter applications. These sessions are crucial for secure transaction signing and other authenticated operations.

Session Duration

The Para session length is 2 hours by default, but can be configured to up to 30 days. To configure this parameter, please visit the Configuration section of the Developer Portal. A user signing a message or transaction extends the session by the duration of the session length.

Managing Sessions

Checking Session Status

Use isSessionActive() to verify whether a user’s session is currently valid before performing authenticated operations.

Future<bool> isSessionActive()

In Flutter applications, it’s especially important to check the session status before allowing users to access authenticated areas of your app due to the persistence of local storage between app launches.

Example usage:

import 'package:para_flutter/client/para.dart';

Future<void> checkSession() async {
  try {
    final isActive = await para.isSessionActive();
    if (!isActive) {
      // First clear any existing data
      await para.logout();
      
      // Navigate to login screen
      // Handle navigation according to your app's navigation strategy
    } else {
      // Session is valid, proceed with app flow
      // Navigate to authenticated part of your app
    }
  } catch (e) {
    // Handle error
  }
}

Refreshing Expired Sessions

When a session has expired, Para recommends initiating a full authentication flow rather than trying to refresh the session.

For Flutter applications, always call logout() before reinitiating authentication when a session has expired to ensure all stored data is properly cleared.

import 'package:para_flutter/client/para.dart';

Future<void> handleSessionExpiration() async {
  // When session expires, first clear storage
  await para.logout();
  
  // Then redirect to authentication screen
  // Handle navigation according to your app's navigation strategy
}

Exporting Sessions to Your Server

Use exportSession() when you need to transfer session state to your server for performing operations on behalf of the user.

String exportSession()

Example implementation:

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:para_flutter/client/para.dart';

Future<Map<String, dynamic>> sendSessionToServer() async {
  // Export session without signing capabilities
  final sessionData = para.exportSession();
  
  // Send to your server
  try {
    final response = await http.post(
      Uri.parse('https://your-api.com/sessions'),
      headers: {
        'Content-Type': 'application/json',
      },
      body: jsonEncode({'session': sessionData}),
    );
    
    if (response.statusCode != 200) {
      throw Exception('Failed to send session to server');
    }
    
    return jsonDecode(response.body);
  } catch (e) {
    // Handle error
    throw e;
  }
}

Best Practices for Flutter

  1. Check Sessions on App Launch: Verify session status when your app starts to determine if users need to reauthenticate.
import 'package:flutter/material.dart';
import 'package:para_flutter/client/para.dart';

// In your app's entry point or state initialization

void initState() {
  super.initState();
  checkSessionOnLaunch();
}

Future<void> checkSessionOnLaunch() async {
  final isActive = await para.isSessionActive();
  if (isActive) {
    // Navigate to authenticated part of your app
  } else {
    await para.logout(); // Clear any lingering data
    // Navigate to login screen
  }
}
  1. Handle App Lifecycle Changes: Flutter apps can be backgrounded and foregrounded, which may affect session status.
import 'package:flutter/material.dart';
import 'package:para_flutter/client/para.dart';

class YourWidget extends StatefulWidget {
  
  _YourWidgetState createState() => _YourWidgetState();
}

class _YourWidgetState extends State<YourWidget> with WidgetsBindingObserver {
  
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      // App came to foreground, check session
      checkSession();
    }
  }

  Future<void> checkSession() async {
    final isActive = await para.isSessionActive();
    if (!isActive) {
      await para.logout();
      // Navigate to login screen
    }
  }

  
  Widget build(BuildContext context) {
    // Your widget implementation
    return Container();
  }
}

Next Steps

Explore more advanced features and integrations with Para in Flutter: