-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
203312e
commit b18d466
Showing
6 changed files
with
311 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,5 @@ app.*.map.json | |
/android/app/debug | ||
/android/app/profile | ||
/android/app/release | ||
|
||
pubspec.lock |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'package:diary/RegisterPage.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class AuthPage extends StatefulWidget { | ||
const AuthPage({Key? key}) : super(key: key); | ||
@override | ||
_AuthPage createState() => _AuthPage(); | ||
} | ||
|
||
class _AuthPage extends State<AuthPage> { | ||
final TextEditingController _emailController = TextEditingController(); | ||
final TextEditingController _passwordController = TextEditingController(); | ||
String _emailError = ""; | ||
String _passwordError = ""; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
debugShowCheckedModeBanner: false, | ||
title: "Dairy", | ||
home: Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Login'), | ||
), | ||
body: Center( | ||
child: Column( | ||
children: [ | ||
TextField( | ||
controller: _emailController, | ||
onChanged: (v) { | ||
setState(() { | ||
_emailError = ""; | ||
}); | ||
}, | ||
keyboardType: TextInputType.emailAddress, | ||
decoration: InputDecoration( | ||
labelText: "Email", | ||
errorText: _emailError == "" ? null : _emailError, | ||
), | ||
), | ||
TextField( | ||
controller: _passwordController, | ||
onChanged: (v) { | ||
setState(() { | ||
_passwordError = ""; | ||
}); | ||
}, | ||
obscureText: true, | ||
enableSuggestions: false, | ||
autocorrect: false, | ||
decoration: InputDecoration( | ||
labelText: "Password", | ||
errorText: _passwordError == "" ? null : _passwordError, | ||
), | ||
), | ||
Builder(builder: (context) { | ||
return TextButton( | ||
onPressed: () => register(context), | ||
child: Text("Don't have an account? Register")); | ||
}), | ||
ElevatedButton(onPressed: submit, child: Text("SUBMIT")) | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} | ||
|
||
void register(BuildContext context) { | ||
Navigator.of(context) | ||
.push(MaterialPageRoute(builder: (context) => RegisterPage())); | ||
} | ||
|
||
void submit() async { | ||
String email = _emailController.text; | ||
String password = _passwordController.text; | ||
try { | ||
UserCredential userCredential = await FirebaseAuth.instance | ||
.signInWithEmailAndPassword(email: email, password: password); | ||
} on FirebaseAuthException catch (e) { | ||
if (e.code == 'user-not-found' || e.code == 'invalid-email') { | ||
setState(() { | ||
_emailError = "Couldn't find such an email, try registering?"; | ||
}); | ||
} else if (e.code == 'wrong-password') { | ||
setState(() { | ||
_passwordError = "Incorrect password."; | ||
}); | ||
} else { | ||
setState(() { | ||
_emailError = e.code; | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class HomePage extends StatefulWidget { | ||
const HomePage({Key? key}) : super(key: key); | ||
@override | ||
_HomePage createState() => _HomePage(); | ||
} | ||
|
||
class _HomePage extends State<HomePage> { | ||
CollectionReference usersRef = FirebaseFirestore.instance.collection('users'); | ||
User firebaseUser = FirebaseAuth.instance.currentUser!; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
debugShowCheckedModeBanner: false, | ||
title: "Dairy", | ||
home: Scaffold( | ||
appBar: AppBar( | ||
title: const Text("Welcome"), | ||
actions: <Widget>[ | ||
PopupMenuButton<String>(onSelected: (s) { | ||
if (s == 'Logout') { | ||
FirebaseAuth.instance.signOut(); | ||
} | ||
}, itemBuilder: (BuildContext context) { | ||
return {"Logout"}.map((String choice) { | ||
return PopupMenuItem<String>( | ||
child: Text(choice), | ||
value: choice, | ||
); | ||
}).toList(); | ||
}) | ||
], | ||
), | ||
body: Center( | ||
child: FutureBuilder( | ||
future: usersRef.doc(firebaseUser.uid).get(), | ||
builder: (BuildContext context, | ||
AsyncSnapshot<DocumentSnapshot> snapshot) { | ||
if (snapshot.connectionState == ConnectionState.done) { | ||
Map<String, dynamic> data = | ||
snapshot.data!.data() as Map<String, dynamic>; | ||
String name = data['name']; | ||
return Text(name); | ||
} else { | ||
return Text("Loading"); | ||
} | ||
}, | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import 'package:diary/AuthPage.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class RegisterPage extends StatefulWidget { | ||
const RegisterPage({Key? key}) : super(key: key); | ||
@override | ||
_RegisterPage createState() => _RegisterPage(); | ||
} | ||
|
||
class _RegisterPage extends State<RegisterPage> { | ||
final TextEditingController _nameController = TextEditingController(); | ||
final TextEditingController _emailController = TextEditingController(); | ||
final TextEditingController _passwordController = TextEditingController(); | ||
final TextEditingController _confirmController = TextEditingController(); | ||
String _nameError = ""; | ||
String _emailError = ""; | ||
String _passwordError = ""; | ||
String _confirmError = ""; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
debugShowCheckedModeBanner: false, | ||
title: "Dairy", | ||
home: Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Login'), | ||
), | ||
body: Center( | ||
child: Column( | ||
children: [ | ||
TextField( | ||
controller: _nameController, | ||
onChanged: (v) { | ||
setState(() { | ||
_nameError = ""; | ||
}); | ||
}, | ||
keyboardType: TextInputType.name, | ||
decoration: InputDecoration( | ||
labelText: "Name", | ||
errorText: _nameError == "" ? null : _nameError, | ||
), | ||
), | ||
TextField( | ||
controller: _emailController, | ||
onChanged: (v) { | ||
setState(() { | ||
_emailError = ""; | ||
}); | ||
}, | ||
keyboardType: TextInputType.emailAddress, | ||
decoration: InputDecoration( | ||
labelText: "Email", | ||
errorText: _emailError == "" ? null : _emailError, | ||
), | ||
), | ||
TextField( | ||
controller: _passwordController, | ||
onChanged: (v) { | ||
setState(() { | ||
_passwordError = ""; | ||
}); | ||
}, | ||
obscureText: true, | ||
enableSuggestions: false, | ||
autocorrect: false, | ||
decoration: InputDecoration( | ||
labelText: "Password", | ||
errorText: _passwordError == "" ? null : _passwordError, | ||
), | ||
), | ||
TextField( | ||
controller: _confirmController, | ||
onChanged: (v) { | ||
setState(() { | ||
_confirmError = ""; | ||
}); | ||
}, | ||
obscureText: true, | ||
enableSuggestions: false, | ||
autocorrect: false, | ||
decoration: InputDecoration( | ||
labelText: "Confirm Password", | ||
errorText: _confirmError == "" ? null : _confirmError, | ||
), | ||
), | ||
Builder( | ||
builder: (context) { | ||
return TextButton( | ||
onPressed: () => login(context), child: Text("Have an account? Login")); | ||
} | ||
), | ||
ElevatedButton(onPressed: submit, child: Text("SUBMIT")) | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} | ||
|
||
void login(BuildContext context) { | ||
Navigator.of(context).push(MaterialPageRoute(builder: (context) => AuthPage())); | ||
} | ||
|
||
Future<void> submit() async { | ||
String name = _nameController.text; | ||
String email = _emailController.text; | ||
String password = _passwordController.text; | ||
String confirm = _confirmController.text; | ||
if (name.isEmpty) { | ||
setState(() { | ||
_nameError = "Name is required."; | ||
}); | ||
return; | ||
} | ||
if (!RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(email)) { | ||
setState(() { | ||
_emailError = "Invalid email."; | ||
}); | ||
return; | ||
} | ||
if (password != confirm) { | ||
setState(() { | ||
_confirmError = "Passwords don't match."; | ||
}); | ||
return; | ||
} | ||
try { | ||
UserCredential userCredential = await FirebaseAuth | ||
.instance | ||
.createUserWithEmailAndPassword(email: email, password: password); | ||
User firebaseUser = FirebaseAuth.instance.currentUser!; | ||
CollectionReference usersCollection = FirebaseFirestore.instance.collection("users"); | ||
usersCollection.doc(firebaseUser.uid).set({ | ||
'name': name | ||
}); | ||
} on FirebaseAuthException catch (e) { | ||
if (e.code == "weak-password") { | ||
setState(() { | ||
_passwordError = "Password too weak."; | ||
}); | ||
} else if (e.code == "email-already-in-use") { | ||
setState(() { | ||
_emailError = "Email is already in use, try logging in?"; | ||
}); | ||
} | ||
} catch (e) { | ||
print(e); | ||
} | ||
} | ||
|
||
} |