add login and input
This commit is contained in:
17
lib/controllers/input_controller.dart
Normal file
17
lib/controllers/input_controller.dart
Normal file
@@ -0,0 +1,17 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class InputController extends GetxController {
|
||||
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
@override
|
||||
void onReady() {}
|
||||
|
||||
@override
|
||||
void onClose() {}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:appwrite/appwrite.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class LoginController extends GetxController {
|
||||
final _account = Rxn<account_models.Account>();
|
||||
@@ -12,48 +13,129 @@ class LoginController extends GetxController {
|
||||
final nameController = TextEditingController();
|
||||
final _endpoint = dotenv.env['APPWRITE_ENDPOINT_URL'] ?? '';
|
||||
final _projectId = dotenv.env['APPWRITE_PROJECT_ID'] ?? '';
|
||||
final bool _selfSigned =
|
||||
(dotenv.env['APPWRITE_SELF_SIGNED'] ?? 'false').toLowerCase() == 'true';
|
||||
final _logedInUser = Rxn<user_models.User>();
|
||||
|
||||
final formKey = GlobalKey<FormState>();
|
||||
final isLogIn = false.obs;
|
||||
|
||||
account_models.Account? get account => _account.value;
|
||||
user_models.User? get logedInUser => _logedInUser.value;
|
||||
|
||||
|
||||
@override
|
||||
@override
|
||||
void onInit() {
|
||||
Client client = Client()
|
||||
.setEndpoint(_endpoint)
|
||||
.setProject(_projectId);
|
||||
Client client = Client().setEndpoint(_endpoint).setProject(_projectId);
|
||||
// Optional: Unsichere/self-signed Zertifikate im Dev zulassen (nicht im Web wirksam)
|
||||
if (!kIsWeb && _selfSigned) {
|
||||
client.setSelfSigned(status: true);
|
||||
}
|
||||
_account.value = Account(client);
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
Future<void> login(String email, String password) async {
|
||||
await _account.value!.createEmailPasswordSession(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
final user = await _account.value!.get();
|
||||
|
||||
_logedInUser.value = user;
|
||||
|
||||
emailValidator(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter your email';
|
||||
}
|
||||
final emailRegex = RegExp(r'^[^@]+@[^@]+\.[^@]+');
|
||||
if (!emailRegex.hasMatch(value)) {
|
||||
return 'Please enter a valid email address';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> register(String email, String password, String name) async {
|
||||
await _account.value!.create(
|
||||
userId: ID.unique(),
|
||||
email: email,
|
||||
password: password,
|
||||
name: name,
|
||||
);
|
||||
await login(email, password);
|
||||
passwordValidator(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter your password';
|
||||
}
|
||||
if (value.length < 6) {
|
||||
return 'Password must be at least 6 characters long';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
nameValidator(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter your name';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> login() async {
|
||||
try {
|
||||
var result = await _account.value!.createEmailPasswordSession(
|
||||
email: mailController.text,
|
||||
password: passwordController.text,
|
||||
);
|
||||
// ignore: avoid_print
|
||||
print('Login result: $result');
|
||||
final user = await _account.value!.get();
|
||||
_logedInUser.value = user;
|
||||
clearFields();
|
||||
Get.snackbar(
|
||||
'Erfolg',
|
||||
'Anmeldung erfolgreich',
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
);
|
||||
} on AppwriteException catch (e) {
|
||||
final msg = (e.message == null || e.message!.isEmpty)
|
||||
? 'Verbindungsfehler. Prüfe Zertifikat/Endpoint.'
|
||||
: e.message!;
|
||||
Get.snackbar(
|
||||
'Login fehlgeschlagen',
|
||||
msg,
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
duration: const Duration(seconds: 5),
|
||||
);
|
||||
} catch (e) {
|
||||
Get.snackbar(
|
||||
'Login fehlgeschlagen',
|
||||
e.toString(),
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
duration: const Duration(seconds: 5),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
clearFields(){
|
||||
mailController.clear();
|
||||
passwordController.clear();
|
||||
nameController.clear();
|
||||
}
|
||||
|
||||
Future<void> register() async {
|
||||
try {
|
||||
await _account.value!.create(
|
||||
userId: ID.unique(),
|
||||
email: mailController.text,
|
||||
password: passwordController.text,
|
||||
name: nameController.text,
|
||||
);
|
||||
await login();
|
||||
} on AppwriteException catch (e) {
|
||||
final msg = (e.message == null || e.message!.isEmpty)
|
||||
? 'Registrierung fehlgeschlagen. Prüfe Verbindung.'
|
||||
: e.message!;
|
||||
Get.snackbar(
|
||||
'Registrierung fehlgeschlagen',
|
||||
msg,
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
duration: const Duration(seconds: 5),
|
||||
);
|
||||
} catch (e) {
|
||||
Get.snackbar(
|
||||
'Registrierung fehlgeschlagen',
|
||||
e.toString(),
|
||||
snackPosition: SnackPosition.BOTTOM,
|
||||
duration: const Duration(seconds: 5),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _account.value!.deleteSession(sessionId: 'current');
|
||||
_logedInUser.value = null;
|
||||
}
|
||||
|
||||
|
||||
await _account.value!.deleteSession(sessionId: 'current');
|
||||
_logedInUser.value = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
@@ -63,3 +145,4 @@ class LoginController extends GetxController {
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user