Created
August 29, 2022 12:36
-
-
Save felipecastrosales/0548073661dc603a8fda1c5b8625ce91 to your computer and use it in GitHub Desktop.
auth_service.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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