Skip to content

An open-source cross-platform alternative to AirDrop

feat: improve PIN handling

Tien Do Nam6a06d421c8cf
diff --git a/app/assets/web/main.js b/app/assets/web/main.js
index 2fd0d0d8..ef8a105a 100644
--- a/app/assets/web/main.js
+++ b/app/assets/web/main.js
@@ -27,8 +27,9 @@ async function requestFiles() {
return;
}
+ let firstAttempt = true;
while (response.status === 401) {
- const pin = prompt(i18n.enterPin);
+ const pin = prompt(i18n.enterPin + (firstAttempt ? '' : `\n${i18n.invalidPin}`));
if (!pin) {
document.getElementById('status-text').innerText = i18n.invalidPin;
return;
@@ -45,6 +46,8 @@ async function requestFiles() {
document.getElementById('status-text').innerText = i18n.tooManyAttempts;
return;
}
+
+ firstAttempt = false;
}
if (response.status === 403) {
diff --git a/app/lib/provider/network/send_provider.dart b/app/lib/provider/network/send_provider.dart
index 0e35f268..7b932124 100644
--- a/app/lib/provider/network/send_provider.dart
+++ b/app/lib/provider/network/send_provider.dart
@@ -128,6 +128,7 @@ class SendNotifier extends Notifier<Map<String, SendSessionState>> {
Response? response;
bool invalidPin;
+ bool pinFirstAttempt = true;
String? pin;
do {
invalidPin = false;
@@ -150,10 +151,14 @@ class SendNotifier extends Notifier<Map<String, SendSessionState>> {
// ignore: use_build_context_synchronously
pin = await showDialog<String>(
context: Routerino.context,
- builder: (_) => const PinDialog(
+ builder: (_) => PinDialog(
obscureText: true,
+ showInvalidPin: !pinFirstAttempt,
),
);
+
+ pinFirstAttempt = false;
+
if (pin == null) {
state = state.updateSession(
sessionId: sessionId,
diff --git a/app/lib/provider/network/server/controller/common.dart b/app/lib/provider/network/server/controller/common.dart
index 51370491..91adaf1a 100644
--- a/app/lib/provider/network/server/controller/common.dart
+++ b/app/lib/provider/network/server/controller/common.dart
@@ -11,13 +11,17 @@ Response? handlePin({
}) {
if (pin != null) {
final attempts = pinAttempts[request.ip] ?? 0;
- if (attempts >= 2) {
+ if (attempts >= 3) {
return server.responseJson(429, message: 'Too many attempts.');
}
final requestPin = request.url.queryParameters['pin'];
if (requestPin != pin) {
if (requestPin?.isNotEmpty ?? false) {
+ if (attempts == 2) {
+ return server.responseJson(429, message: 'Too many attempts.');
+ }
+
pinAttempts[request.ip] = attempts + 1;
}
return server.responseJson(401, message: 'Invalid pin.');
diff --git a/app/lib/widget/dialogs/pin_dialog.dart b/app/lib/widget/dialogs/pin_dialog.dart
index 93a9ccc5..68e8010d 100644
--- a/app/lib/widget/dialogs/pin_dialog.dart
+++ b/app/lib/widget/dialogs/pin_dialog.dart
@@ -1,16 +1,19 @@
import 'package:flutter/material.dart';
import 'package:localsend_app/gen/strings.g.dart';
+import 'package:localsend_app/theme.dart';
import 'package:nanoid2/nanoid2.dart';
import 'package:routerino/routerino.dart';
class PinDialog extends StatefulWidget {
final String? pin;
+ final bool showInvalidPin;
final bool obscureText;
final bool generateRandom;
const PinDialog({
this.pin,
required this.obscureText,
+ this.showInvalidPin = false,
this.generateRandom = false,
super.key,
});
@@ -32,11 +35,24 @@ class _PinDialogState extends State<PinDialog> {
Widget build(BuildContext context) {
return AlertDialog(
title: Text(t.dialogs.pin.title),
- content: TextFormField(
- controller: _textController,
- autofocus: true,
- obscureText: widget.obscureText,
- onFieldSubmitted: (value) => context.pop(value),
+ content: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ TextFormField(
+ controller: _textController,
+ autofocus: true,
+ obscureText: widget.obscureText,
+ onFieldSubmitted: (value) => context.pop(value),
+ ),
+ if (widget.showInvalidPin)
+ Padding(
+ padding: const EdgeInsets.only(top: 8),
+ child: Text(
+ t.web.invalidPin,
+ style: TextStyle(color: Theme.of(context).colorScheme.warning),
+ ),
+ ),
+ ],
),
actions: [
TextButton(