Skip to content

Commit

Permalink
Login/Signup Page with Firebase Auth Added
Browse files Browse the repository at this point in the history
  • Loading branch information
vivjamba committed Jul 17, 2021
1 parent 45a8b73 commit 15b6415
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
104 changes: 104 additions & 0 deletions lib/login.dart
Original file line number Diff line number Diff line change
@@ -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<LoginPage> {
// 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);
}
}
),
],
),
),
),
);
}
}
8 changes: 6 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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());
}

Expand All @@ -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'),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 15b6415

Please sign in to comment.