step 5 : add public key, login try using public and failed
This commit is contained in:
100
lib/repository/base_repository.dart
Normal file
100
lib/repository/base_repository.dart
Normal file
@@ -0,0 +1,100 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
abstract class BaseRepository {
|
||||
final Dio dio;
|
||||
BaseRepository({required this.dio});
|
||||
|
||||
Future<Map<String, dynamic>> post({
|
||||
required Map<String, dynamic> param,
|
||||
required String service,
|
||||
String? token,
|
||||
}) async {
|
||||
try {
|
||||
final response = await dio.post(
|
||||
service,
|
||||
data: jsonEncode(param),
|
||||
options: Options(
|
||||
headers: token != null
|
||||
? {
|
||||
HttpHeaders.contentTypeHeader: "application/json",
|
||||
HttpHeaders.authorizationHeader: "Bearer $token",
|
||||
}
|
||||
: {
|
||||
HttpHeaders.contentTypeHeader: "application/json",
|
||||
},
|
||||
contentType: "application/json",
|
||||
),
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw BaseRepositoryException(
|
||||
message: "Invalid Http Response ${response.statusCode}",
|
||||
);
|
||||
}
|
||||
Map<String, dynamic> jsonData = jsonDecode(response.data);
|
||||
if (jsonData["status"] != "OK") {
|
||||
throw BaseRepositoryException(
|
||||
message: jsonData["message"],
|
||||
);
|
||||
} else {
|
||||
return jsonData;
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
} on SocketException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
} on BaseRepositoryException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> get({
|
||||
required String service,
|
||||
String? token,
|
||||
}) async {
|
||||
try {
|
||||
final response = await dio.get(
|
||||
service,
|
||||
options: Options(
|
||||
headers: token != null
|
||||
? {
|
||||
HttpHeaders.contentTypeHeader: "application/json",
|
||||
HttpHeaders.authorizationHeader: "Bearer $token",
|
||||
}
|
||||
: {
|
||||
HttpHeaders.contentTypeHeader: "application/json",
|
||||
},
|
||||
contentType: "application/json",
|
||||
),
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw BaseRepositoryException(
|
||||
message: "Invalid Http Response ${response.statusCode}",
|
||||
);
|
||||
}
|
||||
Map<String, dynamic> jsonData = jsonDecode(response.data);
|
||||
if (jsonData["status"] != "OK") {
|
||||
throw BaseRepositoryException(
|
||||
message: jsonData["message"],
|
||||
);
|
||||
} else {
|
||||
return jsonData;
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
} on SocketException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
} on BaseRepositoryException catch (e) {
|
||||
throw BaseRepositoryException(message: e.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BaseRepositoryException implements Exception {
|
||||
final String? message;
|
||||
BaseRepositoryException({
|
||||
required this.message,
|
||||
});
|
||||
}
|
||||
@@ -1,31 +1,75 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.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 '../../app/constant.dart';
|
||||
import '../../provider/google_login_provider.dart';
|
||||
|
||||
import 'package:crypton/crypton.dart' as crypton;
|
||||
|
||||
class LoginScreen extends HookConsumerWidget {
|
||||
const LoginScreen({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? currentUser = ref.watch(currentUserProvider);
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() {
|
||||
googleSignIn.onCurrentUserChanged.listen((account) {
|
||||
// untuk update value ke provider google_login_provider yaitu currentUserProvider
|
||||
ref.read(currentUserProvider.notifier).update((state) => account);
|
||||
});
|
||||
googleSignIn.signInSilently();
|
||||
return (){};
|
||||
// googleSignIn.onCurrentUserChanged.listen((account) {
|
||||
// // untuk update value ke provider google_login_provider yaitu currentUserProvider
|
||||
// ref.read(currentUserProvider.notifier).update((state) => account);
|
||||
// });
|
||||
// googleSignIn.signInSilently();
|
||||
|
||||
postPublicKey();
|
||||
return () {};
|
||||
}, const []);
|
||||
|
||||
// fungsi untuk sync ke google mail
|
||||
@@ -47,7 +91,7 @@ class LoginScreen extends HookConsumerWidget {
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(context: context, y: 100),
|
||||
),
|
||||
(currentUser != null)
|
||||
(currentUser != null)
|
||||
? Container(
|
||||
child: ListTile(
|
||||
leading: GoogleUserCircleAvatar(
|
||||
|
||||
Reference in New Issue
Block a user