Reputation: 539
I'm trying to create a simple banking app with flutter.I'm creating the user login information myself and trying to auth them with firebase. I already coded the authentication part of the app.Here is the code:
import 'package:banking_app_firebase/screens/customers_info.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';
import '../constants.dart';
class LoginScreen extends StatefulWidget {
static const String id = 'login_screen';
const LoginScreen({Key? key}) : super(key: key);
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _auth = FirebaseAuth.instance;
bool showSpinner = false;
String? email;
String? password;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: ModalProgressHUD(
inAsyncCall: showSpinner,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Flexible(
child: Hero(
tag: 'logo',
child: Container(
height: 100,
child: Image.network(
"https://image.flaticon.com/icons/png/512/662/662622.png",
),
),
),
),
SizedBox(height: 20),
TextField(
keyboardType: TextInputType.emailAddress,
textAlign: TextAlign.center,
onChanged: (value) {
email = value;
},
decoration:
kTextFieldDecoration.copyWith(hintText: 'Enter your email'),
),
SizedBox(
height: 20,
),
TextField(
obscureText: true,
textAlign: TextAlign.center,
onChanged: (value) {
password = value;
},
decoration: kTextFieldDecoration.copyWith(
hintText: "Enter your password"),
),
SizedBox(
height: 24,
),
Padding(
padding: EdgeInsets.symmetric(vertical: 16),
child: Material(
elevation: 5,
color: Colors.white,
borderRadius: BorderRadius.circular(30),
child: MaterialButton(
onPressed: () async {
setState(() {
showSpinner = true;
});
try {
final user = await _auth.signInWithEmailAndPassword(
email: email!, password: password!);
if (user != null) {
Navigator.pushNamed(context, CustomerScreen.id);
}
setState(() {
showSpinner = true;
});
} catch (e) {
print(e);
}
},
height: 42,
child: Text(
"Login",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
),
),
],
),
),
),
);
;
}
}
Sample Users:
However, I don't know how to sync these information with my database so I can reach the data in the database. Here is my example database:
How can I get the informations about customers like name,surname,balance after auth is successful? I mean, how can I tell the Firebase, the auth email and password same as firestore database?
Upvotes: 0
Views: 1292
Reputation: 83181
How can I get the information about customers like name, surname, balance after auth is successful?
I understand that you want to link one document of the customers
collection to each user. The classical solution to do so is to use the user uid
as the Firestore document ID.
As soon as your user is created, you can get its uid
and create the Firestore document, like, for example:
final user = await _auth.signInWithEmailAndPassword(
email: email!, password: password!
);
if (user != null) {
await addCustomer(user.uid, email, password);
Navigator.pushNamed(context, CustomerScreen.id);
}
...
CollectionReference customers = FirebaseFirestore.instance.collection('customers');
Future<void> addCustomer(userID, email, password) {
return customers
.doc(userID)
.set({
...
})
.then((value) => print("Customer added"))
.catchError((error) => print("Failed to add customer: $error"));
}
I understand from your comment below that you want to query the customers
collection to find the user which has a specific email and password.
This is possible with a query, however I would recommend not using the password value for executing queries. This is a sensitive data that you should avoid using as a query parameter.
Actually you can very well query just by email: since you cannot create two Firebase users with the same email, you are sure that your query will return a unique result (if you didn't create some duplicate documents in the customers
collection...).
Upvotes: 3