From 15b6415af9a4df9cd26371087743ee1acaf30982 Mon Sep 17 00:00:00 2001 From: vivjamba <85517762+vivjamba@users.noreply.github.com> Date: Fri, 16 Jul 2021 21:53:10 -0400 Subject: [PATCH] Login/Signup Page with Firebase Auth Added --- lib/login.dart | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/main.dart | 8 +++- pubspec.yaml | 2 +- 3 files changed, 111 insertions(+), 3 deletions(-) diff --git a/lib/login.dart b/lib/login.dart index e69de29..91ff1a2 100644 --- a/lib/login.dart +++ b/lib/login.dart @@ -0,0 +1,104 @@ +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:firebase_core/firebase_core.dart'; +import 'package:flutter/material.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; + +import 'destinationpage.dart'; + +class LoginPage extends StatefulWidget { + @override + _LoginPageState createState() => _LoginPageState(); +} + +class _LoginPageState extends State { + // Initialize FlutterFire + var emailController = TextEditingController(); + var passwordController = TextEditingController(); + var nameController = TextEditingController(); + var schoolController = TextEditingController(); + + @override + Widget build(BuildContext context) { + return Scaffold( + body: Center( + child: Container( + margin: const EdgeInsets.only(left: 20.0, right: 20.0), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text("Login/Signup", + style: TextStyle( + color: Colors.lightBlueAccent, + fontSize: 50.0, + ) + ), + Container( + margin: const EdgeInsets.only(top: 20.0, bottom: 5), + child: TextField( + controller: emailController, + obscureText: false, + decoration: InputDecoration( + border: OutlineInputBorder(), + labelText: 'Email', + ), + ), + ), + Container( + margin: const EdgeInsets.only(top: 5, bottom: 20.0), + child: TextField( + controller: passwordController, + obscureText: true, + decoration: InputDecoration( + border: OutlineInputBorder(), + labelText: 'Password', + ), + ), + ), + RaisedButton( + color: Colors.lightBlueAccent, + child: Text("Log In!"), + onPressed: () async { + + //Test code to print contents of controllers + // print(emailController.text); + // print(passwordController.text); + + try { + await FirebaseAuth.instance.signInWithEmailAndPassword( + email: emailController.text, + password: passwordController.text + ); + print('Login successful!'); + Navigator.push( + context, + MaterialPageRoute(builder: (context) => DestinationPage(title: 'Saved Destinations')), + ); + } on FirebaseAuthException catch (e) { + print('Login Failed. Error code: ${e.code}'); + print(e.message); + } + }, + ), + RaisedButton( + child: Text("Sign Up!"), + color: Colors.lightBlueAccent, + onPressed: () async { + try { + await FirebaseAuth.instance.createUserWithEmailAndPassword( + email: emailController.text, + password: passwordController.text + ); + print('Account created!'); + } on FirebaseAuthException catch (e) { + print('Signup Failed. Error code: ${e.code}'); + print(e.message); + } + } + ), + ], + ), + ), + ), + ); + } +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index e0b9052..d27d85b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,11 @@ +import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'destinationpage.dart'; +import 'login.dart'; -void main() { +void main() async { + WidgetsFlutterBinding.ensureInitialized(); + await Firebase.initializeApp(); runApp(MyApp()); } @@ -23,7 +27,7 @@ class MyApp extends StatelessWidget { // is not restarted. primarySwatch: Colors.blue, ), - home: MyHomePage(title: 'Flutter Demo Home Page'), + home: LoginPage(),//MyHomePage(title: 'Flutter Demo Home Page'), ); } } diff --git a/pubspec.yaml b/pubspec.yaml index 05a7fb3..5c6ba5f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,10 +24,10 @@ dependencies: flutter: sdk: flutter #dependencies for firebase (auth and firestore) - firebase_core: "^1.4.0" cloud_firestore: ^1.0.0 # new firebase_auth: ^1.0.0 # new provider: ^5.0.0 # new + firebase_core : # newer english_words: ^4.0.0 google_fonts: ^2.0.0