functionality to not show login screen, if alr logged in
This commit is contained in:
		
							parent
							
								
									c690bd4631
								
							
						
					
					
						commit
						54585b163d
					
				
					 2 changed files with 152 additions and 95 deletions
				
			
		
							
								
								
									
										242
									
								
								lib/login.dart
									
										
									
									
									
								
							
							
						
						
									
										242
									
								
								lib/login.dart
									
										
									
									
									
								
							| 
						 | 
					@ -1,6 +1,9 @@
 | 
				
			||||||
import 'dart:convert';
 | 
					import 'dart:convert';
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
import 'package:http/http.dart' as http;
 | 
					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 {
 | 
					class AuthService {
 | 
				
			||||||
  Future<bool> isUserLoggedIn() async {
 | 
					  Future<bool> isUserLoggedIn() async {
 | 
				
			||||||
| 
						 | 
					@ -10,15 +13,21 @@ class AuthService {
 | 
				
			||||||
      print(response.body);
 | 
					      print(response.body);
 | 
				
			||||||
      if (response.statusCode == 200) {
 | 
					      if (response.statusCode == 200) {
 | 
				
			||||||
        print('all good in the east!');
 | 
					        print('all good in the east!');
 | 
				
			||||||
        List json = jsonDecode(response.body);
 | 
					        String jsonOuter = jsonDecode(response.body);
 | 
				
			||||||
        print(json[0]);
 | 
					        Map<String, dynamic> json = jsonDecode(jsonOuter);
 | 
				
			||||||
        return true;
 | 
					        // print(json["is_logged_in"]);
 | 
				
			||||||
 | 
					        return json["is_logged_in"];
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } catch (e) {
 | 
					    } catch (e) {
 | 
				
			||||||
      print(e);
 | 
					      print(e);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return false;
 | 
					    return false;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Future<bool> loginUser(String email, String password) async {
 | 
				
			||||||
 | 
					    try {} catch (e) {}
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class LoginPage extends StatefulWidget {
 | 
					class LoginPage extends StatefulWidget {
 | 
				
			||||||
| 
						 | 
					@ -28,6 +37,43 @@ class LoginPage extends StatefulWidget {
 | 
				
			||||||
  _LoginPageState createState() => _LoginPageState();
 | 
					  _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> {
 | 
					class _LoginPageState extends State<LoginPage> {
 | 
				
			||||||
  final AuthService _authService = AuthService();
 | 
					  final AuthService _authService = AuthService();
 | 
				
			||||||
  // Controllers for capturing user input
 | 
					  // Controllers for capturing user input
 | 
				
			||||||
| 
						 | 
					@ -37,20 +83,19 @@ class _LoginPageState extends State<LoginPage> {
 | 
				
			||||||
  // Key to identify the form
 | 
					  // Key to identify the form
 | 
				
			||||||
  final _formKey = GlobalKey<FormState>();
 | 
					  final _formKey = GlobalKey<FormState>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void checkLogin() async {
 | 
					  // void checkLogin() async {
 | 
				
			||||||
    // try {
 | 
					  //   try {
 | 
				
			||||||
    //   var url = Uri.http('127.0.0.1:3001', 'is_logged_in');
 | 
					  //     var url = Uri.http('127.0.0.1:3001', 'is_logged_in');
 | 
				
			||||||
    //   var response = await http.get(url);
 | 
					  //     var response = await http.get(url);
 | 
				
			||||||
    //   print(response.body);
 | 
					  //     print(response.body);
 | 
				
			||||||
    //   if (response.statusCode == 200) {
 | 
					  //     if (response.statusCode == 200) {
 | 
				
			||||||
    //     print('all good on the west');
 | 
					  //       print('all good on the west');
 | 
				
			||||||
    //   }
 | 
					  //     }
 | 
				
			||||||
    // } catch (e) {
 | 
					  //   } catch (e) {
 | 
				
			||||||
    //   print(e);
 | 
					  //     print(e);
 | 
				
			||||||
    // }
 | 
					  //   }
 | 
				
			||||||
    bool isLoggedIn = await _authService.isUserLoggedIn();
 | 
					  //   // bool isLoggedIn = await _authService.isUserLoggedIn();
 | 
				
			||||||
    
 | 
					  // }
 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Function to handle login action
 | 
					  // Function to handle login action
 | 
				
			||||||
  void _handleLogin() {
 | 
					  void _handleLogin() {
 | 
				
			||||||
| 
						 | 
					@ -71,86 +116,95 @@ class _LoginPageState extends State<LoginPage> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context) {
 | 
					  Widget build(BuildContext context) {
 | 
				
			||||||
    return Scaffold(
 | 
					    return Center(
 | 
				
			||||||
      appBar: AppBar(
 | 
					      child: Scaffold(
 | 
				
			||||||
        title: const Text('Login Page'),
 | 
					        appBar: AppBar(
 | 
				
			||||||
      ),
 | 
					          title: const Text('Login Page'),
 | 
				
			||||||
      body: Padding(
 | 
					        ),
 | 
				
			||||||
        padding: const EdgeInsets.all(16.0),
 | 
					        body: Center(
 | 
				
			||||||
        child: Form(
 | 
					          child: SingleChildScrollView(
 | 
				
			||||||
          key: _formKey,
 | 
					            padding: const EdgeInsets.all(16.0),
 | 
				
			||||||
          child: Align(
 | 
					            child: Form(
 | 
				
			||||||
            alignment: Alignment.centerLeft,
 | 
					              key: _formKey,
 | 
				
			||||||
            child: Column(
 | 
					              child: Align(
 | 
				
			||||||
              mainAxisSize: MainAxisSize.min,
 | 
					                alignment: Alignment.center,
 | 
				
			||||||
              crossAxisAlignment: CrossAxisAlignment.start,
 | 
					                child: Column(
 | 
				
			||||||
              children: [
 | 
					                  mainAxisSize: MainAxisSize.min,
 | 
				
			||||||
                Text('Sign in to your email'),
 | 
					                  crossAxisAlignment: CrossAxisAlignment.start,
 | 
				
			||||||
                SizedBox(
 | 
					                  children: [
 | 
				
			||||||
                  height: 5,
 | 
					                    Text('Sign in to your email'),
 | 
				
			||||||
                ),
 | 
					                    SizedBox(
 | 
				
			||||||
                // Email Field
 | 
					                      height: 5,
 | 
				
			||||||
                Container(
 | 
					 | 
				
			||||||
                  width: 200,
 | 
					 | 
				
			||||||
                  child: TextFormField(
 | 
					 | 
				
			||||||
                    controller: _emailController,
 | 
					 | 
				
			||||||
                    decoration: const InputDecoration(
 | 
					 | 
				
			||||||
                      labelText: 'Email',
 | 
					 | 
				
			||||||
                      hintText: 'Enter your email',
 | 
					 | 
				
			||||||
                      helperMaxLines: 1,
 | 
					 | 
				
			||||||
                    ),
 | 
					                    ),
 | 
				
			||||||
                    validator: (value) {
 | 
					                    // Email Field
 | 
				
			||||||
                      // Simple email validation
 | 
					                    Container(
 | 
				
			||||||
                      if (value == null || value.isEmpty) {
 | 
					                      width: 200,
 | 
				
			||||||
                        return 'Please enter your email';
 | 
					                      child: TextFormField(
 | 
				
			||||||
                      }
 | 
					                        controller: _emailController,
 | 
				
			||||||
                      if (!RegExp(r'^\S+@\S+\.\S+$').hasMatch(value)) {
 | 
					                        decoration: const InputDecoration(
 | 
				
			||||||
                        return 'Please enter a valid email address';
 | 
					                          labelText: 'Email',
 | 
				
			||||||
                      }
 | 
					                          hintText: 'Enter your email',
 | 
				
			||||||
                      return null;
 | 
					                          helperMaxLines: 1,
 | 
				
			||||||
                    },
 | 
					                        ),
 | 
				
			||||||
                  ),
 | 
					                        validator: (value) {
 | 
				
			||||||
                ),
 | 
					                          // Simple email validation
 | 
				
			||||||
                const SizedBox(height: 16.0),
 | 
					                          if (value == null || value.isEmpty) {
 | 
				
			||||||
                // Password Field
 | 
					                            return 'Please enter your email';
 | 
				
			||||||
                Container(
 | 
					                          }
 | 
				
			||||||
                  width: 200,
 | 
					                          if (!RegExp(r'^\S+@\S+\.\S+$').hasMatch(value)) {
 | 
				
			||||||
                  child: TextFormField(
 | 
					                            // print(value);
 | 
				
			||||||
                    controller: _passwordController,
 | 
					                            return 'Please enter a valid email address';
 | 
				
			||||||
                    decoration: const InputDecoration(
 | 
					                          }
 | 
				
			||||||
                      labelText: 'Password',
 | 
					                          return null;
 | 
				
			||||||
                      hintText: 'Enter your password',
 | 
					                        },
 | 
				
			||||||
 | 
					                      ),
 | 
				
			||||||
                    ),
 | 
					                    ),
 | 
				
			||||||
                    obscureText: true, // Hide the password text
 | 
					                    const SizedBox(height: 16.0),
 | 
				
			||||||
                    validator: (value) {
 | 
					                    // Password Field
 | 
				
			||||||
                      // Simple password validation
 | 
					                    Container(
 | 
				
			||||||
                      if (value == null || value.isEmpty) {
 | 
					                      width: 200,
 | 
				
			||||||
                        return 'Please enter your password';
 | 
					                      child: TextFormField(
 | 
				
			||||||
                      }
 | 
					                        controller: _passwordController,
 | 
				
			||||||
                      if (value.length < 6) {
 | 
					                        decoration: const InputDecoration(
 | 
				
			||||||
                        return 'Password must be at least 6 characters long';
 | 
					                          labelText: 'Password',
 | 
				
			||||||
                      }
 | 
					                          hintText: 'Enter your password',
 | 
				
			||||||
                      return null;
 | 
					                        ),
 | 
				
			||||||
                    },
 | 
					                        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'),
 | 
				
			||||||
 | 
					                      ),
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					                  ],
 | 
				
			||||||
                ),
 | 
					                ),
 | 
				
			||||||
                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'),
 | 
					 | 
				
			||||||
                  ),
 | 
					 | 
				
			||||||
                )
 | 
					 | 
				
			||||||
              ],
 | 
					 | 
				
			||||||
            ),
 | 
					            ),
 | 
				
			||||||
          ),
 | 
					          ),
 | 
				
			||||||
        ),
 | 
					        ),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -22,10 +22,13 @@ class HyM extends StatelessWidget {
 | 
				
			||||||
      theme: ThemeData.light(),
 | 
					      theme: ThemeData.light(),
 | 
				
			||||||
      title: 'HyM',
 | 
					      title: 'HyM',
 | 
				
			||||||
      // home: HomeScreen(),
 | 
					      // home: HomeScreen(),
 | 
				
			||||||
      home: HomeScreen(),
 | 
					      // home: HomeScreen(),
 | 
				
			||||||
 | 
					      initialRoute: "/",
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      routes: {
 | 
					      routes: {
 | 
				
			||||||
 | 
					        "/": (context) => SplashScreen(),
 | 
				
			||||||
        "/login": (context) => const LoginPage(),
 | 
					        "/login": (context) => const LoginPage(),
 | 
				
			||||||
 | 
					        "/home": (context) => HomeScreen(),
 | 
				
			||||||
        // "/email": (context) => EmailPage(),
 | 
					        // "/email": (context) => EmailPage(),
 | 
				
			||||||
        "/contacts": (context) => ContactsPage(),
 | 
					        "/contacts": (context) => ContactsPage(),
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue