Sign the terminal in against the back office instead of against two constants
Sign-in compared `admin@nearle.in` / `nearle123` — a compile-time const — after a 600ms delay standing in for a network call that was never made. Two things followed, and the second was the serious one. Every install of a build shared one password, and changing it meant a rebuild. Worse: because nothing was checked with the back office, the *outlet* could not come from the sign-in. It came from a store id typed into Settings, so the till asserted which shop it belonged to and the server took its word. One field on one screen moved a terminal into another tenant's books. Now a person signs in with their own back-office account and the outlet arrives as a consequence — sealed in a signed token, checked server-side on every request, and not editable from this device. `DemoCredentials` is gone, along with the prefilled fields and the "Demo account" hint that printed the password on the login screen. The pieces: - `PosSession` — what the back office answers with. The token is opaque on purpose: the till must not parse it or reason about what it appears to say. - `SessionStore` — the whole session to the platform keystore, not SQLite. The token is a bearer credential and SQLite here is a file behind a shop counter. An expired session reads back as absent, so no caller has to remember to check. - `SyncConfig.bearerToken` — one accessor rather than the same `??` at each call site, because the request that forgot it would be the one silently sending no credentials. The session beats a static API key: the key says the request came from our fleet, the session says which outlet it came from, and only the second can stop a till reaching another tenant's books. - Restore runs in `syncBootstrapProvider` *before* the engine starts. A drain that began first would upload the day's bills unauthenticated. A till trades all day; a reboot mid-shift must not put a login screen in front of a queue. - An outlet picker, shown only when the account genuinely reaches several. Not dismissable — defaulting silently to the first outlet is how a day's takings end up filed against the wrong shop. Store name, address, GSTIN and phone now come down with the session and are written on sign-in. They were compile-time constants, and on a GST invoice those fields are a legal requirement rather than decoration. The smoke test signs in through a fake client and inside `runAsync`: sign-in reaches SQLite now, and real disk I/O cannot complete on a widget test's fake clock — pumping alone leaves it suspended for ever. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import '../../../core/theme/app_colors.dart';
|
||||
import '../../../core/theme/app_dimens.dart';
|
||||
import '../../../core/utils/validators.dart';
|
||||
import '../../../core/widgets/primary_button.dart';
|
||||
import '../../../domain/entities/pos_session.dart';
|
||||
import '../providers/auth_controller.dart';
|
||||
|
||||
/// Store sign-in. The terminal shows this until a valid account is entered.
|
||||
@@ -21,8 +22,8 @@ class LoginScreen extends ConsumerStatefulWidget {
|
||||
|
||||
class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _email = TextEditingController(text: DemoCredentials.email);
|
||||
final _password = TextEditingController(text: DemoCredentials.password);
|
||||
final _email = TextEditingController();
|
||||
final _password = TextEditingController();
|
||||
|
||||
bool _obscure = true;
|
||||
bool _rememberTerminal = true;
|
||||
@@ -38,12 +39,42 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
FocusScope.of(context).unfocus();
|
||||
if (!(_formKey.currentState?.validate() ?? false)) return;
|
||||
|
||||
final ok = await ref.read(authControllerProvider.notifier).signIn(
|
||||
final auth = ref.read(authControllerProvider.notifier);
|
||||
|
||||
var ok = await auth.signIn(
|
||||
email: _email.text,
|
||||
password: _password.text,
|
||||
);
|
||||
if (!ok || !mounted) return;
|
||||
|
||||
// A proprietor with several shops signs in once and then says which counter
|
||||
// this terminal is standing at. The back office has already decided which
|
||||
// outlets they may reach, so this is a choice among those — never a free
|
||||
// text field, which is what the old Settings store id amounted to.
|
||||
final session = auth.session;
|
||||
if (session != null && session.hasChoiceOfOutlet) {
|
||||
final chosen = await showDialog<PosOutlet>(
|
||||
context: context,
|
||||
// Not dismissable: a terminal has to be standing somewhere, and
|
||||
// defaulting silently to the first outlet is how a day's takings end up
|
||||
// filed against the wrong shop.
|
||||
barrierDismissible: false,
|
||||
builder: (_) => _OutletPicker(session: session),
|
||||
);
|
||||
|
||||
if (chosen == null || !mounted) return;
|
||||
|
||||
if (chosen.locationId != session.locationId) {
|
||||
ok = await auth.signIn(
|
||||
email: _email.text,
|
||||
password: _password.text,
|
||||
locationId: chosen.locationId,
|
||||
);
|
||||
if (!ok || !mounted) return;
|
||||
}
|
||||
}
|
||||
|
||||
if (ok && mounted) context.go(AppRoutes.pos);
|
||||
if (mounted) context.go(AppRoutes.pos);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -432,15 +463,6 @@ class _FormPanel extends ConsumerWidget {
|
||||
onPressed: onSubmit,
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.xl),
|
||||
_DemoHint(
|
||||
onFill: busy
|
||||
? null
|
||||
: () {
|
||||
email.text = DemoCredentials.email;
|
||||
password.text = DemoCredentials.password;
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: AppSpacing.xxl),
|
||||
Center(
|
||||
@@ -483,58 +505,99 @@ class _Label extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class _DemoHint extends StatelessWidget {
|
||||
const _DemoHint({this.onFill});
|
||||
/// Asks which of the signed-in account's outlets this terminal is standing in.
|
||||
///
|
||||
/// Only shown when there is genuinely a choice. A shop manager pinned to one
|
||||
/// location never sees it, which is the common case — this is for a proprietor
|
||||
/// whose account reaches several shops.
|
||||
///
|
||||
/// The list comes from the back office and cannot be typed into. That is the
|
||||
/// whole difference from what this replaced: the outlet used to be a store id
|
||||
/// entered in Settings, so a till asserted which shop it belonged to. Here it
|
||||
/// picks from what the account is already entitled to, and the choice is
|
||||
/// re-checked server-side when the new session is issued.
|
||||
class _OutletPicker extends StatelessWidget {
|
||||
const _OutletPicker({required this.session});
|
||||
|
||||
final VoidCallback? onFill;
|
||||
final PosSession session;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(AppSpacing.md),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.primarySurface,
|
||||
borderRadius: AppRadius.brSm,
|
||||
border: Border.all(color: AppColors.primaryBorder),
|
||||
return AlertDialog(
|
||||
backgroundColor: AppColors.surface,
|
||||
shape: RoundedRectangleBorder(borderRadius: AppRadius.brMd),
|
||||
title: const Text(
|
||||
'Which outlet is this terminal at?',
|
||||
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w700),
|
||||
),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Icon(Icons.info_outline_rounded,
|
||||
size: 17, color: AppColors.primary,),
|
||||
const SizedBox(width: AppSpacing.sm),
|
||||
const Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Demo account',
|
||||
style: TextStyle(
|
||||
fontSize: 12.5,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.primary,
|
||||
),
|
||||
content: SizedBox(
|
||||
width: 380,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: AppSpacing.md),
|
||||
child: Text(
|
||||
'Signed in as ${session.fullName.isNotEmpty ? session.fullName : session.email}'
|
||||
' · ${session.tenantName}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12.5,
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
SizedBox(height: 2),
|
||||
SelectableText(
|
||||
'${DemoCredentials.email} · ${DemoCredentials.password}',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: onFill,
|
||||
style: TextButton.styleFrom(
|
||||
minimumSize: const Size(0, 32),
|
||||
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.sm),
|
||||
Flexible(
|
||||
child: ListView.separated(
|
||||
shrinkWrap: true,
|
||||
itemCount: session.outlets.length,
|
||||
separatorBuilder: (_, __) =>
|
||||
const SizedBox(height: AppSpacing.xs),
|
||||
itemBuilder: (context, i) {
|
||||
final outlet = session.outlets[i];
|
||||
final isCurrent = outlet.locationId == session.locationId;
|
||||
|
||||
return ListTile(
|
||||
dense: true,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: AppRadius.brSm,
|
||||
side: BorderSide(
|
||||
color: isCurrent
|
||||
? AppColors.primaryBorder
|
||||
: AppColors.border,
|
||||
),
|
||||
),
|
||||
tileColor:
|
||||
isCurrent ? AppColors.primarySurface : AppColors.surface,
|
||||
title: Text(
|
||||
outlet.locationName.isNotEmpty
|
||||
? outlet.locationName
|
||||
: 'Outlet ${outlet.locationId}',
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
// The numeric id is shown deliberately. It is what appears
|
||||
// in the back office, on a support call and in every log
|
||||
// line, so a person can match what they are looking at.
|
||||
subtitle: Text(
|
||||
[
|
||||
'ID ${outlet.locationId}',
|
||||
if (outlet.city.isNotEmpty) outlet.city,
|
||||
].join(' · '),
|
||||
style: const TextStyle(
|
||||
fontSize: 11.5,
|
||||
color: AppColors.textTertiary,
|
||||
),
|
||||
),
|
||||
onTap: () => Navigator.of(context).pop(outlet),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
child: const Text('Fill', style: TextStyle(fontSize: 12.5)),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user