step 5 : perbaikan login logic di login screen

This commit is contained in:
sindhu
2024-08-25 12:22:15 +07:00
parent b4dea334d1
commit 08dd105fe5
5 changed files with 365 additions and 101 deletions

View File

@@ -54,45 +54,6 @@ class LoginScreen extends HookConsumerWidget {
token: xmodel["model"]["token"],
),
);
// ref.read(currentUserProvider.notifier).state = authModel;
await googleSignIn.signInSilently();
googleSignIn.onCurrentUserChanged.listen((account) {
// ref
// .read(currentUserGoogleProvider.notifier)
// .update((state) => account);
ref.read(currentUserProvider.notifier).state = authModel;
ref.read(currentPageProvider.state).update((state) => 0);
if (account != null &&
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 () {};
}, []);
@@ -140,7 +101,7 @@ class LoginScreen extends HookConsumerWidget {
});
googleSignIn.signInSilently();
// kalau sudah pernah login
// kalau sudah pernah login
if (googleSignIn.currentUser?.email != null) {
googleSignIn.signInSilently();
// ref.read(currentUserGoogleProvider.notifier).update(

View File

@@ -0,0 +1,356 @@
import 'dart:convert';
import 'package:absensi_sas/screen/login/login_provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../app/constant.dart';
import '../../app/route.dart';
import '../../model/auth_model.dart';
import '../../provider/current_menu_provider.dart';
import '../../provider/current_user_provider.dart';
import '../../provider/google_login_provider.dart';
import '../../widget/sankbar_widget.dart';
class LoginScreenV1 extends HookConsumerWidget {
const LoginScreenV1({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
// inisialisasi
// ref.watch itu sama spt memantau state terus menerus
// ref.read itu memantau namun hny 1x saja
// GoogleSignInAccount? currentUserGoogle =
// ref.watch(currentUserGoogleProvider);
final googleSignIn = ref.watch(googleSignInProvider);
final isLoading = useState(false);
// final errorMessage = useState("");
final isSuccess = useState(false);
useEffect(() {
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"],
),
);
// ref.read(currentUserProvider.notifier).state = authModel;
await googleSignIn.signInSilently();
googleSignIn.onCurrentUserChanged.listen((account) {
// ref
// .read(currentUserGoogleProvider.notifier)
// .update((state) => account);
ref.read(currentUserProvider.notifier).state = authModel;
ref.read(currentPageProvider.state).update((state) => 0);
if (account != null &&
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 () {};
}, []);
// LISTEN PROVIDER
ref.listen(loginProvider, (prev, next) {
if (next is LoginStateLoading) {
isLoading.value = true;
} else if (next is LoginStateError) {
isLoading.value = false;
// errorMessage.value = next.message;
// Timer(const Duration(seconds: 3), () {
// errorMessage.value = "";
// });
SanckbarWidget(context, next.message, snackbarType.warning);
} else if (next is LoginStateDone) {
print("Login Done");
isLoading.value = false;
isSuccess.value = true;
ref.read(currentPageProvider.state).update((state) => 0);
Navigator.of(context)
.pushNamedAndRemoveUntil(homeRoute, (route) => false);
}
});
// fungsi untuk sync ke google mail
Future<void> handleSignInGmail() async {
try {
// auth ke email
await googleSignIn.signIn();
googleSignIn.onCurrentUserChanged.listen((account) async {
// ref
// .read(currentUserGoogleProvider.notifier)
// .update((state) => account);
// Check jika account tidak null dan belum login
if (account != null &&
ref.read(currentUserProvider)?.model.staffId == "0") {
// Lakukan login
ref.read(loginProvider.notifier).login(
account.email,
account.id,
);
}
});
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) {
if (kDebugMode) {
print(error);
}
}
}
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: Constant.getActualYPhone(context: context, y: 100),
),
// (currentUserGoogle != null)
// ? Container(
// child: ListTile(
// leading: GoogleUserCircleAvatar(
// identity: currentUserGoogle,
// ),
// title: Text(
// currentUserGoogle.displayName ?? "",
// ),
// subtitle: Text(
// currentUserGoogle.email,
// ),
// trailing: IconButton(
// icon: Icon(Icons.logout_outlined),
// onPressed: () async {
// await googleSignIn.disconnect();
// },
// ),
// ),
// )
// :
// Logo Landscape
Padding(
padding: EdgeInsets.only(
top: Constant.getActualYPhone(context: context, y: 120),
left: Constant.getActualXPhone(context: context, x: 91),
right: Constant.getActualXPhone(context: context, x: 90),
// bottom: Constant.getActualYPhone(context: context, y: y)
),
child: Container(
width: Constant.getActualXPhone(context: context, x: 209),
height: Constant.getActualYPhone(context: context, y: 70),
decoration: BoxDecoration(
// color: Colors.green,
image: DecorationImage(
// fit: BoxFit.contain,
image: AssetImage('images/logo_sismedika_landscape.png'),
),
),
),
),
SizedBox(
height: Constant.getActualYPhone(context: context, y: 100),
),
// title
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Selamat Datang',
style: Constant.titleH1_700(context: context)
.copyWith(color: Constant.textBlack),
),
Container(
width: Constant.getActualXPhone(context: context, x: 24),
height: Constant.getActualYPhone(context: context, y: 24),
// color: Colors.redAccent,
decoration: BoxDecoration(
// color: Colors.green,
image: DecorationImage(
// fit: BoxFit.contain,
image: AssetImage('images/emoji_handshake.png'),
),
),
),
],
),
SizedBox(
height: Constant.getActualYPhone(context: context, y: 7),
),
// title grey
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Silahkan masuk untuk mengakses akun Anda',
style: Constant.titleH2_600(context: context).copyWith(
color: Constant.textLightGrey,
),
),
],
),
SizedBox(
height: Constant.getActualYPhone(context: context, y: 10),
),
// ElevatedButton(
// onPressed: () async {
// await googleSignIn.disconnect();
// },
// child: Text('logout'),
// ),
SizedBox(
height: Constant.getActualYPhone(context: context, y: 64),
),
// button login
Padding(
padding: EdgeInsets.only(
left: Constant.getActualXPhone(context: context, x: 23),
right: Constant.getActualXPhone(context: context, x: 23),
),
child: SizedBox(
width: Constant.getActualXPhone(context: context, x: 344),
height: Constant.getActualYPhone(context: context, y: 48),
child: ElevatedButton(
onPressed: () async {
await handleSignInGmail();
},
child: Stack(
children: [
(isLoading.value)
? SizedBox(
width: Constant.getActualXPhone(
context: context, x: 24),
height: Constant.getActualYPhone(
context: context, y: 24),
child: Center(
child: CircularProgressIndicator(
color: Constant.textTrueBlack,
),
),
)
: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: Constant.getActualXPhone(
context: context, x: 24),
height: Constant.getActualYPhone(
context: context, y: 24),
decoration: BoxDecoration(
// color: Colors.green,
image: DecorationImage(
// fit: BoxFit.contain,
image: AssetImage(
'images/icon_google.png'),
),
),
),
SizedBox(
width: Constant.getActualXPhone(
context: context, x: 2),
),
Text(
'Continue with Google',
style: Constant.logintitle_700(
context: context)
.copyWith(
color: Constant.textBlack,
),
),
],
),
],
),
style: ButtonStyle(
backgroundColor: MaterialStateColor.resolveWith(
(st) => Colors.white,
),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
side: BorderSide(
color: Color.fromRGBO(145, 158, 171, 0.32),
),
),
),
),
),
),
),
],
),
),
),
);
}
}

View File

@@ -7,12 +7,14 @@ import Foundation
import file_selector_macos
import geolocator_apple
import google_sign_in_ios
import path_provider_foundation
import shared_preferences_foundation
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
FLTGoogleSignInPlugin.register(with: registry.registrar(forPlugin: "FLTGoogleSignInPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}

View File

@@ -1,22 +1,6 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
_fe_analyzer_shared:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: ae92f5d747aee634b87f89d9946000c2de774be1d6ac3e58268224348cd0101a
url: "https://pub.dev"
source: hosted
version: "61.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
sha256: ea3d8652bda62982addfd92fdc2d0214e5f82e43325104990d4f4c4a2a313562
url: "https://pub.dev"
source: hosted
version: "5.13.0"
archive:
dependency: transitive
description:
@@ -400,14 +384,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.2.2"
glob:
dependency: transitive
description:
name: glob
sha256: "0e7014b3b7d4dac1ca4d6114f82bf1782ee86745b9b42a92c9289c23d8a0ab63"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
google_identity_services_web:
dependency: transitive
description:
@@ -420,10 +396,10 @@ packages:
dependency: "direct main"
description:
name: google_sign_in
sha256: "8f8b94880f2753ccb796744259da529674e49b9af2e372abf6978c590c0ebfef"
sha256: "0b8787cb9c1a68ad398e8010e8c8766bfa33556d2ab97c439fb4137756d7308f"
url: "https://pub.dev"
source: hosted
version: "6.1.6"
version: "6.2.1"
google_sign_in_android:
dependency: transitive
description:
@@ -436,10 +412,10 @@ packages:
dependency: transitive
description:
name: google_sign_in_ios
sha256: "81495441405c138e3c638f5097bebaa0db644567b3976e08944cfb8926ff2e6d"
sha256: a058c9880be456f21e2e8571c1126eaacd570bdc5b6c6d9d15aea4bdf22ca9fe
url: "https://pub.dev"
source: hosted
version: "5.6.5"
version: "5.7.6"
google_sign_in_platform_interface:
dependency: transitive
description:
@@ -792,14 +768,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.3.2"
package_config:
dependency: transitive
description:
name: package_config
sha256: "1c5b77ccc91e4823a5af61ee74e6b972db1ef98c2ff5a18d3161c982a55448bd"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
path:
dependency: transitive
description:
@@ -928,14 +896,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.14.0"
pigeon:
dependency: transitive
description:
name: pigeon
sha256: "5a79fd0b10423f6b5705525e32015597f861c31220b522a67d1e6b580da96719"
url: "https://pub.dev"
source: hosted
version: "11.0.1"
platform:
dependency: transitive
description:
@@ -976,14 +936,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.0"
pub_semver:
dependency: transitive
description:
name: pub_semver
sha256: "40d3ab1bbd474c4c2328c91e3a7df8c6dd629b79ece4c4bd04bee496a224fb0c"
url: "https://pub.dev"
source: hosted
version: "2.1.4"
riverpod:
dependency: transitive
description:
@@ -1213,14 +1165,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "14.2.5"
watcher:
dependency: transitive
description:
name: watcher
sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
web_socket_channel:
dependency: transitive
description:

View File

@@ -46,7 +46,8 @@ dependencies:
shared_preferences: ^2.0.1
flutter_map: ^6.1.0
latlong2: ^0.9.0
google_sign_in: ^6.1.6
# google_sign_in: ^6.1.6
google_sign_in: ^6.2.1
crypton: ^2.2.1
path_provider: ^2.0.2
intl: ^0.17.0