feat: login session with google
This commit is contained in:
parent
5787cea803
commit
b81ebefc6f
|
@ -45,4 +45,6 @@ app.*.map.json
|
||||||
/android/app/release
|
/android/app/release
|
||||||
|
|
||||||
# FVM Version Cache
|
# FVM Version Cache
|
||||||
.fvm/
|
.fvm/
|
||||||
|
|
||||||
|
*.env
|
|
@ -30,8 +30,18 @@ android {
|
||||||
versionName = flutter.versionName
|
versionName = flutter.versionName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
signingConfigs {
|
||||||
|
debug {
|
||||||
|
keyAlias = "keyDebugQuiz"
|
||||||
|
keyPassword = "uppercase12"
|
||||||
|
storeFile = file("debugKeystore.jks")
|
||||||
|
storePassword = "uppercase12"
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
buildTypes {
|
buildTypes {
|
||||||
release {
|
debug {
|
||||||
// TODO: Add your own signing config for the release build.
|
// TODO: Add your own signing config for the release build.
|
||||||
// Signing with the debug keys for now, so `flutter run --release` works.
|
// Signing with the debug keys for now, so `flutter run --release` works.
|
||||||
signingConfig = signingConfigs.debug
|
signingConfig = signingConfigs.debug
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<application
|
<application
|
||||||
android:label="quiz_app"
|
android:label="quiz_app"
|
||||||
android:name="${applicationName}"
|
android:name="${applicationName}"
|
||||||
|
@ -17,12 +18,12 @@
|
||||||
while the Flutter UI initializes. After that, this theme continues
|
while the Flutter UI initializes. After that, this theme continues
|
||||||
to determine the Window background behind the Flutter UI. -->
|
to determine the Window background behind the Flutter UI. -->
|
||||||
<meta-data
|
<meta-data
|
||||||
android:name="io.flutter.embedding.android.NormalTheme"
|
android:name="io.flutter.embedding.android.NormalTheme"
|
||||||
android:resource="@style/NormalTheme"
|
android:resource="@style/NormalTheme"
|
||||||
/>
|
/>
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN"/>
|
<action android:name="android.intent.action.MAIN" />
|
||||||
<category android:name="android.intent.category.LAUNCHER"/>
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
<!-- Don't delete the meta-data below.
|
<!-- Don't delete the meta-data below.
|
||||||
|
@ -38,8 +39,8 @@
|
||||||
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
|
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
|
||||||
<queries>
|
<queries>
|
||||||
<intent>
|
<intent>
|
||||||
<action android:name="android.intent.action.PROCESS_TEXT"/>
|
<action android:name="android.intent.action.PROCESS_TEXT" />
|
||||||
<data android:mimeType="text/plain"/>
|
<data android:mimeType="text/plain" />
|
||||||
</intent>
|
</intent>
|
||||||
</queries>
|
</queries>
|
||||||
</manifest>
|
</manifest>
|
|
@ -1,5 +1,5 @@
|
||||||
class APIEndpoint {
|
class APIEndpoint {
|
||||||
static const String baseUrl = "http://127.0.0.1:8000/api";
|
static const String baseUrl = "http://192.168.1.9:5000/api";
|
||||||
|
|
||||||
static const String login = "/login";
|
static const String login = "/login";
|
||||||
static const String loginGoogle = "/login/google";
|
static const String loginGoogle = "/login/google";
|
||||||
|
|
|
@ -4,26 +4,35 @@ import 'package:http/http.dart' as http;
|
||||||
import 'package:google_sign_in/google_sign_in.dart';
|
import 'package:google_sign_in/google_sign_in.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:quiz_app/core/endpoint/api_endpoint.dart';
|
import 'package:quiz_app/core/endpoint/api_endpoint.dart';
|
||||||
|
import 'package:quiz_app/core/utils/logger.dart';
|
||||||
|
|
||||||
class LoginController extends GetxController {
|
class LoginController extends GetxController {
|
||||||
final TextEditingController emailController = TextEditingController();
|
final TextEditingController emailController = TextEditingController();
|
||||||
final TextEditingController passwordController = TextEditingController();
|
final TextEditingController passwordController = TextEditingController();
|
||||||
|
|
||||||
var isPasswordHidden = true.obs;
|
var isPasswordHidden = true.obs;
|
||||||
|
var isLoading = false.obs; // Loading state for UI
|
||||||
|
final GoogleSignIn _googleSignIn = GoogleSignIn(); // Singleton instance
|
||||||
|
|
||||||
void togglePasswordVisibility() {
|
void togglePasswordVisibility() {
|
||||||
isPasswordHidden.value = !isPasswordHidden.value;
|
isPasswordHidden.value = !isPasswordHidden.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
final GoogleSignIn _googleSignIn = GoogleSignIn();
|
/// **🔹 Login via Email & Password**
|
||||||
|
|
||||||
// Login menggunakan Flask Backend (Email & Password)
|
|
||||||
Future<void> loginWithEmail() async {
|
Future<void> loginWithEmail() async {
|
||||||
String email = emailController.text.trim();
|
String email = emailController.text.trim();
|
||||||
String password = passwordController.text.trim();
|
String password = passwordController.text.trim();
|
||||||
|
|
||||||
|
if (email.isEmpty || password.isEmpty) {
|
||||||
|
Get.snackbar("Error", "Email and password are required");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
isLoading.value = true;
|
||||||
|
|
||||||
var response = await http.post(
|
var response = await http.post(
|
||||||
Uri.parse(APIEndpoint.baseUrl + APIEndpoint.login),
|
Uri.parse("${APIEndpoint.baseUrl}${APIEndpoint.login}"),
|
||||||
body: jsonEncode({"email": email, "password": password}),
|
body: jsonEncode({"email": email, "password": password}),
|
||||||
headers: {"Content-Type": "application/json"},
|
headers: {"Content-Type": "application/json"},
|
||||||
);
|
);
|
||||||
|
@ -31,39 +40,80 @@ class LoginController extends GetxController {
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
var data = jsonDecode(response.body);
|
var data = jsonDecode(response.body);
|
||||||
String token = data['token'];
|
String token = data['token'];
|
||||||
|
|
||||||
|
// await _secureStorage.write(key: "auth_token", value: token);
|
||||||
|
|
||||||
Get.snackbar("Success", "Login successful!");
|
Get.snackbar("Success", "Login successful!");
|
||||||
|
logC.i("Login Token: $token");
|
||||||
} else {
|
} else {
|
||||||
Get.snackbar("Error", "Invalid email or password");
|
var errorMsg = jsonDecode(response.body)['message'] ?? "Invalid credentials";
|
||||||
|
Get.snackbar("Error", errorMsg);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e, stackTrace) {
|
||||||
|
logC.e(e, stackTrace: stackTrace);
|
||||||
Get.snackbar("Error", "Failed to connect to server");
|
Get.snackbar("Error", "Failed to connect to server");
|
||||||
|
} finally {
|
||||||
|
isLoading.value = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login menggunakan Google (Tanpa Firebase)
|
|
||||||
Future<void> loginWithGoogle() async {
|
Future<void> loginWithGoogle() async {
|
||||||
try {
|
try {
|
||||||
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
|
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
|
||||||
if (googleUser == null) return;
|
if (googleUser == null) {
|
||||||
|
Get.snackbar("Error", "Google Sign-In canceled");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
logC.i("Google User ID: ${googleUser.id}");
|
||||||
|
logC.i("Google User Email: ${googleUser.email}");
|
||||||
|
logC.i("Google User Display Name: ${googleUser.displayName}");
|
||||||
|
|
||||||
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
|
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
|
||||||
String idToken = googleAuth.idToken ?? "";
|
|
||||||
|
logC.i("Google Access Token: ${googleAuth.accessToken}");
|
||||||
|
logC.i("Google ID Token: ${googleAuth.idToken}");
|
||||||
|
|
||||||
|
if (googleAuth.idToken == null || googleAuth.idToken!.isEmpty) {
|
||||||
|
Get.snackbar("Error", "Google sign-in failed. No token received.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var response = await http.post(
|
var response = await http.post(
|
||||||
Uri.parse(APIEndpoint.baseUrl + APIEndpoint.loginGoogle),
|
Uri.parse("${APIEndpoint.baseUrl}${APIEndpoint.loginGoogle}"),
|
||||||
body: jsonEncode({"token": idToken}),
|
body: jsonEncode({"token": googleAuth.idToken}),
|
||||||
headers: {"Content-Type": "application/json"},
|
headers: {"Content-Type": "application/json"},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
var data = jsonDecode(response.body);
|
var data = jsonDecode(response.body);
|
||||||
String token = data['token']; // Simpan token untuk sesi login
|
String token = data['token'];
|
||||||
|
|
||||||
Get.snackbar("Success", "Google login successful!");
|
Get.snackbar("Success", "Google login successful!");
|
||||||
|
logC.i("Google Login Token: $token");
|
||||||
} else {
|
} else {
|
||||||
Get.snackbar("Error", "Google login failed");
|
var errorMsg = jsonDecode(response.body)['message'] ?? "Google login failed";
|
||||||
|
Get.snackbar("Error", errorMsg);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e, stackTrace) {
|
||||||
|
logC.e("Google Sign-In Error: $e", stackTrace: stackTrace);
|
||||||
Get.snackbar("Error", "Google sign-in error");
|
Get.snackbar("Error", "Google sign-in error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// **🔹 Logout Function**
|
||||||
|
Future<void> logout() async {
|
||||||
|
try {
|
||||||
|
await _googleSignIn.signOut();
|
||||||
|
// await _secureStorage.delete(key: "auth_token");
|
||||||
|
|
||||||
|
emailController.clear();
|
||||||
|
passwordController.clear();
|
||||||
|
|
||||||
|
Get.snackbar("Success", "Logged out successfully");
|
||||||
|
} catch (e) {
|
||||||
|
logC.e("Logout error: $e");
|
||||||
|
Get.snackbar("Error", "Logout failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,6 @@ class LoginView extends GetView<LoginController> {
|
||||||
onPressed: () => controller.loginWithEmail(),
|
onPressed: () => controller.loginWithEmail(),
|
||||||
child: const Text("Login with Email"),
|
child: const Text("Login with Email"),
|
||||||
),
|
),
|
||||||
|
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
|
|
||||||
// Google Sign-In Button
|
// Google Sign-In Button
|
||||||
|
@ -65,6 +64,11 @@ class LoginView extends GetView<LoginController> {
|
||||||
elevation: 2,
|
elevation: 2,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () => controller.logout(),
|
||||||
|
child: const Text("log out"),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
|
@ -62,6 +62,14 @@ packages:
|
||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
flutter_dotenv:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: flutter_dotenv
|
||||||
|
sha256: b7c7be5cd9f6ef7a78429cabd2774d3c4af50e79cb2b7593e3d5d763ef95c61b
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.2.1"
|
||||||
flutter_lints:
|
flutter_lints:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description:
|
description:
|
||||||
|
|
|
@ -38,6 +38,7 @@ dependencies:
|
||||||
|
|
||||||
google_sign_in: ^6.2.2
|
google_sign_in: ^6.2.2
|
||||||
http: ^1.3.0
|
http: ^1.3.0
|
||||||
|
flutter_dotenv: ^5.2.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Reference in New Issue