150 lines
3.7 KiB
Dart
150 lines
3.7 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',
|
|
queryParameters: {
|
|
'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'];
|
|
}
|
|
}
|
|
}
|
|
|
|
setToken() async {
|
|
String token = '';
|
|
final loginResponse = await auth.login(
|
|
'example123123333333@mail.ru',
|
|
'string213123',
|
|
);
|
|
if (loginResponse != null) {
|
|
token = loginResponse['token'];
|
|
api.options.headers['Authorization'] = "Bearer $token";
|
|
|
|
return loginResponse['record']['id'];
|
|
}
|
|
}
|
|
|
|
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'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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'];
|
|
}
|
|
}
|
|
}
|
|
|
|
userLogout(String tokenId) async {
|
|
try {
|
|
final response = await api.delete(
|
|
'/collections/_authOrigins/records/$tokenId',
|
|
);
|
|
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();
|