import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import 'dart:io'; import 'MyHomePage.dart'; import 'classes/alert.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: LoginDemo(), ); } } class LoginDemo extends StatefulWidget { @override _LoginDemoState createState() => _LoginDemoState(); } class _LoginDemoState extends State with ShowErrorDialog { TextEditingController inputPseudo = TextEditingController(); TextEditingController inputPassword = TextEditingController(); Future _login(BuildContext context) async { var url = Uri.parse("http://localhost:8083/api/token"); var pseudo = inputPseudo.text; var password = inputPassword.text; if ((pseudo.isNotEmpty) && (password.isNotEmpty)) { try { String credentials = "${pseudo}:${password}"; Codec stringToBase64 = utf8.fuse(base64); String encoded = stringToBase64.encode(credentials); var response = await http.get(url, headers: { HttpHeaders.authorizationHeader: 'Basic $encoded', }); if ((response.statusCode == 200) || (response.statusCode == 201)) { SharedPreferences prefs = await SharedPreferences.getInstance(); var cookies = response.headers["set-cookie"].toString().split(";"); for (var cookie in cookies) { var cookiesMany = cookie.split(","); for (var cookie2 in cookiesMany) { switch (cookie2.split("=")[0]) { case "jwt": { prefs.setString("jwt", cookie2); } break; case "user": { prefs.setString("user", cookie2); } break; default: break; } } } Navigator.push( context, MaterialPageRoute( builder: (_) => MyHomePage(title: 'Flutter Demo'))); } else { var text = ""; switch (response.statusCode) { case 400: { text = "RequĂȘte mal construite"; } break; case 406: { text = "Mot de passe incorrect"; } break; case 404: { text = "Utilisateur inconnu"; } break; case 403: { text = "Utilisateur desactive"; } break; case 410: { text = "Token invalide"; } break; case 500: { text = "Probleme interne du serveur"; } break; default: { text = "Probleme d'authentification inconnu"; } break; } showErrorDialog(context, text); } } catch (e) { showErrorDialog(context, "${e}"); } } else { showErrorDialog(context, "Champ vide"); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: Text("Login Page"), ), body: SingleChildScrollView( child: Column( children: [ Padding( padding: const EdgeInsets.only(top: 60.0), child: Center( child: Container( width: 200, height: 150, /*decoration: BoxDecoration( color: Colors.red, borderRadius: BorderRadius.circular(50.0)),*/ child: Image.asset('asset/images/flutter-logo.png')), ), ), Padding( //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), padding: EdgeInsets.symmetric(horizontal: 15), child: TextField( controller: inputPseudo, decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Pseudo', hintText: 'Enter pseudo existent'), ), ), Padding( padding: const EdgeInsets.only( left: 15.0, right: 15.0, top: 15, bottom: 0), //padding: EdgeInsets.symmetric(horizontal: 15), child: TextField( controller: inputPassword, obscureText: true, decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Password', hintText: 'Enter secure password'), ), ), FlatButton( onPressed: () { //TODO FORGOT PASSWORD SCREEN GOES HERE }, child: Text( 'Forgot Password', style: TextStyle(color: Colors.blue, fontSize: 15), ), ), Container( height: 50, width: 250, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(20)), child: FlatButton( onPressed: () { _login(context); }, child: Text( 'Login', style: TextStyle(color: Colors.white, fontSize: 25), ), ), ), SizedBox( height: 130, ), Text('New User? Create Account') ], ), ), ); } }