224 lines
8.0 KiB
Dart
224 lines
8.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:hub/flutter_flow/nav/nav.dart';
|
|
import 'package:hub/shared/helpers/base_storage.dart';
|
|
import 'package:hub/shared/helpers/storage_helper.dart';
|
|
import 'package:hub/shared/utils/snackbar_util.dart';
|
|
|
|
import '../../../backend/api_requests/api_calls.dart';
|
|
import '../../../flutter_flow/flutter_flow_util.dart';
|
|
import '../../../flutter_flow/random_data_util.dart';
|
|
import '../../utils/device_util.dart';
|
|
import '../../utils/dialog_util.dart';
|
|
import '../../utils/log_util.dart';
|
|
|
|
class AuthenticationService {
|
|
static Future<void> login(BuildContext context) async {
|
|
final GetLocalsCall callback = PhpGroup.getLocalsCall;
|
|
|
|
final response = await callback.call();
|
|
|
|
if (response.jsonBody['error']) {
|
|
DialogUtil.errorDefault(context);
|
|
return;
|
|
}
|
|
List<dynamic> locals = response.jsonBody['locais'] ?? [];
|
|
|
|
if (locals.isEmpty) {
|
|
await StorageHelper().set(SecureStorageKey.haveLocal.value, 'false', Storage.SecureStorage);
|
|
context.go('/receptionPage');
|
|
} else {
|
|
await StorageHelper().set(SecureStorageKey.haveLocal.value, 'true', Storage.SecureStorage);
|
|
context.go('/homePage');
|
|
}
|
|
|
|
await StorageHelper().set(SecureStorageKey.isLogged.value, 'true', Storage.SecureStorage);
|
|
}
|
|
|
|
static Future signIn(
|
|
BuildContext context,
|
|
FlutterFlowModel model, {
|
|
String? emailAdress,
|
|
String? password,
|
|
}) async {
|
|
try {
|
|
final ApiCallResponse? response;
|
|
final LoginCall callback = PhpGroup.loginCall;
|
|
String deviceDescription = randomString(10, 10, true, false, false);
|
|
await StorageHelper().set(SecureStorageKey.deviceDescription.value, deviceDescription, Storage.SecureStorage);
|
|
|
|
final String? devUUID;
|
|
final String userUUID;
|
|
final String status;
|
|
final String userDevUUID;
|
|
final String userName;
|
|
final String email;
|
|
final String passwd;
|
|
|
|
email = emailAdress!;
|
|
passwd = password!;
|
|
devUUID = await DeviceUtil.getDevUUID();
|
|
|
|
if ((email != '') && (passwd != '')) {
|
|
await StorageHelper().set(SecureStorageKey.email.value, email, Storage.SecureStorage);
|
|
await StorageHelper().set(SecureStorageKey.password.value, passwd, Storage.SecureStorage);
|
|
await StorageHelper().set(SQLiteStorageKey.devUUID.value, devUUID!, Storage.SQLiteStorage);
|
|
response = await callback.call();
|
|
|
|
if (response.jsonBody['error'] == false) {
|
|
userUUID = response.jsonBody['uid'];
|
|
status = response.jsonBody['user']['status'];
|
|
userDevUUID = response.jsonBody['user']['dev_id'];
|
|
userName = response.jsonBody['user']['name'];
|
|
|
|
await StorageHelper().set(SQLiteStorageKey.userUUID.value, userUUID, Storage.SQLiteStorage);
|
|
await StorageHelper().set(SQLiteStorageKey.userDevUUID.value, userDevUUID, Storage.SQLiteStorage);
|
|
await StorageHelper().set(SQLiteStorageKey.status.value, status, Storage.SQLiteStorage);
|
|
await StorageHelper().set(SQLiteStorageKey.userName.value, userName, Storage.SQLiteStorage);
|
|
|
|
await login(context);
|
|
} else {
|
|
if (response.jsonBody['error'] == null) {
|
|
DialogUtil.errorDefault(context);
|
|
} else {
|
|
DialogUtil.error(context, response.jsonBody['error_msg'].toString());
|
|
DialogUtil.error(context, response.jsonBody['error_msg'].toString());
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
} catch (e, s) {
|
|
DialogUtil.errorDefault(context);
|
|
LogUtil.requestAPIFailed('login.php', emailAdress.toString(), "Login", e, s);
|
|
}
|
|
}
|
|
|
|
static Future<bool> signUp(
|
|
BuildContext context, {
|
|
required String? name,
|
|
String? passwd,
|
|
required String? email,
|
|
String? device,
|
|
}) async {
|
|
try {
|
|
ApiCallResponse? response;
|
|
if ((email != null && email != '') &&
|
|
(passwd != null && passwd != '' && passwd.length > 7) &&
|
|
(name != null && name != '')) {
|
|
response = await PhpGroup.registerCall.call(
|
|
name: name,
|
|
password: passwd,
|
|
email: email,
|
|
token: randomString(36, 36, false, false, true),
|
|
uuid: randomString(36, 36, false, false, true),
|
|
tipo: device!,
|
|
descricao: randomString(36, 36, true, false, false),
|
|
);
|
|
|
|
if (response.jsonBody['error'] == false) return true;
|
|
DialogUtil.error(context, response.jsonBody['error_msg']);
|
|
return false;
|
|
} else {
|
|
DialogUtil.errorDefault(context);
|
|
return false;
|
|
}
|
|
} catch (e, s) {
|
|
DialogUtil.errorDefault(context);
|
|
LogUtil.requestAPIFailed('registro.php', email.toString(), "Register", e, s);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static Future<void> signOut(BuildContext context) async {
|
|
await PhpGroup.unregisterDevice.call();
|
|
final Map<String, dynamic> extra = <String, dynamic>{
|
|
kTransitionInfoKey: const TransitionInfo(
|
|
hasTransition: true,
|
|
transitionType: PageTransitionType.scale,
|
|
alignment: Alignment.bottomCenter,
|
|
),
|
|
};
|
|
await StorageHelper().clearAll(Storage.SecureStorage);
|
|
await StorageHelper().clearAll(Storage.SQLiteStorage);
|
|
context.go('/welcomePage', extra: extra);
|
|
}
|
|
|
|
static Future<void> forgotPassword(BuildContext context, String email) async {
|
|
try {
|
|
final ApiCallResponse? response;
|
|
final ForgotPasswordCall callback = PhpGroup.forgotPasswordCall;
|
|
final String message = FFLocalizations.of(context)
|
|
.getVariableText(enText: "Send E-mail Successful!", ptText: "E-mail Enviado com Sucesso!");
|
|
|
|
response = await callback.call(email: email);
|
|
|
|
if (response.jsonBody['error'] == false) {
|
|
DialogUtil.success(context, message);
|
|
} else {
|
|
DialogUtil.error(context, response.jsonBody['error_msg']);
|
|
}
|
|
} catch (e, s) {
|
|
DialogUtil.errorDefault(context);
|
|
LogUtil.requestAPIFailed('forgotPassword.php', email, "Forgot Password", e, s);
|
|
}
|
|
}
|
|
|
|
static Future<void> changePassword(BuildContext context, String email, String password, String token) async {
|
|
try {
|
|
final ApiCallResponse response =
|
|
await PhpGroup.changePasswordCall.call(email: email, psswd: password, token: token);
|
|
|
|
if (response.jsonBody['error'] == false) {
|
|
final String message = FFLocalizations.of(context).getVariableText(
|
|
enText: "Password changed successfully!",
|
|
ptText: "Senha alterada com sucesso!",
|
|
);
|
|
DialogUtil.success(context, message).then((_) => context.pop);
|
|
} else {
|
|
final String message = response.jsonBody['error_msg'];
|
|
DialogUtil.error(context, message);
|
|
}
|
|
} catch (e, s) {
|
|
DialogUtil.errorDefault(context);
|
|
LogUtil.requestAPIFailed('changePassword.php', email, "Change Password", e, s);
|
|
}
|
|
}
|
|
|
|
static Future<void> deleteAccount(BuildContext context) async {
|
|
String content;
|
|
try {
|
|
await PhpGroup.deleteAccount.call().then((value) async {
|
|
final Map<String, dynamic> extra = <String, dynamic>{
|
|
kTransitionInfoKey: const TransitionInfo(
|
|
hasTransition: true,
|
|
transitionType: PageTransitionType.scale,
|
|
alignment: Alignment.bottomCenter,
|
|
),
|
|
};
|
|
if (value.jsonBody['error'] == false) {
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Account deleted successfully',
|
|
ptText: 'Conta deletada com sucesso',
|
|
);
|
|
await StorageHelper().clearAll(Storage.SecureStorage);
|
|
context.pop();
|
|
context.go('/welcomePage', extra: extra);
|
|
}
|
|
}).catchError((err) {
|
|
context.pop();
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error deleting account',
|
|
ptText: 'Erro ao deletar conta',
|
|
);
|
|
SnackBarUtil.showSnackBar(context, content);
|
|
});
|
|
} catch (err) {
|
|
context.pop();
|
|
content = FFLocalizations.of(context).getVariableText(
|
|
enText: 'Error deleting account',
|
|
ptText: 'Erro ao deletar conta',
|
|
);
|
|
SnackBarUtil.showSnackBar(context, content, isError: true);
|
|
}
|
|
}
|
|
}
|