step 14 : fix bug loginscreen current user from package google sign in

This commit is contained in:
sindhu
2024-01-26 23:04:24 +07:00
parent 82d403b210
commit 0596fd4a75
2 changed files with 87 additions and 79 deletions

View File

@@ -32,8 +32,10 @@ class HomeScreen extends HookConsumerWidget {
ref.watch(currentCheckJamPresensiProvider); ref.watch(currentCheckJamPresensiProvider);
final positionLatitude = useState<String>(""); final positionLatitude = useState<String>("");
final positionLongitude = useState<String>(""); final positionLongitude = useState<String>("");
GoogleSignInAccount? currentUserGoogle = // GoogleSignInAccount? currentUserGoogle =
ref.watch(currentUserGoogleProvider); // ref.watch(currentUserGoogleProvider);
final googleSignIn = ref.watch(googleSignInProvider);
useEffect(() { useEffect(() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async { WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
@@ -281,7 +283,7 @@ class HomeScreen extends HookConsumerWidget {
right: Constant.getActualXPhone(context: context, x: 27), right: Constant.getActualXPhone(context: context, x: 27),
), ),
child: Container( child: Container(
child: (currentUserGoogle == null) child: (googleSignIn.currentUser == null)
? Center( ? Center(
child: CircularProgressIndicator(), child: CircularProgressIndicator(),
) )
@@ -296,12 +298,13 @@ class HomeScreen extends HookConsumerWidget {
// ), // ),
// ), // ),
leading: GoogleUserCircleAvatar( leading: GoogleUserCircleAvatar(
identity: currentUserGoogle, identity: googleSignIn.currentUser!,
), ),
title: Text( title: Text(
// "Stephen Kusumo", // "Stephen Kusumo",
// selectedUser?.model.name ?? "", // selectedUser?.model.name ?? "",
currentUserGoogle.displayName ?? "", // currentUserGoogle.displayName ?? "",
googleSignIn.currentUser?.displayName ?? "",
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
style: Constant.titleH1_700(context: context) style: Constant.titleH1_700(context: context)
..copyWith( ..copyWith(
@@ -311,7 +314,8 @@ class HomeScreen extends HookConsumerWidget {
subtitle: Text( subtitle: Text(
// "Step@example.com", // "Step@example.com",
// currentUserGoogle?.email ?? "", // currentUserGoogle?.email ?? "",
selectedUser?.model.email ?? "", // selectedUser?.model.email ?? "",
googleSignIn.currentUser?.email ?? "",
style: style:
Constant.subtitle_500_12(context: context) Constant.subtitle_500_12(context: context)
.copyWith( .copyWith(

View File

@@ -25,89 +25,77 @@ class LoginScreen extends HookConsumerWidget {
// inisialisasi // inisialisasi
// ref.watch itu sama spt memantau state terus menerus // ref.watch itu sama spt memantau state terus menerus
// ref.read itu memantau namun hny 1x saja // ref.read itu memantau namun hny 1x saja
GoogleSignInAccount? currentUserGoogle = // GoogleSignInAccount? currentUserGoogle =
ref.watch(currentUserGoogleProvider); // ref.watch(currentUserGoogleProvider);
final googleSignIn = ref.watch(googleSignInProvider); final googleSignIn = ref.watch(googleSignInProvider);
final isLoading = useState(false); final isLoading = useState(false);
final errorMessage = useState(""); // final errorMessage = useState("");
final isSuccess = useState(false); final isSuccess = useState(false);
useEffect(() { useEffect(() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async { WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
final staffID = ref.read(currentUserProvider)?.model.staffId ?? "0"; // final staffID = ref.read(currentUserProvider)?.model.staffId ?? "0";
final shared = await SharedPreferences.getInstance();
final bearerString = shared.get(Constant.bearerName).toString();
final xmodel = jsonDecode(bearerString);
if (xmodel == null) return;
final authModel = AuthModel(
token: xmodel["token"],
model: StaffModel(
companyId: xmodel["model"]["companyId"],
companyName: xmodel["model"]["companyName"],
email: xmodel["model"]["email"],
staffId: xmodel["model"]["staffId"],
idGoogleSignIn: xmodel['model']['idGoogleSignIn'],
name: xmodel['model']['name'],
nip: xmodel['model']['nip'],
phoneNumber: xmodel["model"]["phoneNumber"],
token: xmodel["model"]["token"],
),
);
// ref.read(currentUserProvider.notifier).state = authModel;
await googleSignIn.signInSilently();
googleSignIn.onCurrentUserChanged.listen((account) { googleSignIn.onCurrentUserChanged.listen((account) {
ref // ref
.read(currentUserGoogleProvider.notifier) // .read(currentUserGoogleProvider.notifier)
.update((state) => account); // .update((state) => account);
});
googleSignIn.signInSilently();
// if (currentUserGoogle != null && staffID == "0") {
// ref.read(loginProvider.notifier).login(
// currentUserGoogle.email,
// currentUserGoogle.id,
// );
// }
if (currentUserGoogle != null) {
if (staffID == "0") {
ref.read(loginProvider.notifier).login(
currentUserGoogle.email,
currentUserGoogle.id,
);
}
}
});
return () {};
}, [currentUserGoogle]);
useEffect(() {
return () {};
}, [currentUserGoogle]);
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
final staffID = ref.read(currentUserProvider)?.model.staffId ?? "0";
final shared = await SharedPreferences.getInstance();
final bearerString = shared.get(Constant.bearerName).toString();
final xmodel = jsonDecode(bearerString);
if (xmodel == null) return;
final authModel = AuthModel(
token: xmodel["token"],
model: StaffModel(
companyId: xmodel["model"]["companyId"],
companyName: xmodel["model"]["companyName"],
email: xmodel["model"]["email"],
staffId: xmodel["model"]["staffId"],
idGoogleSignIn: xmodel['model']['idGoogleSignIn'],
name: xmodel['model']['name'],
nip: xmodel['model']['nip'],
phoneNumber: xmodel["model"]["phoneNumber"],
token: xmodel["model"]["token"],
),
);
googleSignIn.onCurrentUserChanged.listen((account) {
if (account != null && staffID == "0") {
ref
.read(currentUserGoogleProvider.notifier)
.update((state) => account);
ref.read(currentUserProvider.notifier).state = authModel; ref.read(currentUserProvider.notifier).state = authModel;
ref.read(currentPageProvider.state).update((state) => 0); ref.read(currentPageProvider.state).update((state) => 0);
Navigator.of(context).pushNamedAndRemoveUntil(
homeRoute, if (account != null &&
(route) => false, ref.read(currentUserProvider)?.model.staffId == "0") {
); // Lakukan login
ref.read(loginProvider.notifier).login(
account.email,
account.id,
);
}
});
// kalau sudah pernah login
if (googleSignIn.currentUser?.email != null) {
googleSignIn.signInSilently();
// ref.read(currentUserGoogleProvider.notifier).update(
// (state) => googleSignIn.currentUser,
// );
ref.read(loginProvider.notifier).login(
googleSignIn.currentUser?.email ?? "",
googleSignIn.currentUser?.id ?? "",
);
} }
// ref.read(currentUserProvider.notifier).state = authModel;
// Navigator.of(context).pushNamedAndRemoveUntil(
// homeRoute,
// (route) => false,
// );
}); });
return () {};
// ref.read(currentUserProvider.notifier).state = authModel; }, []);
// Navigator.of(context).pushNamedAndRemoveUntil(
// homeRoute,
// (route) => false,
// );
});
// LISTEN PROVIDER // LISTEN PROVIDER
ref.listen(loginProvider, (prev, next) { ref.listen(loginProvider, (prev, next) {
@@ -121,6 +109,7 @@ class LoginScreen extends HookConsumerWidget {
// }); // });
SanckbarWidget(context, next.message, snackbarType.warning); SanckbarWidget(context, next.message, snackbarType.warning);
} else if (next is LoginStateDone) { } else if (next is LoginStateDone) {
print("Login Done");
isLoading.value = false; isLoading.value = false;
isSuccess.value = true; isSuccess.value = true;
ref.read(currentPageProvider.state).update((state) => 0); ref.read(currentPageProvider.state).update((state) => 0);
@@ -135,9 +124,9 @@ class LoginScreen extends HookConsumerWidget {
// auth ke email // auth ke email
await googleSignIn.signIn(); await googleSignIn.signIn();
googleSignIn.onCurrentUserChanged.listen((account) async { googleSignIn.onCurrentUserChanged.listen((account) async {
ref // ref
.read(currentUserGoogleProvider.notifier) // .read(currentUserGoogleProvider.notifier)
.update((state) => account); // .update((state) => account);
// Check jika account tidak null dan belum login // Check jika account tidak null dan belum login
if (account != null && if (account != null &&
@@ -150,6 +139,21 @@ class LoginScreen extends HookConsumerWidget {
} }
}); });
googleSignIn.signInSilently(); googleSignIn.signInSilently();
// kalau sudah pernah login
if (googleSignIn.currentUser?.email != null) {
googleSignIn.signInSilently();
// ref.read(currentUserGoogleProvider.notifier).update(
// (state) => googleSignIn.currentUser,
// );
ref.read(loginProvider.notifier).login(
googleSignIn.currentUser?.email ?? "",
googleSignIn.currentUser?.id ?? "",
);
}
// Navigator.of(context)
// .pushNamedAndRemoveUntil(homeRoute, (route) => false);
} catch (error) { } catch (error) {
if (kDebugMode) { if (kDebugMode) {
print(error); print(error);