102 lines
2.5 KiB
Dart
102 lines
2.5 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:server/constants.dart';
|
|
import 'package:server/server.dart';
|
|
|
|
class Auth {
|
|
String lastError = '';
|
|
login(String email, String password) async {
|
|
try {
|
|
final response = await api.post(
|
|
'/collections/users/auth-with-password',
|
|
data: {identityKey: email, passwordKey: password},
|
|
);
|
|
return response.data;
|
|
} on DioException catch (e) {
|
|
if (e.type == DioExceptionType.connectionError) {
|
|
lastError = 'Internet Connection Error';
|
|
} else {
|
|
lastError = e.response!.data['message'];
|
|
}
|
|
}
|
|
}
|
|
|
|
updateUser(
|
|
String userId,
|
|
String firstName,
|
|
String lastname,
|
|
String secondName,
|
|
String datebirthday,
|
|
String gender,
|
|
) async {
|
|
try {
|
|
final response = await api.patch(
|
|
'/collections/users/record/$userId',
|
|
data: {
|
|
'firstname': firstName,
|
|
'lastname': lastname,
|
|
'secondname': secondName,
|
|
'datebirthday': datebirthday,
|
|
'gender': gender,
|
|
},
|
|
);
|
|
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'];
|
|
}
|
|
}
|
|
}
|
|
|
|
register(
|
|
String email,
|
|
String password,
|
|
String confirmPassword,
|
|
String firstName,
|
|
String lastname,
|
|
String secondName,
|
|
String datebirthday,
|
|
String gender,
|
|
) async {
|
|
if (password != confirmPassword) {
|
|
lastError = 'Confirm password != password';
|
|
} else {
|
|
try {
|
|
final response = await api.post(
|
|
'/collections/users/records',
|
|
data: {
|
|
emailKey: email,
|
|
passwordKey: password,
|
|
confirmPasswordKey: password,
|
|
},
|
|
);
|
|
if (response.data != null) {
|
|
final responseData = response.data['id'];
|
|
final result = await updateUser(
|
|
responseData,
|
|
firstName,
|
|
lastname,
|
|
secondName,
|
|
datebirthday,
|
|
gender,
|
|
);
|
|
if (response.data) {
|
|
return result;
|
|
}
|
|
}
|
|
} on DioException catch (e) {
|
|
if (e.type == DioExceptionType.connectionError) {
|
|
lastError = 'Internet Connection Error';
|
|
} else {
|
|
lastError = e.response!.data['message'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
final auth = Auth();
|