flutter_secure_auth 0.1.5
flutter_secure_auth: ^0.1.5 copied to clipboard
A secure, lightweight authentication package for Flutter (REST + OAuth2 PKCE + token refresh) with secure local storage.
import 'package:flutter/material.dart';
import 'package:flutter_secure_auth/flutter_secure_auth.dart';
import 'package:http/http.dart' as http;
void main() => runApp(const ExampleApp());
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Secure Auth Example',
theme: ThemeData(primarySwatch: Colors.indigo),
home: const AuthDemoScreen(),
);
}
}
class AuthDemoScreen extends StatefulWidget {
const AuthDemoScreen({super.key});
@override
State<AuthDemoScreen> createState() => _AuthDemoScreenState();
}
class _AuthDemoScreenState extends State<AuthDemoScreen> {
final AuthService authService = AuthService(
tokenEndpoint: Uri.parse('https://api.example.com/oauth/token'),
);
String _output = '';
Future<void> _loginWithPassword() async {
setState(() => _output = 'Signing in...');
try {
final tokens = await authService.signInWithPassword(
endpoint: Uri.parse('https://api.example.com/auth/login'),
username: 'test@example.com',
password: 'password123',
);
setState(() => _output = 'Access token: ${tokens.accessToken}');
} catch (e) {
setState(() => _output = 'Error: $e');
}
}
Future<void> _fetchProtected() async {
final req = http.Request(
'GET',
Uri.parse('https://api.example.com/protected'),
);
final authed = await authService.authorizedRequest(req);
final response = await authed.send();
final body = await response.stream.bytesToString();
setState(() => _output = 'Response: $body');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('flutter_secure_auth Demo')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
ElevatedButton(
onPressed: _loginWithPassword,
child: const Text('Login with Password'),
),
ElevatedButton(
onPressed: _fetchProtected,
child: const Text('Fetch Protected Resource'),
),
const SizedBox(height: 16),
Text(_output, style: const TextStyle(fontSize: 14)),
],
),
),
);
}
}