A React Native library for handling user session timeouts with app lifecycle support, warning dialogs, and Android 10+ compatibility
npm install react-native-session-timeoutA comprehensive React Native library for inactivity detection and idle app monitoring with automatic session timeout management, customizable warning UI support, and full Android 10+ compatibility.
This library detects user inactivity and manages session expiration for security and compliance needs.
Perfect for:
- š¦ Banking/Finance Apps - Auto-logout after inactivity (PCI-DSS, SOC2)
- š¢ Enterprise Apps - Security policies requiring session timeouts
- š E-commerce - Expire shopping carts/sessions
- š„ Healthcare Apps - HIPAA compliance with automatic session termination
- š Any app requiring security-based session management
ā
Inactivity & Idle Detection - Automatically tracks user activity (taps, scrolls, swipes, gestures)
ā
Android 10+ Compatible - No background timer issues
ā
Automatic Lifecycle Handling - Properly handles app background/foreground transitions
ā
Built-in Warning Dialogs - Countdown timer with customizable warning UI
ā
TypeScript Support - Full type definitions included
ā
Zero Native Linking - Works with autolinking
ā
Customizable - Configure timeout duration, warning time, and callbacks
ā
Performant - Uses native modules for accurate timekeeping
``bash`
npm install react-native-session-timeoutor
yarn add react-native-session-timeout
`bash`
cd ios && pod install
`tsx
import React from 'react';
import { View, Text } from 'react-native';
import {
SessionTimeoutProvider,
useSessionTimeout,
} from 'react-native-session-timeout';
function App() {
return (
warningDuration={60000} // Show warning 1 minute before timeout
onTimeout={() => {
console.log('Session timed out!');
// Handle logout or session expiry
}}
onWarning={(remainingTime) => {
console.log(Warning: ${remainingTime}ms remaining);`
}}
>
);
}
`tsx
import { useSessionTimeout } from 'react-native-session-timeout';
function YourComponent() {
const { isWarning, remainingTime, resetTimer, pauseTimer, resumeTimer } =
useSessionTimeout();
return (
{isWarning && (
Session expiring in {Math.floor(remainingTime / 1000)} seconds
)}
);
}
`
`tsx
import React from 'react';
import { Modal, View, Text, Button } from 'react-native';
import {
SessionTimeoutProvider,
useSessionTimeout,
} from 'react-native-session-timeout';
function CustomWarningDialog() {
const { isWarning, remainingTime, resetTimer } = useSessionTimeout();
return (
Your session will expire in {Math.floor(remainingTime / 1000)}{' '}
seconds
);
}
function App() {
return (
warningDuration={60000}
onTimeout={handleTimeout}
>
);
}
`
The library automatically detects user activity and resets the timer on any interaction:
- ā
Taps - Any touch on the screen
- ā
Scrolls - ScrollView, FlatList, etc.
- ā
Swipes - Gesture-based navigation
- ā
Gestures - Any PanResponder-based interaction
How it works: The library wraps your app with a PanResponder that detects all touch events. When the user interacts with the app, the inactivity timer automatically resets, keeping the session alive. If the user is idle (no interaction) for the configured timeout duration, the onTimeout callback is triggered.
Example Use Case:
`tsx`
// Logout user after 5 minutes of inactivity
onTimeout={() => {
console.log('User has been idle for 5 minutes');
// Perform logout
}}
>
| Prop | Type | Required | Description |
| ------------------- | --------------------------------- | -------- | ------------------------------------------------------------------- |
| timeout | number | Yes | Total timeout duration in milliseconds |warningDuration
| | number | No | Show warning this many milliseconds before timeout (default: 60000) |onTimeout
| | () => void | Yes | Callback when session times out |onWarning
| | (remainingTime: number) => void | No | Callback when warning period starts |enabled
| | boolean | No | Enable/disable the timeout (default: true) |pauseOnBackground
| | boolean | No | Pause timer when app goes to background (default: false) |
Returns an object with:
| Property | Type | Description |
| --------------- | ------------ | ------------------------------------ |
| isWarning | boolean | Whether in warning period |remainingTime
| | number | Milliseconds remaining until timeout |resetTimer
| | () => void | Reset the session timer |pauseTimer
| | () => void | Pause the timer |resumeTimer
| | () => void | Resume the timer |isActive
| | boolean | Whether timer is currently active |
The library uses native timers (Android/iOS) for accurate timekeeping and automatically calls your onTimeout callback when the session expires.
`tsx
import AsyncStorage from '@react-native-async-storage/async-storage';
import { SessionTimeoutProvider } from 'react-native-session-timeout';
function App() {
const handleTimeout = async () => {
try {
// 1. Call logout endpoint to invalidate server session
const token = await AsyncStorage.getItem('authToken');
await fetch('https://api.example.com/logout', {
method: 'POST',
headers: { Authorization: Bearer ${token} },
});
} catch (error) {
console.error('Logout API error:', error);
// Continue with local cleanup even if API fails
}
// 2. Clear local storage
await AsyncStorage.removeItem('authToken');
// 3. Navigate to login
navigation.reset({ index: 0, routes: [{ name: 'Login' }] });
// 4. Notify user
Alert.alert('Session Expired', 'Please log in again');
};
return (
warningDuration={60000} // 1 minute warning
onTimeout={handleTimeout}
>
);
}
`
- Native Timers: Accurate timing even when JS thread is busy
- Automatic Lifecycle Handling: Manages app background/foreground transitions
- Warning System: Configurable warning period before timeout
- Activity Tracking: Resets timer on user interaction
This library is essential for apps that require automatic logout for security:
- š¦ Banking & Finance - PCI-DSS compliance requirement
- š„ Healthcare - HIPAA compliance for patient data protection
- š¼ Enterprise - Corporate security policies (ISO 27001)
- šļø Government - Classified/sensitive information handling
- š Security-sensitive apps - Auto-logout after inactivity
-
!Simulator Screen Recording - iPhone 15 Pro - 2025-12-13 at 23 45 30
By default, any touch or gesture anywhere in your app will reset the inactivity timer, thanks to the global PanResponder. If you have UI elements (like control buttons, modals, or overlays) that should NOT reset the timer when interacted with, wrap them in a View with onStartShouldSetResponder={() => true}. This prevents touch events from bubbling up to the SessionTimeoutProvider's PanResponder.
Example:
`tsx``
This ensures that only intended interactions reset the timer, while controls and overlays behave as expected.
MIT