Added jwt authenticaation
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:frontend_splatournament_manager/services/auth_service.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||
|
||||
class AuthProvider extends ChangeNotifier {
|
||||
final AuthService _authService = AuthService();
|
||||
final FlutterSecureStorage _storage = const FlutterSecureStorage();
|
||||
|
||||
bool _isLoggedIn = false;
|
||||
String? _username;
|
||||
@@ -23,6 +26,12 @@ class AuthProvider extends ChangeNotifier {
|
||||
|
||||
try {
|
||||
final user = await _authService.login(username, password);
|
||||
|
||||
final token = user['token'];
|
||||
if (token != null) {
|
||||
await _storage.write(key: 'jwt_token', value: token);
|
||||
}
|
||||
|
||||
_isLoggedIn = true;
|
||||
_username = user['username'];
|
||||
_userId = user['id'];
|
||||
@@ -44,6 +53,12 @@ class AuthProvider extends ChangeNotifier {
|
||||
|
||||
try {
|
||||
final user = await _authService.register(username, password);
|
||||
|
||||
final token = user['token'];
|
||||
if (token != null) {
|
||||
await _storage.write(key: 'jwt_token', value: token);
|
||||
}
|
||||
|
||||
_isLoggedIn = true;
|
||||
_username = user['username'];
|
||||
_userId = user['id'];
|
||||
@@ -58,17 +73,32 @@ class AuthProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
void logout() {
|
||||
Future<void> logout() async {
|
||||
_isLoggedIn = false;
|
||||
_username = null;
|
||||
_userId = null;
|
||||
_error = null;
|
||||
await _storage.delete(key: 'jwt_token');
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> checkAuthStatus() async {
|
||||
final token = await _storage.read(key: 'jwt_token');
|
||||
if (token != null) {
|
||||
if (JwtDecoder.isExpired(token)) {
|
||||
await logout();
|
||||
} else {
|
||||
Map<String, dynamic> decodedToken = JwtDecoder.decode(token);
|
||||
_isLoggedIn = true;
|
||||
_userId = decodedToken['id'];
|
||||
_username = decodedToken['username'];
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void clearError() {
|
||||
_error = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,9 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:frontend_splatournament_manager/models/tournament.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../main.dart';
|
||||
import 'package:frontend_splatournament_manager/services/api_client.dart';
|
||||
|
||||
class TournamentProvider extends ChangeNotifier {
|
||||
final String baseUrl = SplatournamentApp.baseUrl;
|
||||
|
||||
List<Tournament> _availableTournaments = [];
|
||||
Future<List<Tournament>>? _initialLoadFuture;
|
||||
@@ -16,7 +13,7 @@ class TournamentProvider extends ChangeNotifier {
|
||||
List<Tournament> get availableTournaments => _availableTournaments;
|
||||
|
||||
Future<List<Tournament>> _fetchTournaments() async {
|
||||
final response = await http.get(Uri.parse('$baseUrl/tournaments'));
|
||||
final response = await ApiClient.get('/tournaments');
|
||||
if (response.statusCode != HttpStatus.ok) {
|
||||
throw Exception('Failed to load tournaments (${response.statusCode})');
|
||||
}
|
||||
@@ -48,21 +45,15 @@ class TournamentProvider extends ChangeNotifier {
|
||||
DateTime registrationStartDate,
|
||||
DateTime registrationEndDate,
|
||||
) async {
|
||||
final response = await http.post(
|
||||
Uri.parse('$baseUrl/tournaments'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
final response = await ApiClient.post(
|
||||
'/tournaments',
|
||||
{
|
||||
'name': name,
|
||||
'description': description,
|
||||
'maxTeamAmount': maxTeamAmount,
|
||||
//weird date formatting
|
||||
'registrationStartDate': registrationStartDate.toIso8601String().split(
|
||||
'T',
|
||||
)[0],
|
||||
'registrationEndDate': registrationEndDate.toIso8601String().split(
|
||||
'T',
|
||||
)[0],
|
||||
}),
|
||||
'registrationStartDate': registrationStartDate.toIso8601String().split('T')[0],
|
||||
'registrationEndDate': registrationEndDate.toIso8601String().split('T')[0],
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode != HttpStatus.created) {
|
||||
|
||||
Reference in New Issue
Block a user