This commit is contained in:
Juan Marulanda De Los Rios 2024-10-24 00:15:13 -04:00
parent 747f84a2b7
commit 36c161cb55
2 changed files with 161 additions and 0 deletions

160
lib/login.dart Normal file
View File

@ -0,0 +1,160 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class AuthService {
Future<bool> isUserLoggedIn() async {
try {
var url = Uri.http('127.0.0.1:3001', 'is_logged_in');
var response = await http.get(url);
print(response.body);
if (response.statusCode == 200) {
print('all good in the east!');
List json = jsonDecode(response.body);
print(json[0]);
return true;
}
} catch (e) {
print(e);
}
return false;
}
}
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final AuthService _authService = AuthService();
// Controllers for capturing user input
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
// Key to identify the form
final _formKey = GlobalKey<FormState>();
void checkLogin() async {
// try {
// var url = Uri.http('127.0.0.1:3001', 'is_logged_in');
// var response = await http.get(url);
// print(response.body);
// if (response.statusCode == 200) {
// print('all good on the west');
// }
// } catch (e) {
// print(e);
// }
bool isLoggedIn = await _authService.isUserLoggedIn();
}
// Function to handle login action
void _handleLogin() {
if (_formKey.currentState!.validate()) {
// Perform login action (e.g., authenticate with backend)
String email = _emailController.text;
String password = _passwordController.text;
// For demonstration, just print the values
print('Email: $email');
print('Password: $password');
// Clear the input fields
_emailController.clear();
_passwordController.clear();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login Page'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Align(
alignment: Alignment.centerLeft,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Sign in to your email'),
SizedBox(
height: 5,
),
// Email Field
Container(
width: 200,
child: TextFormField(
controller: _emailController,
decoration: const InputDecoration(
labelText: 'Email',
hintText: 'Enter your email',
helperMaxLines: 1,
),
validator: (value) {
// Simple email validation
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'^\S+@\S+\.\S+$').hasMatch(value)) {
return 'Please enter a valid email address';
}
return null;
},
),
),
const SizedBox(height: 16.0),
// Password Field
Container(
width: 200,
child: TextFormField(
controller: _passwordController,
decoration: const InputDecoration(
labelText: 'Password',
hintText: 'Enter your password',
),
obscureText: true, // Hide the password text
validator: (value) {
// Simple password validation
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;
},
),
),
const SizedBox(height: 32.0),
// Login Button
SizedBox(
width: 200,
child: ElevatedButton(
onPressed: _handleLogin,
child: const Text('Login'),
),
),
SizedBox(
width: 200,
child: ElevatedButton(
onPressed: checkLogin,
child: const Text('checker'),
),
)
],
),
),
),
),
);
}
}

View File

@ -18,6 +18,7 @@ dependencies:
english_words: ^4.0.0
provider: ^6.0.0
intl: ^0.19.0
dev_dependencies:
flutter_test: