146 lines
3.9 KiB
Dart
146 lines
3.9 KiB
Dart
import 'package:appwrite/models.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_template_getx_provider/services/appwrite_service.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
class LoginController extends GetxController {
|
|
final szHeaderLogin = 'Login Page'.obs;
|
|
final szHeaderSignin = 'Signin Page'.obs;
|
|
final loginFormKey = GlobalKey<FormState>();
|
|
final isLoginScreen = true.obs;
|
|
late TextEditingController emailController;
|
|
late TextEditingController passwordController;
|
|
late TextEditingController nameController;
|
|
late AppWriteProvider appWriteProvider;
|
|
Session? _session;
|
|
User? _signedUser;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
appWriteProvider = AppWriteProvider();
|
|
emailController = TextEditingController();
|
|
passwordController = TextEditingController();
|
|
nameController = TextEditingController();
|
|
}
|
|
|
|
@override
|
|
void onReady() {}
|
|
|
|
@override
|
|
void onClose() {
|
|
emailController.dispose();
|
|
passwordController.dispose();
|
|
nameController.dispose();
|
|
}
|
|
|
|
switchScreen() {
|
|
_clearFields();
|
|
isLoginScreen.value = !isLoginScreen.value;
|
|
update();
|
|
}
|
|
|
|
void _clearFields() {
|
|
emailController.clear();
|
|
passwordController.clear();
|
|
nameController.clear();
|
|
}
|
|
|
|
void logOrSignIn() async {
|
|
_checkInputValueOfFormFields();
|
|
loginFormKey.currentState?.save();
|
|
if (isLoginScreen.value) {
|
|
// Perform login action
|
|
String email = emailController.text;
|
|
String password = passwordController.text;
|
|
// Add your login logic here
|
|
print('Logging in with Email: $email, Password: $password');
|
|
_session = await appWriteProvider.login({
|
|
'email': email,
|
|
'password': password,
|
|
});
|
|
if (_session!.current) {
|
|
Get.snackbar(
|
|
'Login Successful',
|
|
'You have been logged in successfully.',
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
);
|
|
} else {
|
|
Get.snackbar(
|
|
'Login Failed',
|
|
'Invalid email or password.',
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
);
|
|
}
|
|
} else {
|
|
// Perform signin action
|
|
String name = nameController.text;
|
|
String email = emailController.text;
|
|
String password = passwordController.text;
|
|
// Add your signin logic here
|
|
print('Signing in with Name: $name, Email: $email, Password: $password');
|
|
_signedUser = await appWriteProvider.signup({
|
|
'email': email,
|
|
'password': password,
|
|
'name': name,
|
|
});
|
|
if (_signedUser != null) {
|
|
Get.snackbar(
|
|
'Signin Successful',
|
|
'Your account has been created successfully.',
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
);
|
|
} else {
|
|
Get.snackbar(
|
|
'Signin Failed',
|
|
'An error occurred during account creation.',
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
);
|
|
}
|
|
}
|
|
_clearFields();
|
|
}
|
|
|
|
bool _checkInputValueOfFormFields() {
|
|
bool isValid = true;
|
|
if (!isLoginScreen.value) {
|
|
if (nameController.text == '') {
|
|
isValid = false;
|
|
}
|
|
}
|
|
if (emailController.text == '') {
|
|
isValid = false;
|
|
}
|
|
if (passwordController.text.isEmpty) {
|
|
isValid = false;
|
|
}
|
|
if (isValid == false) {
|
|
Get.snackbar(
|
|
'Invalid Input',
|
|
'Please fill in all required(*) fields.',
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
);
|
|
}
|
|
return isValid;
|
|
}
|
|
|
|
Future<bool> logout() async {
|
|
bool isLoggedOut = false;
|
|
isLoggedOut = await appWriteProvider.logout();
|
|
if (isLoggedOut) {
|
|
Get.snackbar(
|
|
'Logout Successful',
|
|
'You have been logged out successfully.',
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
);
|
|
} else {
|
|
Get.snackbar(
|
|
'Logout Failed',
|
|
'An error occurred during logout.',
|
|
snackPosition: SnackPosition.BOTTOM,
|
|
);
|
|
}
|
|
return isLoggedOut;
|
|
}
|
|
}
|