52 lines
1.3 KiB
Dart
52 lines
1.3 KiB
Dart
import 'package:shelf/shelf.dart';
|
|
import 'package:shelf_router/shelf_router.dart';
|
|
import 'package:bcrypt/bcrypt.dart';
|
|
import '../database/database_provider.dart';
|
|
import 'dart:convert';
|
|
|
|
class AuthRoutes {
|
|
final DatabaseProvider database;
|
|
|
|
AuthRoutes(this.database);
|
|
|
|
Router get routes {
|
|
final router = Router();
|
|
router.post('/login', _login);
|
|
router.get('/watch', _watch);
|
|
return router;
|
|
}
|
|
|
|
Future<Response> _login(Request request) async {
|
|
final body = await request.readAsString();
|
|
final data = jsonDecode(body);
|
|
|
|
final login = data['login'];
|
|
final password = data['password'];
|
|
|
|
final user = await database.findUserByLogin(login);
|
|
|
|
if (user == null || !BCrypt.checkpw(password, user.pwdHash)) {
|
|
return Response(401, body: 'Invalid credentials');
|
|
}
|
|
|
|
return Response(200, body: jsonEncode({'user': user.toMap()}));
|
|
}
|
|
|
|
Future<Response> _watch(Request request) async {
|
|
final uniqueId = request.url.queryParameters['unique_id'];
|
|
|
|
final userId = database.getUserIdByShareId(uniqueId!);
|
|
|
|
if (userId == null) {
|
|
return Response(404, body: 'Share link not found');
|
|
}
|
|
|
|
final position = await database.getLatestPosition(userId);
|
|
|
|
if (position == null) {
|
|
return Response(404, body: 'No position available');
|
|
}
|
|
|
|
return Response(200, body: position.toJson());
|
|
}
|
|
} |