This commit is contained in:
User3 2025-05-27 11:35:34 +03:00
parent 8f3009b98c
commit 37f74d5f92
4 changed files with 43 additions and 4 deletions

View File

@ -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();

View File

@ -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);
}

View File

@ -4,3 +4,4 @@ const emailKey = 'email';
const identityKey = 'identity';
const passwordKey = 'password';
const confirmPasswordKey = 'passwordConfirm';
const id = 'user_id';

View File

@ -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();
}
});
}