This is the code im using currently, I have the role based system working on my register page but it does not work when i try and log in
I’ve tried using the catch function to try and get user data and then put it in a condition but that did not work. Im expecting it to navigate to certain pages depending on the role of the user.
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:modernlogintute/components/my_button.dart';
import 'package:modernlogintute/components/my_textfield.dart';
import 'package:modernlogintute/components/square_tile.dart';
import 'package:modernlogintute/pages/admin_page.dart';
import 'package:modernlogintute/pages/bookstore.dart';
import 'package:modernlogintute/pages/register_page.dart';
import 'package:modernlogintute/services/AuthService.dart';
import 'forgot_pw_page.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void navigateBasedOnRole(BuildContext context, Role selectedRole) {
if (selectedRole == Role.admin) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => AdminPage()),
(Route<dynamic> route) => false,
);
} else {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => Bookstore()),
(Route<dynamic> route) => false,
);
}
}
class LoginPage extends StatefulWidget {
final Function()? onTap;
LoginPage({super.key, required this.onTap});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
// text editing controllers
final emailController = TextEditingController();
final passwordController = TextEditingController();
// sign user in method
void signUserIn() async {
//show loading circle
showDialog(context: context, builder: (context) {
return const Center(
child: CircularProgressIndicator(),
);
},
);
try {await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text,
password: passwordController.text
);
//pop the loading circle
Navigator.pop(context);
} on FirebaseAuthException catch (e) {
//pop the loading circle
Navigator.pop(context);
if(e.code == 'user-not-found'){
print("No user found");
//show error to user
wrongEmailMessage();
}
else if (e.code == 'wrong-password'){
print("Wrong Password");
//show error to user
wrongPasswordMessage();
}
}
}
// wrong email message pop up
void wrongEmailMessage(){
showDialog(context: context, builder: (context) {
return const AlertDialog(
title: Text('Incorrect Email'),
);
},
);
}
// wrong Password message pop up
void wrongPasswordMessage(){
showDialog(context: context, builder: (context) {
return const AlertDialog(
title: Text('Incorrect Password'),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[300],
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 50),
// logo
const Icon(
Icons.lock,
size: 100,
),
const SizedBox(height: 50),
// welcome back, you've been missed!
Text(
'Welcome back you've been missed!',
style: TextStyle(
color: Colors.grey[700],
fontSize: 16,
),
),
const SizedBox(height: 25),
// email textfield
MyTextField(
controller: emailController,
hintText: 'Email',
obscureText: false,
),
const SizedBox(height: 10),
// password textfield
MyTextField(
controller: passwordController,
hintText: 'Password',
obscureText: true,
),
const SizedBox(height: 10),
// password?
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context)
{
return ForgotPasswordPage();
},
),
);
},
child: Text(
'Forgot Password?',
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
const SizedBox(height: 25),
// sign in button
MyButton(
text: 'Sign In',
onTap: signUserIn,
),
const SizedBox(height: 50),
//or continue with
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Row(children: [
Expanded(child: Divider(
thickness: 0.5,
color: Colors.grey[400],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Text(
'Or continue with',
style: TextStyle(color: Colors.grey[700]),
),
),
Expanded(
child: Divider
(
thickness: 0.5,
color: Colors.grey[400],
),
),
],
),
),
const SizedBox(height: 50,),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SquareTile(
onTap: () => AuthService().signInWithGoogle(),
imagePath: 'lib/images/google.png', ),
SizedBox(width: 25,),
SquareTile(
onTap: () {
},
imagePath: 'lib/images/apple.png',),
],
),
const SizedBox(height: 50,),
// not a member? register now
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Not a member?',
style: TextStyle(color: Colors.grey[700]),
),
const SizedBox(width: 4),
GestureDetector(
onTap: widget.onTap,
child: const Text(
'Register now',
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
),
],
)
],
),
),
),
),
);
}
}
2
Answers
Simple just save the userCredentails that u get from function (FirebaseAuth.instance.signInWithEmailAndPassword)and then call your function(navigateBasedOnRole)
like:
UserCredentail userCredentail = await FirebaseAuth.instance.signInWithEmailAndPassword( email: emailController.text, password: passwordController.text); navigateBasedOnRole(context, userCredentail.user?.role);
hopefully that helps
Your problem is obvious! You have created this method:
navigateBasedOnRole
, but you have not used it anywhere in your code.Here is what you should do: