hym_ui/lib/login.dart

215 lines
6.8 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
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!');
String jsonOuter = jsonDecode(response.body);
Map<String, dynamic> json = jsonDecode(jsonOuter);
// print(json["is_logged_in"]);
return json["is_logged_in"];
}
} catch (e) {
print(e);
}
return false;
}
Future<bool> loginUser(String email, String password) async {
try {} catch (e) {}
return false;
}
}
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
_LoginPageState createState() => _LoginPageState();
}
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
final AuthService _authService = AuthService();
@override
void initState() {
super.initState();
_checkLoginStatus();
}
Future<void> _checkLoginStatus() async {
// SharedPreferences prefs = await SharedPreferences.getInstance();
// print(prefs);
// bool isLoggedIn = prefs.getBool('isLoggedIn') ?? false;
bool isLoggedIn = await _authService.isUserLoggedIn();
print("is loogeed in $isLoggedIn");
if (isLoggedIn) {
print('here');
Navigator.pushReplacementNamed(context, '/home');
} else {
Navigator.pushReplacementNamed(context, '/login');
}
}
@override
Widget build(BuildContext context) {
return Center(
child: Scaffold(
body: Center(child: CircularProgressIndicator()),
),
);
}
}
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 Center(
child: Scaffold(
appBar: AppBar(
title: const Text('Login Page'),
),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Align(
alignment: Alignment.center,
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)) {
// print(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,
onPressed: () async {
await _authService.isUserLoggedIn();
// print(result);
},
child: const Text('checker'),
),
)
],
),
),
),
),
),
),
);
}
}