From 37f74d5f9237e0a6e15bf8529e8695826bf12777 Mon Sep 17 00:00:00 2001 From: User3 Date: Tue, 27 May 2025 11:35:34 +0300 Subject: [PATCH] #user1 --- lib/apis/auth.dart | 19 ++++++++++++++++++- lib/client.dart | 10 ++++++++++ lib/constants.dart | 1 + test/server_test.dart | 17 ++++++++++++++--- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/apis/auth.dart b/lib/apis/auth.dart index 350126b..1eb97c6 100644 --- a/lib/apis/auth.dart +++ b/lib/apis/auth.dart @@ -31,7 +31,7 @@ class Auth { try { final response = await api.patch( '/collections/users/record/$userId', - data: { + queryParameters: { 'firstname': firstName, 'lastname': lastname, 'secondname': secondName, @@ -60,6 +60,7 @@ class Auth { if (loginResponse != null) { token = loginResponse['token']; api.options.headers['Authorization'] = "Bearer $token"; + return loginResponse['record']['id']; } } @@ -109,6 +110,22 @@ class Auth { } } } + + getCurrentUser() async { + try { + final id = await setToken(); + final response = await api.get('/collections/users/records/$id'); + if (response.data != null) { + return response.data; + } + } on DioException catch (e) { + if (e.type == DioExceptionType.connectionError) { + lastError = 'Internet Connection Error'; + } else { + lastError = e.response!.data['message']; + } + } + } } final auth = Auth(); diff --git a/lib/client.dart b/lib/client.dart index d505360..f472eeb 100644 --- a/lib/client.dart +++ b/lib/client.dart @@ -45,3 +45,13 @@ setToken(value) async { final prefs = await getLocalStorageInstance(); prefs.setString(token, value); } + +setKey(key, value) async { + final prefs = await getLocalStorageInstance(); + prefs.setString(key, value); +} + +getKey(key) async { + final prefs = await getLocalStorageInstance(); + prefs.get(key); +} diff --git a/lib/constants.dart b/lib/constants.dart index 754e33d..ed779e3 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -4,3 +4,4 @@ const emailKey = 'email'; const identityKey = 'identity'; const passwordKey = 'password'; const confirmPasswordKey = 'passwordConfirm'; +const id = 'user_id'; diff --git a/test/server_test.dart b/test/server_test.dart index b353e08..2b1746b 100644 --- a/test/server_test.dart +++ b/test/server_test.dart @@ -7,11 +7,11 @@ void main() { test('Спринт 2. Авторизация', () async { try { final response = await auth.login( - 'example123123@test.ru', + 'example123123333333@mail.ru', 'string213123', ); if (response != null) { - expect(response['record']['email'], 'example123123@test.ru'); + expect(response['record']['email'], 'example123123333333@mail.ru'); } } catch (e) { // ignore: avoid_print @@ -22,7 +22,7 @@ void main() { test('Спринт 2. Создание пользователя', () async { try { final response = await auth.register( - 'example123123333333@mail.ru', + 'example123123333333@mail2.ru', 'string213123', 'string213123', 'testtest', @@ -61,4 +61,15 @@ void main() { return Error(); } }); + test('Спринт 2.Получение информации о профиле', () async { + try { + final response = await auth.getCurrentUser(); + if (response != null) { + expect(response['firstname'], 'testtest'); + } + } catch (e) { + print(e); + return Error(); + } + }); }