login.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:http/http.dart' as http;
  4. class AuthService {
  5. Future<bool> isUserLoggedIn() async {
  6. try {
  7. var url = Uri.http('127.0.0.1:3001', 'is_logged_in');
  8. var response = await http.get(url);
  9. print(response.body);
  10. if (response.statusCode == 200) {
  11. print('all good in the east!');
  12. List json = jsonDecode(response.body);
  13. print(json[0]);
  14. return true;
  15. }
  16. } catch (e) {
  17. print(e);
  18. }
  19. return false;
  20. }
  21. }
  22. class LoginPage extends StatefulWidget {
  23. const LoginPage({super.key});
  24. @override
  25. _LoginPageState createState() => _LoginPageState();
  26. }
  27. class _LoginPageState extends State<LoginPage> {
  28. final AuthService _authService = AuthService();
  29. // Controllers for capturing user input
  30. final TextEditingController _emailController = TextEditingController();
  31. final TextEditingController _passwordController = TextEditingController();
  32. // Key to identify the form
  33. final _formKey = GlobalKey<FormState>();
  34. void checkLogin() async {
  35. // try {
  36. // var url = Uri.http('127.0.0.1:3001', 'is_logged_in');
  37. // var response = await http.get(url);
  38. // print(response.body);
  39. // if (response.statusCode == 200) {
  40. // print('all good on the west');
  41. // }
  42. // } catch (e) {
  43. // print(e);
  44. // }
  45. bool isLoggedIn = await _authService.isUserLoggedIn();
  46. }
  47. // Function to handle login action
  48. void _handleLogin() {
  49. if (_formKey.currentState!.validate()) {
  50. // Perform login action (e.g., authenticate with backend)
  51. String email = _emailController.text;
  52. String password = _passwordController.text;
  53. // For demonstration, just print the values
  54. print('Email: $email');
  55. print('Password: $password');
  56. // Clear the input fields
  57. _emailController.clear();
  58. _passwordController.clear();
  59. }
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. return Scaffold(
  64. appBar: AppBar(
  65. title: const Text('Login Page'),
  66. ),
  67. body: Padding(
  68. padding: const EdgeInsets.all(16.0),
  69. child: Form(
  70. key: _formKey,
  71. child: Align(
  72. alignment: Alignment.centerLeft,
  73. child: Column(
  74. mainAxisSize: MainAxisSize.min,
  75. crossAxisAlignment: CrossAxisAlignment.start,
  76. children: [
  77. Text('Sign in to your email'),
  78. SizedBox(
  79. height: 5,
  80. ),
  81. // Email Field
  82. Container(
  83. width: 200,
  84. child: TextFormField(
  85. controller: _emailController,
  86. decoration: const InputDecoration(
  87. labelText: 'Email',
  88. hintText: 'Enter your email',
  89. helperMaxLines: 1,
  90. ),
  91. validator: (value) {
  92. // Simple email validation
  93. if (value == null || value.isEmpty) {
  94. return 'Please enter your email';
  95. }
  96. if (!RegExp(r'^\S+@\S+\.\S+$').hasMatch(value)) {
  97. return 'Please enter a valid email address';
  98. }
  99. return null;
  100. },
  101. ),
  102. ),
  103. const SizedBox(height: 16.0),
  104. // Password Field
  105. Container(
  106. width: 200,
  107. child: TextFormField(
  108. controller: _passwordController,
  109. decoration: const InputDecoration(
  110. labelText: 'Password',
  111. hintText: 'Enter your password',
  112. ),
  113. obscureText: true, // Hide the password text
  114. validator: (value) {
  115. // Simple password validation
  116. if (value == null || value.isEmpty) {
  117. return 'Please enter your password';
  118. }
  119. if (value.length < 6) {
  120. return 'Password must be at least 6 characters long';
  121. }
  122. return null;
  123. },
  124. ),
  125. ),
  126. const SizedBox(height: 32.0),
  127. // Login Button
  128. SizedBox(
  129. width: 200,
  130. child: ElevatedButton(
  131. onPressed: _handleLogin,
  132. child: const Text('Login'),
  133. ),
  134. ),
  135. SizedBox(
  136. width: 200,
  137. child: ElevatedButton(
  138. onPressed: checkLogin,
  139. child: const Text('checker'),
  140. ),
  141. )
  142. ],
  143. ),
  144. ),
  145. ),
  146. ),
  147. );
  148. }
  149. }