Add JWT auth for protected routes, add /reg endpoint, remove /user endpoints

This commit is contained in:
dmit.b
2026-05-08 13:38:52 +03:00
parent 49bb854ca2
commit 3124629e6c
9 changed files with 109 additions and 145 deletions
+31 -53
View File
@@ -12,7 +12,7 @@ import '../models/log.dart';
class DatabaseProvider {
late Connection _dbConnection;
final Map<String, int> _shareLinks = {};
final Map<String, bool> _shareLinks = {};
final _uuid = const Uuid();
Future<void> initialize() async {
@@ -79,11 +79,9 @@ class DatabaseProvider {
Sql.named('''
CREATE TABLE IF NOT EXISTS geopositions (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
x_value DOUBLE PRECISION NOT NULL,
y_value DOUBLE PRECISION NOT NULL,
datetime TIMESTAMP NOT NULL DEFAULT NOW(),
lifetime INTERVAL NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT NOW(),
expires_at TIMESTAMP NOT NULL
)
'''),
@@ -204,24 +202,20 @@ class DatabaseProvider {
// ==================== Geoposition operations ====================
Future<Geoposition> createPosition(
int userId,
double x,
double y,
Duration lifetime,
) async {
final expiresAt = DateTime.now().add(lifetime);
final expiresAt = DateTime.now().add(const Duration(hours: 24));
final results = await _dbConnection.execute(
Sql.named('''
INSERT INTO geopositions (user_id, x_value, y_value, datetime, lifetime, expires_at)
VALUES (@userId, @xValue, @yValue, NOW(), @lifetime, @expiresAt)
RETURNING id, user_id, x_value, y_value, datetime, lifetime, expires_at
INSERT INTO geopositions (x_value, y_value, last_update, expires_at)
VALUES (@xValue, @yValue, NOW(), @expiresAt)
RETURNING id, x_value, y_value, last_update, expires_at
'''),
parameters: {
'userId': userId,
'xValue': x,
'yValue': y,
'lifetime': _toInterval(lifetime),
'expiresAt': expiresAt.toIso8601String(),
},
);
@@ -229,34 +223,28 @@ class DatabaseProvider {
final row = results.first;
return Geoposition(
id: int.parse(row[0].toString()),
userId: int.parse(row[1].toString()),
xValue: double.parse(row[2].toString()),
yValue: double.parse(row[3].toString()),
datetime: DateTime.parse(row[4].toString()),
lifetime: lifetime,
expiresAt: DateTime.parse(row[6].toString()),
xValue: double.parse(row[1].toString()),
yValue: double.parse(row[2].toString()),
lastUpdate: DateTime.parse(row[3].toString()),
expiresAt: DateTime.parse(row[4].toString()),
);
}
Future<Geoposition> updatePosition(
int userId,
double x,
double y,
Duration lifetime,
) async {
final expiresAt = DateTime.now().add(lifetime);
final expiresAt = DateTime.now().add(const Duration(hours: 24));
final results = await _dbConnection.execute(
Sql.named('''
INSERT INTO geopositions (user_id, x_value, y_value, datetime, lifetime, expires_at)
VALUES (@userId, @xValue, @yValue, NOW(), @lifetime, @expiresAt)
RETURNING id, user_id, x_value, y_value, datetime, lifetime, expires_at
INSERT INTO geopositions (x_value, y_value, last_update, expires_at)
VALUES (@xValue, @yValue, NOW(), @expiresAt)
RETURNING id, x_value, y_value, last_update, expires_at
'''),
parameters: {
'userId': userId,
'xValue': x,
'yValue': y,
'lifetime': _toInterval(lifetime),
'expiresAt': expiresAt.toIso8601String(),
},
);
@@ -264,25 +252,22 @@ class DatabaseProvider {
final row = results.first;
return Geoposition(
id: int.parse(row[0].toString()),
userId: int.parse(row[1].toString()),
xValue: double.parse(row[2].toString()),
yValue: double.parse(row[3].toString()),
datetime: DateTime.parse(row[4].toString()),
lifetime: lifetime,
expiresAt: DateTime.parse(row[6].toString()),
xValue: double.parse(row[1].toString()),
yValue: double.parse(row[2].toString()),
lastUpdate: DateTime.parse(row[3].toString()),
expiresAt: DateTime.parse(row[4].toString()),
);
}
Future<Geoposition?> getLatestPosition(int userId) async {
Future<Geoposition?> getLatestPosition() async {
final results = await _dbConnection.execute(
Sql.named('''
SELECT id, user_id, x_value, y_value, datetime, lifetime, expires_at
SELECT id, x_value, y_value, last_update, expires_at
FROM geopositions
WHERE user_id = @userId AND expires_at > NOW()
ORDER BY datetime DESC
WHERE expires_at > NOW()
ORDER BY last_update DESC
LIMIT 1
'''),
parameters: {'userId': userId},
);
if (results.isEmpty) return null;
@@ -290,13 +275,10 @@ class DatabaseProvider {
final row = results.first;
return Geoposition(
id: int.parse(row[0].toString()),
userId: int.parse(row[1].toString()),
xValue: double.parse(row[2].toString()),
yValue: double.parse(row[3].toString()),
datetime: DateTime.parse(row[4].toString()),
lifetime: Duration(
seconds: int.tryParse(row[5].toString()) ?? 0),
expiresAt: DateTime.parse(row[6].toString()),
xValue: double.parse(row[1].toString()),
yValue: double.parse(row[2].toString()),
lastUpdate: DateTime.parse(row[3].toString()),
expiresAt: DateTime.parse(row[4].toString()),
);
}
@@ -308,14 +290,14 @@ class DatabaseProvider {
// ==================== Share operations ====================
String createShareId(int userId) {
String createShareId() {
final uniqueId = _uuid.v4();
_shareLinks[uniqueId] = userId;
_shareLinks[uniqueId] = true;
return uniqueId;
}
int? getUserIdByShareId(String uniqueId) {
return _shareLinks[uniqueId];
bool isValidShareId(String uniqueId) {
return _shareLinks[uniqueId] == true;
}
// ==================== Log operations ====================
@@ -349,8 +331,4 @@ class DatabaseProvider {
.toList();
}
String _toInterval(Duration duration) {
final seconds = duration.inSeconds;
return '$seconds seconds';
}
}
}
+1 -3
View File
@@ -6,11 +6,9 @@ CREATE TABLE IF NOT EXISTS users (
CREATE TABLE IF NOT EXISTS geopositions (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
x_value DOUBLE PRECISION NOT NULL,
y_value DOUBLE PRECISION NOT NULL,
datetime TIMESTAMP NOT NULL DEFAULT NOW(),
lifetime INTERVAL NOT NULL,
last_update TIMESTAMP NOT NULL DEFAULT NOW(),
expires_at TIMESTAMP NOT NULL
);