Skip to content

Instantly share code, notes, and snippets.

@felipecastrosales
Created August 29, 2022 12:36
Show Gist options
  • Save felipecastrosales/0548073661dc603a8fda1c5b8625ce91 to your computer and use it in GitHub Desktop.
Save felipecastrosales/0548073661dc603a8fda1c5b8625ce91 to your computer and use it in GitHub Desktop.
auth_service.dart
class AuthService extends GetxService {
final _isLogged = RxnBool();
final _getStorage = GetStorage();
void logout() async {
_getStorage.write(ConstantsAPI.auth, null);
snackBar(
MessageModel.success(
title: 'Oba! Logout com sucesso.',
message: 'O logout foi realizado com sucesso.',
),
);
await HiveInit.clearBoxes();
Get.offAllNamed('/onboarding');
}
String? get getToken => _getStorage.read(ConstantsAPI.auth);
Future<AuthService> init() async {
_getStorage.listenKey(ConstantsAPI.auth, (value) {
_isLogged(value != null);
});
ever<bool?>(_isLogged, (isLogged) async {
if (isLogged == null || !isLogged) {
Get.offAllNamed('/onboarding');
} else {
await HiveInit.initCache();
Get.offAllNamed('/base');
}
});
_isLogged(getToken != null);
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment