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/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:google_sign_in/google_sign_in.dart';
|
import 'package:google_sign_in/google_sign_in.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
import '../../app/constant.dart';
|
import '../../app/constant.dart';
|
||||||
import '../../provider/google_login_provider.dart';
|
import '../../provider/google_login_provider.dart';
|
||||||
|
|
||||||
|
import 'package:crypton/crypton.dart' as crypton;
|
||||||
|
|
||||||
class LoginScreen extends HookConsumerWidget {
|
class LoginScreen extends HookConsumerWidget {
|
||||||
const LoginScreen({super.key});
|
const LoginScreen({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
|
|
||||||
// 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? currentUser = ref.watch(currentUserProvider);
|
GoogleSignInAccount? currentUser = ref.watch(currentUserProvider);
|
||||||
final googleSignIn = ref.watch(googleSignInProvider);
|
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(() {
|
useEffect(() {
|
||||||
googleSignIn.onCurrentUserChanged.listen((account) {
|
// googleSignIn.onCurrentUserChanged.listen((account) {
|
||||||
// untuk update value ke provider google_login_provider yaitu currentUserProvider
|
// // untuk update value ke provider google_login_provider yaitu currentUserProvider
|
||||||
ref.read(currentUserProvider.notifier).update((state) => account);
|
// ref.read(currentUserProvider.notifier).update((state) => account);
|
||||||
});
|
// });
|
||||||
googleSignIn.signInSilently();
|
// googleSignIn.signInSilently();
|
||||||
return (){};
|
|
||||||
|
postPublicKey();
|
||||||
|
return () {};
|
||||||
}, const []);
|
}, const []);
|
||||||
|
|
||||||
// fungsi untuk sync ke google mail
|
// fungsi untuk sync ke google mail
|
||||||
|
|||||||
@@ -6,9 +6,11 @@ import FlutterMacOS
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
import geolocator_apple
|
import geolocator_apple
|
||||||
|
import path_provider_foundation
|
||||||
import shared_preferences_foundation
|
import shared_preferences_foundation
|
||||||
|
|
||||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||||
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
|
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
|
||||||
|
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||||
}
|
}
|
||||||
|
|||||||
6
publickey/public.pem
Normal file
6
publickey/public.pem
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7Exn1YjlSoZ/ihKQbH04iogX1
|
||||||
|
auWL4In0i1x5akuvlSkkzoLRWXM7sbTrHIjyqDJuuFrcA/ilygYB4QYrIk3v8owS
|
||||||
|
HcJB64f3qCrxyE8dK4iPmtogH9Gb7+L2LMOV/UjHwtP3TgAiMEM55qVIf1qbQGxZ
|
||||||
|
WNtIR4RHD0uMVtOCtwIDAQAB
|
||||||
|
-----END PUBLIC KEY-----
|
||||||
48
pubspec.lock
48
pubspec.lock
@@ -25,6 +25,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.4.2"
|
version: "2.4.2"
|
||||||
|
asn1lib:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: asn1lib
|
||||||
|
sha256: "21afe4333076c02877d14f4a89df111e658a6d466cbfc802eb705eb91bd5adfd"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.5.0"
|
||||||
async:
|
async:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -81,6 +89,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.3"
|
version: "3.0.3"
|
||||||
|
crypton:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: crypton
|
||||||
|
sha256: "17b6631fbf89e389d421b46629132287ed37d601b2ad1357445826ab85022271"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.2.1"
|
||||||
cupertino_icons:
|
cupertino_icons:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@@ -432,6 +448,30 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.8.3"
|
version: "1.8.3"
|
||||||
|
path_provider:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: path_provider
|
||||||
|
sha256: b27217933eeeba8ff24845c34003b003b2b22151de3c908d0e679e8fe1aa078b
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.2"
|
||||||
|
path_provider_android:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: path_provider_android
|
||||||
|
sha256: "477184d672607c0a3bf68fbbf601805f92ef79c82b64b4d6eb318cbca4c48668"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.2.2"
|
||||||
|
path_provider_foundation:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: path_provider_foundation
|
||||||
|
sha256: "19314d595120f82aca0ba62787d58dde2cc6b5df7d2f0daf72489e38d1b57f2d"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.3.1"
|
||||||
path_provider_linux:
|
path_provider_linux:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -480,6 +520,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.8"
|
version: "2.1.8"
|
||||||
|
pointycastle:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: pointycastle
|
||||||
|
sha256: "7c1e5f0d23c9016c5bbd8b1473d0d3fb3fc851b876046039509e18e0c7485f2c"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.7.3"
|
||||||
polylabel:
|
polylabel:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -46,6 +46,8 @@ dependencies:
|
|||||||
flutter_map: ^6.1.0
|
flutter_map: ^6.1.0
|
||||||
latlong2: ^0.9.0
|
latlong2: ^0.9.0
|
||||||
google_sign_in: ^6.1.6
|
google_sign_in: ^6.1.6
|
||||||
|
crypton: ^2.2.1
|
||||||
|
path_provider: ^2.0.2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
@@ -72,6 +74,7 @@ flutter:
|
|||||||
# To add assets to your application, add an assets section, like this:
|
# To add assets to your application, add an assets section, like this:
|
||||||
assets:
|
assets:
|
||||||
- images/
|
- images/
|
||||||
|
- publickey/
|
||||||
# - images/a_dot_ham.jpeg
|
# - images/a_dot_ham.jpeg
|
||||||
|
|
||||||
# An image asset can refer to one or more resolution-specific "variants", see
|
# An image asset can refer to one or more resolution-specific "variants", see
|
||||||
|
|||||||
Reference in New Issue
Block a user