feat: added authentification and redirection
This commit is contained in:
parent
1c9c5ce5fe
commit
ef641d4023
24 changed files with 731 additions and 173 deletions
43
lib/data/repositories/auth_repository.dart
Normal file
43
lib/data/repositories/auth_repository.dart
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:seshat/data/services/auth_client.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
class AuthRepository extends ChangeNotifier {
|
||||
AuthRepository({required AuthClient authClient}) : _authClient = authClient;
|
||||
|
||||
final AuthClient _authClient;
|
||||
|
||||
bool? _isAuthenticated;
|
||||
|
||||
Future<bool> get isLoggedIn async {
|
||||
if (_isAuthenticated != null) {
|
||||
return _isAuthenticated!;
|
||||
}
|
||||
final result = await _authClient.hasValidToken();
|
||||
switch (result) {
|
||||
case Ok():
|
||||
if (result.value) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case Error():
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Result<void>> login(String username, String password) async {
|
||||
try {
|
||||
final result = await _authClient.login(username, password);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_isAuthenticated = true;
|
||||
return Result.ok(());
|
||||
case Error():
|
||||
return Result.error(result.error);
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
debugPrintStack(stackTrace: stackTrace);
|
||||
return Result.error(Exception(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -14,32 +14,42 @@ class OwnerRepository {
|
|||
|
||||
final ApiClient _apiClient;
|
||||
final WebsocketClient _wsClient;
|
||||
List<Owner>? _cachedData;
|
||||
List<Owner>? _cachedOwners;
|
||||
|
||||
Future<Result<Owner>> postOwner(
|
||||
String firstName,
|
||||
String lastName,
|
||||
String contact,
|
||||
) async {
|
||||
return Result.ok(
|
||||
Owner(firstName: firstName, lastName: lastName, contact: contact, id: 50),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Result<List<Owner>>> getOwners() async {
|
||||
if (_cachedData == null) {
|
||||
if (_cachedOwners == null) {
|
||||
final result = await _apiClient.getOwners();
|
||||
|
||||
if (result is Ok<List<Owner>>) {
|
||||
_cachedData = result.value;
|
||||
_cachedOwners = result.value;
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return Result.ok(_cachedData!);
|
||||
return Result.ok(_cachedOwners!);
|
||||
}
|
||||
}
|
||||
|
||||
Stream<Owner> liveOwners() async* {
|
||||
await for (String data in _wsClient.connect()) {
|
||||
await for (String data in await _wsClient.connect()) {
|
||||
Map<String, dynamic> decodedData = jsonDecode(
|
||||
data,
|
||||
).cast<Map<String, dynamic>>();
|
||||
Owner owner = Owner.fromJSON(decodedData);
|
||||
if (_cachedData == null) {
|
||||
getOwners();
|
||||
if (_cachedOwners == null) {
|
||||
await getOwners();
|
||||
} else {
|
||||
_cachedData!.add(owner);
|
||||
_cachedOwners!.add(owner);
|
||||
yield* Stream.value(owner);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,61 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:seshat/config/constants.dart';
|
||||
import 'package:seshat/data/services/auth_client.dart';
|
||||
import 'package:seshat/domain/models/owner.dart';
|
||||
import 'package:seshat/utils/command.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
typedef AuthHeaderProvider = String? Function();
|
||||
|
||||
class ApiClient {
|
||||
ApiClient({
|
||||
String? host,
|
||||
int? port,
|
||||
HttpClient Function()? clientFactory,
|
||||
required AuthClient authClient,
|
||||
}) : _authClient = authClient;
|
||||
|
||||
final AuthClient _authClient;
|
||||
late final Command0 load;
|
||||
String? token;
|
||||
bool isReady = false;
|
||||
FlutterSecureStorage? _secureStorage;
|
||||
|
||||
Future<void> _initStore() async {
|
||||
_secureStorage ??= const FlutterSecureStorage(
|
||||
aOptions: AndroidOptions(encryptedSharedPreferences: true),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Result<List<Owner>>> getOwners() async {
|
||||
return Result.ok(<Owner>[]);
|
||||
final client = HttpClient();
|
||||
try {
|
||||
await _initStore();
|
||||
final request = await client.getUrl(
|
||||
Uri.parse("https://$apiBasePath/owners"),
|
||||
);
|
||||
final token = await _secureStorage!.read(key: "token");
|
||||
debugPrint("\n\n\n\nFOUND TOKEN : $token\n\n\n\n");
|
||||
// await _authHeader(request.headers);
|
||||
request.headers.add(HttpHeaders.authorizationHeader, "Bearer $token");
|
||||
final response = await request.close();
|
||||
if (response.statusCode == 200) {
|
||||
final stringData = await response.transform(Utf8Decoder()).join();
|
||||
final json = jsonDecode(stringData) as List<dynamic>;
|
||||
return Result.ok(
|
||||
json.map((element) => Owner.fromJSON(element)).toList(),
|
||||
);
|
||||
} else {
|
||||
return const Result.error(HttpException("Invalid response"));
|
||||
}
|
||||
} on Exception catch (error) {
|
||||
return Result.error(error);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
77
lib/data/services/auth_client.dart
Normal file
77
lib/data/services/auth_client.dart
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:seshat/config/constants.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
import "package:http/http.dart" as http;
|
||||
|
||||
class AuthClient {
|
||||
AuthClient();
|
||||
FlutterSecureStorage? _secureStorage;
|
||||
|
||||
Future<void> _initStore() async {
|
||||
_secureStorage ??= const FlutterSecureStorage(
|
||||
aOptions: AndroidOptions(encryptedSharedPreferences: true),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Result<bool>> hasValidToken() async {
|
||||
try {
|
||||
await _initStore();
|
||||
bool hasToken = await _secureStorage!.containsKey(key: "token");
|
||||
debugPrint("\n\n\n${hasToken == true} => HAS_TOKEN\n\n\n");
|
||||
if (hasToken) {
|
||||
var token = await _secureStorage!.read(key: "token");
|
||||
var url = Uri.parse("https://$apiBasePath/token-check");
|
||||
var response = await http.post(
|
||||
url,
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: jsonEncode({"token": token}),
|
||||
);
|
||||
debugPrint(
|
||||
"\n\n\n${response.body is String} => ${response.body}\n\n\n",
|
||||
);
|
||||
|
||||
if (response.body == "true") {
|
||||
return Result.ok(true);
|
||||
}
|
||||
}
|
||||
return Result.ok(false);
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
return Result.error(Exception(e));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Result<String>> login(String username, String password) async {
|
||||
var client = http.Client();
|
||||
try {
|
||||
await _initStore();
|
||||
var url = Uri.parse("https://$apiBasePath/auth");
|
||||
var response = await client.post(
|
||||
url,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
},
|
||||
body: jsonEncode({"password": password, "username": username}),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
var json = jsonDecode(response.body);
|
||||
await _secureStorage!.write(key: "token", value: json["access_token"]);
|
||||
return Result.ok(json["access_token"]);
|
||||
} else if (response.statusCode == 401) {
|
||||
return Result.error(Exception("Wrong credentials"));
|
||||
} else {
|
||||
return Result.error(Exception("Token creation error"));
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
debugPrint(e.toString());
|
||||
debugPrintStack(stackTrace: stackTrace);
|
||||
return Result.error(Exception(e));
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,16 @@
|
|||
import 'package:seshat/config/constants.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
class WebsocketClient {
|
||||
static const String _host = "ws://bal.ninjdai.fr:3000";
|
||||
Future<Stream<dynamic>> connect() async {
|
||||
final channel = WebSocketChannel.connect(
|
||||
Uri.parse("wss://$apiBasePath/ws"),
|
||||
);
|
||||
|
||||
await channel.ready;
|
||||
|
||||
channel.sink.add("json-token: ");
|
||||
|
||||
Stream<dynamic> connect() {
|
||||
final channel = WebSocketChannel.connect(Uri.parse("$_host/ws"));
|
||||
return channel.stream;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue