From 11b10b7587bfe4beb18b348f5976d20a13348801 Mon Sep 17 00:00:00 2001 From: saphi Date: Thu, 28 Sep 2023 13:38:12 +0200 Subject: [PATCH] aggiunto schermate login e register --- ios/firebase_app_id_file.json | 6 +- lib/firebase_options.dart | 68 +++++++++++++ lib/main.dart | 11 +- lib/myApp.dart | 4 +- lib/navigation.dart | 27 ++++- lib/pages/LoginView.dart | 45 ++++++++ lib/pages/RegisterView.dart | 186 ++++++++++++++++++++++++++++++++++ pubspec.yaml | 4 + 8 files changed, 344 insertions(+), 7 deletions(-) create mode 100644 lib/firebase_options.dart create mode 100644 lib/pages/LoginView.dart create mode 100644 lib/pages/RegisterView.dart diff --git a/ios/firebase_app_id_file.json b/ios/firebase_app_id_file.json index ec3404c..07bd753 100644 --- a/ios/firebase_app_id_file.json +++ b/ios/firebase_app_id_file.json @@ -1,7 +1,7 @@ { "file_generated_by": "FlutterFire CLI", "purpose": "FirebaseAppID & ProjectID for this Firebase app in this directory", - "GOOGLE_APP_ID": "1:164857321043:ios:b497e5d24254d60e1ad8a6", - "FIREBASE_PROJECT_ID": "progetto-m335", - "GCM_SENDER_ID": "164857321043" + "GOOGLE_APP_ID": "1:23966937248:ios:d7915a6ac5618efeb599c5", + "FIREBASE_PROJECT_ID": "m-335-3d14b", + "GCM_SENDER_ID": "23966937248" } \ No newline at end of file diff --git a/lib/firebase_options.dart b/lib/firebase_options.dart new file mode 100644 index 0000000..391a954 --- /dev/null +++ b/lib/firebase_options.dart @@ -0,0 +1,68 @@ +// File generated by FlutterFire CLI. +// ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members +import 'package:firebase_core/firebase_core.dart' show FirebaseOptions; +import 'package:flutter/foundation.dart' + show defaultTargetPlatform, kIsWeb, TargetPlatform; + +/// Default [FirebaseOptions] for use with your Firebase apps. +/// +/// Example: +/// ```dart +/// import 'firebase_options.dart'; +/// // ... +/// await Firebase.initializeApp( +/// options: DefaultFirebaseOptions.currentPlatform, +/// ); +/// ``` +class DefaultFirebaseOptions { + static FirebaseOptions get currentPlatform { + if (kIsWeb) { + throw UnsupportedError( + 'DefaultFirebaseOptions have not been configured for web - ' + 'you can reconfigure this by running the FlutterFire CLI again.', + ); + } + switch (defaultTargetPlatform) { + case TargetPlatform.android: + return android; + case TargetPlatform.iOS: + return ios; + case TargetPlatform.macOS: + throw UnsupportedError( + 'DefaultFirebaseOptions have not been configured for macos - ' + 'you can reconfigure this by running the FlutterFire CLI again.', + ); + case TargetPlatform.windows: + throw UnsupportedError( + 'DefaultFirebaseOptions have not been configured for windows - ' + 'you can reconfigure this by running the FlutterFire CLI again.', + ); + case TargetPlatform.linux: + throw UnsupportedError( + 'DefaultFirebaseOptions have not been configured for linux - ' + 'you can reconfigure this by running the FlutterFire CLI again.', + ); + default: + throw UnsupportedError( + 'DefaultFirebaseOptions are not supported for this platform.', + ); + } + } + + static const FirebaseOptions android = FirebaseOptions( + apiKey: 'AIzaSyA5Mz_Az4vCKnFl_qJwiQ07cJzXSYDAqmw', + appId: '1:23966937248:android:9694d7a3e940cff3b599c5', + messagingSenderId: '23966937248', + projectId: 'm-335-3d14b', + storageBucket: 'm-335-3d14b.appspot.com', + ); + + static const FirebaseOptions ios = FirebaseOptions( + apiKey: 'AIzaSyDEJpIubqU-jFzjLP2s8S01B3Gh6W0eNgM', + appId: '1:23966937248:ios:d7915a6ac5618efeb599c5', + messagingSenderId: '23966937248', + projectId: 'm-335-3d14b', + storageBucket: 'm-335-3d14b.appspot.com', + iosBundleId: 'ch.ameti.progettoM335Flutter', + ); +} diff --git a/lib/main.dart b/lib/main.dart index cbaaa02..eedf7e0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,15 @@ +import 'package:firebase_core/firebase_core.dart'; +import 'firebase_options.dart'; + import 'package:flutter/material.dart'; import 'myApp.dart'; -void main() { +void main() async { + + WidgetsFlutterBinding.ensureInitialized(); + await Firebase.initializeApp( + options: DefaultFirebaseOptions.currentPlatform, + ); + runApp(MyApp()); } diff --git a/lib/myApp.dart b/lib/myApp.dart index 2f9d223..3f57cc8 100644 --- a/lib/myApp.dart +++ b/lib/myApp.dart @@ -4,13 +4,13 @@ import 'navigation.dart'; class MyApp extends StatelessWidget { const MyApp({ Key? key }) : super(key: key); + @override Widget build(BuildContext context) { return MaterialApp( title: 'My App', theme: ThemeData( - useMaterial3: true, - primaryColor: Colors.red, + useMaterial3: true ), home: Navigation() ); diff --git a/lib/navigation.dart b/lib/navigation.dart index c524013..7f1e35f 100644 --- a/lib/navigation.dart +++ b/lib/navigation.dart @@ -1,8 +1,13 @@ +import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; +import 'package:progetto_m335_flutter/pages/LoginView.dart'; +import 'package:progetto_m335_flutter/pages/RegisterView.dart'; import 'pages/TodayView.dart'; import 'pages/InboxView.dart'; import 'pages/NotesView.dart'; + + class Navigation extends StatefulWidget { const Navigation({super.key}); @@ -12,12 +17,15 @@ class Navigation extends StatefulWidget { class _NavigationState extends State { - int _selectedIndex = 0; + int _selectedIndex = 1; static const List _widgetOptions = [ + LoginView(), + RegisterView(), TodayView(), InboxView(), NotesView(), ]; + FirebaseAuth auth = FirebaseAuth.instance; void _onItemTapped(int index) { setState(() { @@ -25,6 +33,20 @@ class _NavigationState extends State { }); } + @override + void initState() { + FirebaseAuth.instance + .authStateChanges() + .listen((User? user) { + if (user == null) { + print('User is currently signed out!'); + } else { + print('User is signed in!'); + } + }); + super.initState(); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -34,6 +56,8 @@ class _NavigationState extends State { body: Center(child: _widgetOptions.elementAt(_selectedIndex)), bottomNavigationBar: BottomNavigationBar( items: const [ + BottomNavigationBarItem(icon: Icon(Icons.login), label: "Login"), + BottomNavigationBarItem(icon: Icon(Icons.app_registration), label: "Register"), BottomNavigationBarItem( icon: Icon(Icons.calendar_today), label: "today"), BottomNavigationBarItem(icon: Icon(Icons.inbox), label: "Inbox"), @@ -41,6 +65,7 @@ class _NavigationState extends State { ], currentIndex: _selectedIndex, onTap: _onItemTapped, + type: BottomNavigationBarType.fixed, ), ); } diff --git a/lib/pages/LoginView.dart b/lib/pages/LoginView.dart new file mode 100644 index 0000000..d157e10 --- /dev/null +++ b/lib/pages/LoginView.dart @@ -0,0 +1,45 @@ +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:flutter/material.dart'; +import 'package:google_sign_in/google_sign_in.dart'; + +class LoginView extends StatefulWidget { + const LoginView({super.key}); + + @override + State createState() => _LoginViewState(); +} + +class _LoginViewState extends State { + + Future signInWithGoogle() async { + // Trigger the authentication flow + final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn(); + + // Obtain the auth details from the request + final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication; + + // Create a new credential + final credential = GoogleAuthProvider.credential( + accessToken: googleAuth?.accessToken, + idToken: googleAuth?.idToken, + ); + + // Once signed in, return the UserCredential + return await FirebaseAuth.instance.signInWithCredential(credential); + } + + @override + void initState() { + signInWithGoogle().then((value) => print(value)); + super.initState(); + } + + @override + Widget build(BuildContext context) { + return const Scaffold( + body: Center( + child: Icon(Icons.login) + ) + ); + } +} diff --git a/lib/pages/RegisterView.dart b/lib/pages/RegisterView.dart new file mode 100644 index 0000000..46e6c11 --- /dev/null +++ b/lib/pages/RegisterView.dart @@ -0,0 +1,186 @@ +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:flutter/material.dart'; +import 'package:form_field_validator/form_field_validator.dart'; +import 'package:flutter/foundation.dart'; +import 'package:progetto_m335_flutter/pages/LoginView.dart'; + +class RegisterView extends StatefulWidget { + const RegisterView({Key? key}) : super(key: key); + + @override + State createState() => _RegisterViewState(); +} + +class _RegisterViewState extends State { + String email = ''; + String password = ''; + bool showPassword = false; + Color buttonColor = Colors.blue[200]!; + + void register() async { + print("$email $password"); + if (_formkey.currentState!.validate()) { + try { + final credential = + await FirebaseAuth.instance.createUserWithEmailAndPassword( + email: email, + password: password, + ); + } on FirebaseAuthException catch (e) { + if (e.code == 'weak-password') { + print('The password provided is too weak.'); + } else if (e.code == 'email-already-in-use') { + print('The account already exists for that email.'); + } + } catch (e) { + print(e); + } + } + } + + Map userData = {}; + final _formkey = GlobalKey(); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('Register'), + ), + body: SingleChildScrollView( + child: Padding( + padding: const EdgeInsets.all(12.0), + child: Form( + key: _formkey, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: TextFormField( + onChanged: (val) { + setState(() { + email = val; + if (password.length > 6 && _formkey.currentState!.validate()) { + buttonColor = Colors.blue[400]!; + } else { + buttonColor = Colors.blue[200]!; + } + }); + }, + validator: MultiValidator([ + RequiredValidator(errorText: 'Enter email address'), + EmailValidator( + errorText: 'Please correct email filled'), + ]), + decoration: const InputDecoration( + hintText: 'Email', + labelText: 'Email', + prefixIcon: Icon( + Icons.email, + color: Colors.grey, + ), + errorStyle: TextStyle(fontSize: 18.0), + border: OutlineInputBorder( + borderSide: BorderSide(color: Colors.red), + borderRadius: + BorderRadius.all(Radius.circular(9.0)))), + ), + ), + Padding( + padding: const EdgeInsets.all(8.0), + child: TextFormField( + obscureText: !showPassword, + onChanged: (val) { + setState(() { + password = val; + if (password.length > 6 && _formkey.currentState!.validate()) { + buttonColor = Colors.blue[400]!; + } else { + buttonColor = Colors.blue[200]!; + } + }); + }, + validator: MultiValidator([ + RequiredValidator(errorText: 'Enter a password'), + PatternValidator( + r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,}$', + errorText: 'enter a valid password'), + ]), + decoration: InputDecoration( + hintText: 'Password', + labelText: 'Password', + prefixIcon: const Icon( + Icons.key, + color: Colors.grey, + ), + suffixIcon: Padding( + padding: const EdgeInsets.only(right: 20.0), + child: IconButton( + icon: Icon( + showPassword + ? Icons.visibility_off + : Icons.visibility, + color: Colors.grey, + ), + onPressed: () { + setState(() { + showPassword = !showPassword; + }); + }), + ), + errorStyle: const TextStyle(fontSize: 18.0), + border: const OutlineInputBorder( + borderSide: BorderSide(color: Colors.red), + borderRadius: + BorderRadius.all(Radius.circular(9.0)))), + )), + Center( + child: Padding( + padding: const EdgeInsets.all(18.0), + child: SizedBox( + // margin: EdgeInsets.fromLTRB(200, 20, 50, 0), + width: MediaQuery.of(context).size.width, + + height: 50, + // margin: EdgeInsets.fromLTRB(200, 20, 50, 0), + child: ElevatedButton( + style: ElevatedButton.styleFrom( + backgroundColor: buttonColor, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10), + ), + ), + child: const Text( + 'Register', + style: TextStyle(color: Colors.white, fontSize: 22), + ), + onPressed: () { + register(); + }, + ), + ), + )), + Center( + child: Padding( + padding: const EdgeInsets.only(top: 20), + child: Center( + child: TextButton( + child: const Text('Already have an account? Login'), + onPressed: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => const LoginView()), + ); + }, + ), + ), + ), + ), + ], + )), + ), + )); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 911c217..f03ea1a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,12 +31,16 @@ dependencies: flutter: sdk: flutter + google_sign_in: ^6.1.5 + # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 firebase_core: ^2.16.0 url_launcher: ^6.1.14 + firebase_auth: ^4.10.0 + form_field_validator: ^1.1.0 dev_dependencies: flutter_test: