Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ jobs:
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v7

- name: Configure Pages
uses: actions/configure-pages@v5
uses: actions/configure-pages@v6

- name: Upload docs artifact
uses: actions/upload-pages-artifact@v3
uses: actions/upload-pages-artifact@v5
with:
path: docs

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
uses: actions/deploy-pages@v5
200 changes: 99 additions & 101 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class _DemoStrategy implements AuthStrategy {

@override
Future<AuthSession> refresh(RefreshToken token) async => login(
const Credentials(username: _validUsername, password: _validPassword),
);
const Credentials(username: _validUsername, password: _validPassword),
);
}

/// Real HTTP backend strategy. Talks to the Dart server in `../../server`.
Expand All @@ -64,14 +64,14 @@ class _HttpAuthStrategy implements AuthStrategy {
res.data as Map<String, dynamic>;

AuthSession _toSession(Map<String, dynamic> data) => AuthSession(
accessToken: data['accessToken'] as String,
refreshToken: RefreshToken(data['refreshToken'] as String),
expiresAt: data['expiresIn'] != null
? DateTime.now().add(Duration(seconds: data['expiresIn'] as int))
: null,
userId: data['userId'] as String,
displayName: data['displayName'] as String,
);
accessToken: data['accessToken'] as String,
refreshToken: RefreshToken(data['refreshToken'] as String),
expiresAt: data['expiresIn'] != null
? DateTime.now().add(Duration(seconds: data['expiresIn'] as int))
: null,
userId: data['userId'] as String,
displayName: data['displayName'] as String,
);

@override
Future<AuthSession> login(Credentials credentials) async {
Expand Down Expand Up @@ -194,10 +194,10 @@ class _DemoAppState extends State<DemoApp> {
}

void _toggleBackend(bool value) => setState(() {
unawaited(_auth.dispose());
_useBackend = value;
_init();
});
unawaited(_auth.dispose());
_useBackend = value;
_init();
});

/// Runs an auth action and swallows the rethrown error: [AuthManager] already
/// surfaces it as an [AuthError] state, so there is nothing left to handle.
Expand Down Expand Up @@ -244,10 +244,7 @@ class _DemoAppState extends State<DemoApp> {
/// `Call /me` afterwards shows the transparent renewal.
Future<void> _expireTokenSoon(BuildContext context, int seconds) async {
try {
await _dio.post(
'$_baseUrl/debug/access-ttl',
data: {'seconds': seconds},
);
await _dio.post('$_baseUrl/debug/access-ttl', data: {'seconds': seconds});
await _invoke(() => _auth.refresh());
if (!context.mounted) return;
_snack(
Expand All @@ -274,53 +271,50 @@ class _DemoAppState extends State<DemoApp> {

@override
Widget build(BuildContext context) => MaterialApp(
title: 'zero_auth demo',
theme: _theme(Brightness.light),
darkTheme: _theme(Brightness.dark),
home: Scaffold(
appBar: AppBar(title: const Text('zero_auth demo')),
body: SafeArea(
child: StreamBuilder<AuthState>(
initialData: _auth.current,
stream: _auth.state,
builder: (context, snapshot) => _DemoBody(
state: snapshot.data,
auth: _auth,
useBackend: _useBackend,
baseUrl: _baseUrl,
username: _username,
password: _password,
onToggleBackend: _toggleBackend,
onLogin: () => _invoke(
() => _auth.login(
Credentials(
username: _username.text,
password: _password.text,
),
),
),
onRefresh: () => _invoke(() => _auth.refresh()),
onLogout: () => _invoke(() => _auth.logout()),
onCallMe: () => _callMe(context),
onExpireNow: () => _expireTokenNow(context),
onExpireSoon: () => _expireTokenSoon(context, 10),
onResetDebug: () => _resetDebug(context),
title: 'zero_auth demo',
theme: _theme(Brightness.light),
darkTheme: _theme(Brightness.dark),
home: Scaffold(
appBar: AppBar(title: const Text('zero_auth demo')),
body: SafeArea(
child: StreamBuilder<AuthState>(
initialData: _auth.current,
stream: _auth.state,
builder: (context, snapshot) => _DemoBody(
state: snapshot.data,
auth: _auth,
useBackend: _useBackend,
baseUrl: _baseUrl,
username: _username,
password: _password,
onToggleBackend: _toggleBackend,
onLogin: () => _invoke(
() => _auth.login(
Credentials(username: _username.text, password: _password.text),
),
),
onRefresh: () => _invoke(() => _auth.refresh()),
onLogout: () => _invoke(() => _auth.logout()),
onCallMe: () => _callMe(context),
onExpireNow: () => _expireTokenNow(context),
onExpireSoon: () => _expireTokenSoon(context, 10),
onResetDebug: () => _resetDebug(context),
),
),
);
),
),
);

/// One seed colour drives the whole palette; widgets read shades from the
/// theme instead of hardcoding colors.
static ThemeData _theme(Brightness brightness) => ThemeData(
useMaterial3: true,
brightness: brightness,
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF00695C),
brightness: brightness,
),
);
useMaterial3: true,
brightness: brightness,
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF00695C),
brightness: brightness,
),
);
}

/// The scrollable demo surface. Adapts to the viewport: a full-width column on
Expand Down Expand Up @@ -452,8 +446,9 @@ class _StatusCard extends StatelessWidget {
Expanded(
child: Text(
'state: ${state?.runtimeType ?? 'Unknown'}',
style: theme.textTheme.titleMedium
?.copyWith(fontWeight: FontWeight.w600),
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
overflow: TextOverflow.ellipsis,
),
),
Expand Down Expand Up @@ -583,17 +578,16 @@ class _Badge extends StatelessWidget {

@override
Widget build(BuildContext context) => Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: color,
borderRadius: const BorderRadius.all(Radius.circular(8)),
),
child: Text(
label,
style:
Theme.of(context).textTheme.labelMedium?.copyWith(color: onColor),
),
);
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: color,
borderRadius: const BorderRadius.all(Radius.circular(8)),
),
child: Text(
label,
style: Theme.of(context).textTheme.labelMedium?.copyWith(color: onColor),
),
);
}

/// The complete access token, wrapped over as many lines as it needs.
Expand All @@ -617,8 +611,9 @@ class _TokenBlock extends StatelessWidget {
children: [
Text(
'token',
style: theme.textTheme.bodySmall
?.copyWith(color: theme.colorScheme.onSurfaceVariant),
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 4),
Text(
Expand Down Expand Up @@ -663,8 +658,9 @@ class _LabeledRow extends StatelessWidget {
width: 96,
child: Text(
label,
style: theme.textTheme.bodySmall
?.copyWith(color: theme.colorScheme.onSurfaceVariant),
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
Expanded(child: child),
Expand All @@ -683,13 +679,13 @@ class _InfoRow extends StatelessWidget {

@override
Widget build(BuildContext context) => _LabeledRow(
label: label,
child: Text(
value,
style: Theme.of(context).textTheme.bodyMedium,
overflow: TextOverflow.ellipsis,
),
);
label: label,
child: Text(
value,
style: Theme.of(context).textTheme.bodyMedium,
overflow: TextOverflow.ellipsis,
),
);
}

/// Failure surface. Uses the theme's error container rather than literal red,
Expand Down Expand Up @@ -718,8 +714,9 @@ class _ErrorCard extends StatelessWidget {
children: [
Text(
error.message,
style: theme.textTheme.bodyMedium
?.copyWith(color: colors.onErrorContainer),
style: theme.textTheme.bodyMedium?.copyWith(
color: colors.onErrorContainer,
),
),
if (error.code != null) ...[
const SizedBox(height: 4),
Expand Down Expand Up @@ -754,15 +751,15 @@ class _BackendCard extends StatelessWidget {

@override
Widget build(BuildContext context) => Card(
child: SwitchListTile.adaptive(
value: useBackend,
onChanged: onChanged,
title: const Text('Live backend'),
subtitle: Text(useBackend ? baseUrl : 'offline double, no server'),
secondary: const Icon(Icons.cloud_outlined),
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
),
);
child: SwitchListTile.adaptive(
value: useBackend,
onChanged: onChanged,
title: const Text('Live backend'),
subtitle: Text(useBackend ? baseUrl : 'offline double, no server'),
secondary: const Icon(Icons.cloud_outlined),
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
),
);
}

/// Sign-in form. The password can be revealed, and submitting from the keyboard
Expand Down Expand Up @@ -832,8 +829,9 @@ class _LoginCardState extends State<_LoginCard> {
const SizedBox(height: 8),
Text(
'Demo account: user / user',
style: theme.textTheme.bodySmall
?.copyWith(color: theme.colorScheme.onSurfaceVariant),
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 16),
FilledButton(
Expand Down Expand Up @@ -890,8 +888,9 @@ class _SessionCard extends StatelessWidget {
Text(
'Requests carry the bearer token; an expired one is renewed '
'transparently before it is sent.',
style: theme.textTheme.bodySmall
?.copyWith(color: colors.onSurfaceVariant),
style: theme.textTheme.bodySmall?.copyWith(
color: colors.onSurfaceVariant,
),
),
const SizedBox(height: 16),
FilledButton(
Expand Down Expand Up @@ -967,8 +966,9 @@ class _DebugCard extends StatelessWidget {
const SizedBox(height: 4),
Text(
'Force token expiry without waiting for it to happen.',
style: theme.textTheme.bodySmall
?.copyWith(color: theme.colorScheme.onSurfaceVariant),
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 12),
Wrap(
Expand All @@ -991,9 +991,7 @@ class _DebugCard extends StatelessWidget {
),
TextButton(
onPressed: onReset,
style: TextButton.styleFrom(
minimumSize: const Size(120, 48),
),
style: TextButton.styleFrom(minimumSize: const Size(120, 48)),
child: const Text('Reset'),
),
],
Expand Down
2 changes: 1 addition & 1 deletion example/lib/secure_token_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:zero_auth/zero_auth.dart';
/// is required. Swap for your own codec as needed.
final class SecureTokenStore implements TokenStore {
SecureTokenStore([FlutterSecureStorage? storage])
: _storage = storage ?? const FlutterSecureStorage();
: _storage = storage ?? const FlutterSecureStorage();

final FlutterSecureStorage _storage;

Expand Down
6 changes: 1 addition & 5 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ void main() {
testWidgets('renders without overflow from phone to desktop', (tester) async {
// A phone, a tablet and a desktop window, all at 1x so logical size equals
// the physical size we set.
const sizes = <Size>[
Size(360, 780),
Size(768, 1024),
Size(1440, 900),
];
const sizes = <Size>[Size(360, 780), Size(768, 1024), Size(1440, 900)];

for (final size in sizes) {
tester.view.devicePixelRatio = 1.0;
Expand Down
Loading