kiss_pocketbase_auth 0.1.0
kiss_pocketbase_auth: ^0.1.0 copied to clipboard
PocketBase authentication provider for the kiss_auth package
Kiss PocketBase Auth Example #
A comprehensive Flutter example app demonstrating authentication flows with PocketBase using the kiss_pocketbase_auth
package. This example showcases best practices for implementing secure authentication in Flutter applications.
Features #
-
User Authentication
- Email and password login
- User registration flow
- Persistent authentication state across app restarts
- Automatic token validation on app launch
- Secure logout functionality
-
Security & State Management
- JWT token persistence using
shared_preferences
- Automatic token refresh handling
- Secure credential handling
- Authentication state validation
- JWT token persistence using
Prerequisites #
Before running this example, ensure you have:
-
Flutter SDK (3.8.1 or later)
flutter --version
-
PocketBase server running locally
- Default URL:
http://localhost:8090
- Admin panel:
http://localhost:8090/_/
- Default URL:
-
Development tools
- IDE with Flutter support (VS Code, Android Studio, etc.)
- iOS Simulator/Android Emulator or physical device
Quick Start #
-
Start PocketBase server (from the root directory):
./scripts/docker-up.sh
This will start PocketBase with:
- API endpoint:
http://localhost:8090/api/
- Admin UI:
http://localhost:8090/_/
- API endpoint:
-
Navigate to the example app:
cd example/kiss_pocketbase_example
-
Install dependencies:
flutter pub get
-
Run the app:
# For debug mode with hot reload flutter run # Or specify a device flutter run -d chrome # Web flutter run -d ios # iOS flutter run -d android # Android
Creating Test Users #
You can create test users in several ways:
Method 1: Using the App's Signup Feature #
The example app now supports full user registration:
- Launch the app
- Tap "Don't have an account? Sign up" from the login screen
- Fill in the signup form:
- Email: Any valid email address
- Password: At least 8 characters
- Confirm password
- Tap "Sign Up" to create and login automatically
Method 2: PocketBase Admin UI #
- Open the PocketBase Admin panel: http://localhost:8090/_/
- Create an admin account if prompted
- Navigate to Collections → Users
- Click "New record" to create test users manually
Method 3: Using Test Helpers #
For automated testing, you can create users programmatically:
// In your test setup
await PocketBaseTestHelper.createTestUser(
email: 'test@example.com',
password: 'testpassword123',
);
Suggested Test Credentials #
For manual testing, you can use:
- Email:
test@example.com
- Password:
testpassword123
Project Structure #
kiss_pocketbase_example/
├── lib/
│ ├── main.dart # App entry point with splash screen
│ ├── screens/
│ │ ├── login_screen.dart # Login UI with form validation
│ │ ├── signup_screen.dart # Registration UI
│ │ └── home_screen.dart # Authenticated user dashboard
│ └── services/
│ └── auth_service.dart # Authentication logic & token management
├── pubspec.yaml # Dependencies configuration
└── test/
└── widget_test.dart # Widget tests
Screen Details #
🔐 Login Screen (login_screen.dart
) #
- Features:
- Email/password form with validation
- Real-time error feedback
- Loading states during authentication
- Navigation to signup screen
- Error Handling:
- Invalid credentials
- Network connectivity issues
- Server errors
📝 Signup Screen (signup_screen.dart
) #
- Features:
- Email/password registration form
- Password confirmation field
- Input validation (email format, password strength)
- Success feedback and auto-navigation to login
- Note: Creates users via PocketBase API (requires proper setup)
🏠 Home Screen (home_screen.dart
) #
- Displays:
- User profile information (ID, email, verification status)
- JWT token details and expiration
- Authentication claims in formatted JSON
- Raw user record data
- Actions:
- Logout button with confirmation
- Token refresh status
Authentication Flow #
graph TD
A[App Launch] --> B{Saved Token?}
B -->|Yes| C[Validate Token]
B -->|No| D[Show Login Screen]
C -->|Valid| E[Show Home Screen]
C -->|Invalid| D
D --> F[User Login/Signup]
F -->|Success| G[Save Token]
G --> E
E --> H[User Logout]
H --> I[Clear Token]
I --> D
Flow Details #
- Initial Check: Splash screen checks
SharedPreferences
for saved JWT token - Token Validation: Validates token with PocketBase server
- Smart Navigation:
- ✅ Valid token → Home screen (authenticated state)
- ❌ Invalid/expired token → Login screen
- 🆕 No token → Login screen
- Authentication: Login/signup creates new session and persists token
- Session Management: Logout clears token and navigates to login
Implementation Details #
Token Persistence #
// Token is automatically saved on successful login
await prefs.setString('auth_token', token);
// Token is validated on app startup
final token = prefs.getString('auth_token');
if (token != null) {
await _validator.validateToken(token);
}
Error Handling #
The example demonstrates proper error handling for:
-
Authentication Errors
- Invalid credentials (401)
- Account not verified
- Account disabled
-
Network Errors
- Connection timeout
- No internet connection
- Server unreachable
-
Data Errors
- Malformed responses
- Invalid JWT tokens
- Expired tokens
Configuration #
Using a Custom PocketBase Instance #
-
Update the base URL in
auth_service.dart
:static const String _baseUrl = 'https://your-pocketbase.com';
-
Configure CORS (if needed for web):
# In your PocketBase settings Origins: http://localhost:3000
-
Custom User Collection:
// If using a custom collection instead of 'users' await _validator.authenticateWithPassword( identity: email, password: password, collection: 'custom_users', // Specify your collection );
Environment-Specific Configuration #
// Consider using environment variables
class Config {
static const String baseUrl = String.fromEnvironment(
'POCKETBASE_URL',
defaultValue: 'http://localhost:8090',
);
}
Testing #
Run the included widget tests:
flutter test
For integration testing with a real PocketBase instance:
# Start PocketBase
./scripts/docker-up.sh
# Run integration tests
flutter test integration_test/
Troubleshooting #
Common Issues #
-
"Connection refused" error
- Ensure PocketBase is running:
docker ps
- Check the URL in
auth_service.dart
- For Android emulator, use
10.0.2.2
instead oflocalhost
- Ensure PocketBase is running:
-
"Invalid credentials" on valid login
- Verify user exists in PocketBase admin
- Check email is verified if required
- Ensure password meets requirements
-
Token expires immediately
- Check PocketBase token duration settings
- Verify system time is synchronized
-
App doesn't remember login
- Check SharedPreferences permissions
- Verify token is being saved correctly
- Clear app data and try again
Next Steps #
- Implement biometric authentication
- Add social login providers
- Implement password reset flow
- Add user profile management
- Implement refresh token rotation
- Add offline support with token caching
Resources #
License #
This example is provided under the same license as the kiss_pocketbase_auth package.