step 7 : login, logout (bug disconnect), home screen top info user login
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:absensi_sas_flutter/screen/login/login_provider.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.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:path_provider/path_provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../app/constant.dart';
|
||||
import '../../app/route.dart';
|
||||
import '../../model/auth_model.dart';
|
||||
import '../../provider/current_user_provider.dart';
|
||||
import '../../provider/google_login_provider.dart';
|
||||
|
||||
import 'package:crypton/crypton.dart' as crypton;
|
||||
import '../../widget/sankbar_widget.dart';
|
||||
|
||||
class LoginScreen extends HookConsumerWidget {
|
||||
const LoginScreen({super.key});
|
||||
@@ -22,60 +24,130 @@ class LoginScreen extends HookConsumerWidget {
|
||||
// inisialisasi
|
||||
// ref.watch itu sama spt memantau state terus menerus
|
||||
// ref.read itu memantau namun hny 1x saja
|
||||
GoogleSignInAccount? currentUser = ref.watch(currentUserProvider);
|
||||
GoogleSignInAccount? currentUserGoogle =
|
||||
ref.watch(currentUserGoogleProvider);
|
||||
final googleSignIn = ref.watch(googleSignInProvider);
|
||||
|
||||
Future<void> postPublicKey() async {
|
||||
Dio dio = Dio();
|
||||
String url = "https://example.com/api/post_endpoint";
|
||||
try {
|
||||
final publicPem = await rootBundle.loadString('assets/public_key.pem');
|
||||
final publicKey = crypton.RSAPublicKey.fromPEM(publicPem);
|
||||
|
||||
// GoogleSignInAccount? currentUser = ref.watch(currentUserProvider);
|
||||
// final googleSignIn = ref.watch(googleSignInProvider);
|
||||
// return filePath;
|
||||
|
||||
publicKey.encrypt("testing satu dua tiga");
|
||||
Map<String, dynamic> data = {
|
||||
"publickey": publicKey,
|
||||
"id_google_sign_in": "102110797605607117832",
|
||||
"email": "sindhu@sismedia.com"
|
||||
};
|
||||
|
||||
Response response = await dio.post(
|
||||
url,
|
||||
data: data,
|
||||
);
|
||||
|
||||
// Menampilkan respons dari server
|
||||
print("Response data: ${response.data}");
|
||||
|
||||
// googleSignIn.onCurrentUserChanged.listen((account) {
|
||||
// // untuk update value ke provider google_login_provider yaitu currentUserProvider
|
||||
// ref.read(currentUserProvider.notifier).update((state) => account);
|
||||
// });
|
||||
// googleSignIn.signInSilently();
|
||||
} catch (e) {
|
||||
print("Error reading file: $e");
|
||||
}
|
||||
}
|
||||
final isLoading = useState(false);
|
||||
final errorMessage = useState("");
|
||||
final isSuccess = useState(false);
|
||||
|
||||
useEffect(() {
|
||||
// googleSignIn.onCurrentUserChanged.listen((account) {
|
||||
// // untuk update value ke provider google_login_provider yaitu currentUserProvider
|
||||
// ref.read(currentUserProvider.notifier).update((state) => account);
|
||||
// });
|
||||
// googleSignIn.signInSilently();
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
||||
final staffID = ref.read(currentUserProvider)?.model.staffId ?? "0";
|
||||
|
||||
postPublicKey();
|
||||
googleSignIn.onCurrentUserChanged.listen((account) {
|
||||
ref
|
||||
.read(currentUserGoogleProvider.notifier)
|
||||
.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 () {};
|
||||
}, const []);
|
||||
}, [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;
|
||||
Navigator.of(context).pushNamedAndRemoveUntil(
|
||||
homeRoute,
|
||||
(route) => false,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// ref.read(currentUserProvider.notifier).state = authModel;
|
||||
|
||||
// Navigator.of(context).pushNamedAndRemoveUntil(
|
||||
// homeRoute,
|
||||
// (route) => false,
|
||||
// );
|
||||
});
|
||||
|
||||
// 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) {
|
||||
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> handleSignIn() async {
|
||||
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();
|
||||
} catch (error) {
|
||||
if (kDebugMode) {
|
||||
print(error);
|
||||
@@ -91,27 +163,27 @@ class LoginScreen extends HookConsumerWidget {
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(context: context, y: 100),
|
||||
),
|
||||
(currentUser != null)
|
||||
? Container(
|
||||
child: ListTile(
|
||||
leading: GoogleUserCircleAvatar(
|
||||
identity: currentUser,
|
||||
),
|
||||
title: Text(
|
||||
currentUser.displayName ?? "",
|
||||
),
|
||||
subtitle: Text(
|
||||
currentUser.email,
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: Icon(Icons.logout_outlined),
|
||||
onPressed: () async {
|
||||
await googleSignIn.disconnect();
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
:
|
||||
// (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(
|
||||
@@ -182,6 +254,17 @@ class LoginScreen extends HookConsumerWidget {
|
||||
],
|
||||
),
|
||||
|
||||
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),
|
||||
),
|
||||
@@ -197,7 +280,7 @@ class LoginScreen extends HookConsumerWidget {
|
||||
height: Constant.getActualYPhone(context: context, y: 48),
|
||||
child: ElevatedButton(
|
||||
onPressed: () async {
|
||||
await handleSignIn();
|
||||
await handleSignInGmail();
|
||||
},
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
|
||||
Reference in New Issue
Block a user