Firebase email verification Flutter - firebase

I know this question has been asked a lot and I have spent a lot of time reading and trying to implement the answers. So I am trying to get the response from isEmailVerified from Firebase Auth to work and it does work but right now it always returns false unless I refresh the app or close it and reopen it. which is obviously a bad user experience. How do I get the response to update without having to close the app.
here is the relevant pieces of code.
Future<bool> isEmailVerified() async {
FirebaseUser user = await _auth.currentUser();
if (user == null) {
return false;
} else {
await user.reload();
user = await _auth.currentUser();
return user.isEmailVerified;
}
}
main.dart
child: Consumer<Auth>(
builder: (_, auth, __) => MaterialApp(
theme: Provider.of<ThemeNotifier>(context).getTheme(),
home: FutureBuilder(
future: Future.wait([auth.isEmailVerified(), auth.tryAutoLogin()]),
builder: (BuildContext ctx, AsyncSnapshot authResultSnapshot) =>
authResultSnapshot.connectionState == ConnectionState.done
? authResultSnapshot.data[1]
? authResultSnapshot.data[0]
? HearingsScreen()
: SplashScreen(
emailVerified: true,
)
: LoginScreen()
: SplashScreen(),
),
It is not returning true until I restart the app
Things I have tried besides this:
1) await user.getIdToken(refresh: true);
2) sign user out then back in
3) firebase_user_stream package
Any help is appreciated.

I have implemented the same scenario in a splash screen with below code, you can change it as per your requirement. :
//To check is User is logged in
Future<bool> isLoggedIn() async {
FirebaseUser user = await _fireBaseAuth.currentUser();
if (user == null) {
return false;
}
return user.isEmailVerified;
}
and
countDownTime() async {
return Timer(
Duration(seconds: splashDuration),
() async {
if (await userAuth.isLoggedIn()) {
Navigator.pushReplacement(
context,
ScaleRoute(
widget: HomeScreen(),),
);
}
} else {
Navigator.pushReplacement(
context,
ScaleRoute(
widget: LoginScreen(),),
);
}
},
);
}
and
#override
void initState() {
super.initState();
countDownTime();
}
Update
One needs to implement isEmailVerified in initState() function periodically which can be the ideal approach to execute the verification with firebase.
bool _isUserEmailVerified;
Timer _timer;
#override
void initState() {
super.initState();
// ... any code here ...
Future(() async {
_timer = Timer.periodic(Duration(seconds: 10), (timer) async {
await FirebaseAuth.instance.currentUser()..reload();
var user = await FirebaseAuth.instance.currentUser();
if (user.isEmailVerified) {
setState((){
_isUserEmailVerified = user.isEmailVerified;
});
timer.cancel();
}
});
});
}
#override
void dispose() {
super.dispose();
if (_timer != null) {
_timer.cancel();
}
}

Related

How to trigger dispose from another page

I have two pages, Page_1 which uses didChangeAppLifecycleState which looks out for when the user closes the app. While the user is on the app, a timer increases, as soon as the user closes the app, the timer stops.
Page_2 has a logout button, which uses firebase signOut() function.
If I were to use the logout function in Page_1 and sign in with another account, the timer would start new for the second user, but If I were to logout from my Page_2 and sign in with another user, then the timer would of carried from the first user instead of starting again,
What I'm asking is how can I use WidgetsBinding.instance!.removeObserver(this); on Page_2 instead of Page_1
Page_1.dart
class _HomeScreenState extends State<HomeScreen> with WidgetsBindingObserver {
User? user = FirebaseAuth.instance.currentUser;
UserModel loggedInUser = UserModel();
final _auth = FirebaseAuth.instance;
StreamController<int> controller = StreamController();
late StreamSubscription<int> streamSubscription;
#override
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
print(user!.uid);
FirebaseFirestore.instance
.collection("users")
.doc(user!.uid)
.get()
.then((value) {
loggedInUser = UserModel.fromMap(value.data());
setState(() {});
});
app_start = DateTime.now();
}
late DateTime app_start;
late DateTime app_end;
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
print(state);
// if user clicks logout end this funciton
// Checks if app is active
final isBackground = state == AppLifecycleState.paused;
if (state == AppLifecycleState.inactive ||
state == AppLifecycleState.detached) return;
if (isBackground) {
FirebaseFirestore.instance
.collection("users")
.doc(user!.uid)
.get()
.then((value) {
loggedInUser = UserModel.fromMap(value.data());
if (loggedInUser.timeActive == null) {
loggedInUser.timeActive = 1;
} else {
}
app_end = DateTime.now();
final differenceInDays = app_end.difference(app_start).inSeconds;
int? test = loggedInUser.timeActive;
int? totalOnTime = differenceInDays + loggedInUser.timeActive!.toInt();
FirebaseFirestore.instance
.collection('users')
.doc(loggedInUser.uid)
.update({"timeActive": totalOnTime});
setState(() {});
});
} else {
app_start = DateTime.now();
}
}
#override
void dispose() {
// TODO: implement dispose
super.dispose();
WidgetsBinding.instance!.removeObserver(this);
}
}
Page_2.dart
#override
void dispose() {
// TODO: implement dispose
super.dispose();
WidgetsBinding.instance!.removeObserver(this);
}
ElevatedButton(
onPressed: () {
dispose()
_auth.signOut();
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) =>
LoginScreen()));
print("clicked");
},
child: Text('Log out'),
)

How to maintain Firebase Authentication after refresh with Flutter web?

I am using the authStateChanges stream from Firebase with flutter. I have two views, one for mobile and the another one for a web application. I want to redirect the user to the SignIn screen if he is not connected, logged in or authenticated. At first it works well but then when i am logged in and refresh the browser i got the SignIn screen loaded for like 1 second and then the Web screen appears again. I checked with print what's going on and from what i saw, the authStateChanges Stream is null for that 1-2 seconds(when SignIn screen appears) and then has a value when the stream receives the connected user. Is there a way to check, or wait until this authentication is done before loading the SignIn screen when it must not load it ?
My main component contains the StreamBuilder as following:
Widget build(BuildContext context) {
final firebaseAuthService = Provider.of<FirebaseAuthService>(context);
return StreamBuilder<User>(
stream: firebaseAuthService.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
User user = snapshot.data;
if (user == null) {
//first time no connection
return SignIn();
}
if (kIsWeb) {
return WebMain(user: user);
}
// load mobile version
return MobileMain();
}
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
});
}
Here you can find my FirebaseAuth wrapper class which contains the methods from firebase:
class FirebaseAuthService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
User _user;
bool get isAuthenticated {
return _user == null ? false : true;
}
User get user {
return _user;
}
Future<User> signInWithEmailAndPassword(
String userEmail, String userPassword) async {
return _user = await _firebaseAuth
.signInWithEmailAndPassword(email: userEmail, password: userPassword)
.then((userCredential) => userCredential.user);
}
Stream<User> authStateChanges() {
_user = _firebaseAuth.currentUser;
return _firebaseAuth.authStateChanges();
}
Future<void> signOut() async {
return _firebaseAuth.signOut();
}
}
While I am not sure why authStateChanges does not notify when the user sign in state is changed (usually a second later), a similar function does seem to work for your use case.
Try idTokenChanges()
FirebaseAuth.instance.idTokenChanges().listen((event) {
print("On Data: ${event}");
});
This event will return your Firebase User object. When refreshed, it might return 'null' initially, but within a second, returns your signed in User. You could potentially make the sign in page wait a couple of seconds to make sure a signed in user isn't being initialized.
EDIT:
While there may be better solutions, this is currently working for me.
final subscription = FirebaseAuth.instance.idTokenChanges().listen(null);
subscription.onData((event) async {
if(event != null) {
print("We have a user now");
isLoading = false;
print(FirebaseAuth.instance.currentUser);
subscription.cancel();
Navigator.push(
context,
MaterialPageRoute(builder: (context) => OverviewController())
);
} else {
print("No user yet..");
await Future.delayed(Duration(seconds: 2));
if(isLoading) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginController())
);
isLoading = false;
subscription.cancel();
}
}
});
For me, the below code seems to work fine. Although there is a warning in docs that says "You should not use this getter to determine the user's current state, instead use [authStateChanges], [idTokenChanges] or [userChanges] to subscribe to updates."
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Diary Book',
theme: ThemeData(
visualDensity: VisualDensity.adaptivePlatformDensity,
primarySwatch: Colors.green,
),
home: (FirebaseAuth.instance.currentUser == null)
? LoginPage()
: MainPage(),
);
}
}
I haven't encountered any issues using the above code. I Will let you know if do. If someone can comment any future errors this may have that would be great
FirebaseAuth.instance.authStateChanges().listen(
(event) {
if (event == null) {
print('----user is currently signed out');
} else {
print('----user is signed in ');
}
runApp(
const MyApp()
);
},
);

How to use sharedPreference in flutter to stay user loggedin in flutter using a setBool and GetBool

I am practicing a email authentication in flutter and almost everything is over. Now, i want to use sharedPreference to stay the user logged in. I have tried something, but i don't get result. I am using a bool type to get whether user loggedIn or not. But i am very new to this, can you help me in this? and is there anything i am missing out?
This is the sharedPreference static Class i am using
class sharedPreference {
static String sharedPreferenceUserLoggedInKey = 'userLoggedIn';
static String sharedPreferenceUserSignedUpKey = 'userSignedUp';
//saving data to sharedPreference
static Future<bool> saveUserLoggedInSharedPreference(
bool isUserLoggedIn) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setBool(sharedPreferenceUserLoggedInKey, isUserLoggedIn);
}
static Future<bool> saveUserSignedUpSharedPreference(
bool isUserSignUp) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setBool(sharedPreferenceUserSignedUpKey, isUserSignUp);
}
//getting data to sharedPreference
static Future<bool> getUserLoggedInSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.getBool(sharedPreferenceUserLoggedInKey);
}
static Future<bool> getUserSignedUpSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.getBool(sharedPreferenceUserSignedUpKey);
}
}
This is the signIn button triggering the setBool:
SignInButton:
FlatButton(
onPressed: ()
{
HelperFunction.saveUserLoggedInSharedPreference(true);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => DashBoard(email: email),
),
})
The main function
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light
.copyWith(systemNavigationBarColor: Colors.black));
runApp(
DevicePreview(
enabled: kReleaseMode,
builder: (context) => FlashChat(),
),
);
}
class FlashChat extends StatefulWidget {
#override
_FlashChatState createState() => _FlashChatState();
}
class _FlashChatState extends State<FlashChat> {
bool isUserLoggedIn;
bool isUserSignedUp;
void getLoggedInStatus() async {
await HelperFunction.getUserLoggedInSharedPreference().then((value) {
isUserLoggedIn = value;
});
}
void getSignedUpStatus() async {
await HelperFunction.getUserSignedUpSharedPreference().then((value) {
isUserSignedUp = value;
});
}
#override
void initState() {
getLoggedInStatus();
getSignedUpStatus();
super.initState();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: isUserLoggedIn == true
? DashBoard.id: WelcomeScreen.id,
routes: {
WelcomeScreen.id: (context) => WelcomeScreen(),
LoginScreen.id: (context) => LoginScreen(),
RegistrationScreen.id: (context) => RegistrationScreen(),
DashBoard.id: (context) => DashBoard(),
},
debugShowCheckedModeBanner: false,
);
});
});
when the user gets login set
prefs.setBool("isLogin", True);
and when the user get a logout in logout function put
pref.clear()
and in splash screen or at starting put this logic
SharedPreferences prefs = await SharedPreferences.getInstance();
var isLogin = prefs.getBool("isLogin");
if (isLogin)
{
//Navigate user to the required screen
}
else{
//navigate user to login screen
}

Load Firestore document first in Flutter

Thanks in advance!
Flutter & Firestore
I'm checking if the user is an admin. I have a collection 'users', within: a bool: admin: true or false. If it's true it shows the admin Screen. Else it shows the main screen.
The problem: the first 1 second I get this Screen. (see screen shot, only 1 second visible) Is there an option to use a loading indicator when the app gets the data from firestore.
I tried adding if (currentUser.admin == null) {}
But that doesn't work.
User currentUser;
//I made a separate modal document
#override
void initState() {
super.initState();
pageController = PageController(initialPage: 0);
getUser();
}
getUser() async {
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
DocumentSnapshot doc =
await Firestore.instance.collection("users").document(user.uid).get();
setState(() {
currentUser = User.fromDocument(doc);
});
print(currentUser.admin);
}
#override
Widget build(BuildContext context) {
if (currentUser.admin == true) {
return AdminScreen(
currentUser: currentUser,
);
} else {
return mainScreen();
}
}
}
Screenshot
UPDATE: tried this:
getUser() async {
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
DocumentSnapshot doc =
await Firestore.instance.collection("users").document(user.uid).get();
if (doc.exists) {
try {
setState(() {
currentUser = User.fromDocument(doc);
});
} catch (e) {
print(e);
}
}
Found it!
For anyone having the same issue.
This was the problem:
if (currentUser.admin == true)
It should be: if (currentUser == null) or if (currentUser != null)
it doesn't work with currentUser.xxxx
You can also do as follows
Firestore.instance
.collection('driverListedRides')
.getDocuments()
.then((QuerySnapshot snapshot) => {
snapshot.documents.forEach((f) {
String _variableName = f.data["YourCollectionFieldId"];
})
});

Persist user Auth Flutter Firebase

I am using Firebase Auth with google sign in Flutter. I am able to sign in however when I close the app(kill it), I have to sign up all over again. So is there a way to persist user authentication till specifically logged out by the user?
Here is my auth class
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
class Auth {
FirebaseAuth _firebaseAuth;
FirebaseUser _user;
Auth() {
this._firebaseAuth = FirebaseAuth.instance;
}
Future<bool> isLoggedIn() async {
this._user = await _firebaseAuth.currentUser();
if (this._user == null) {
return false;
}
return true;
}
Future<bool> authenticateWithGoogle() async {
final googleSignIn = GoogleSignIn();
final GoogleSignInAccount googleUser = await googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
this._user = await _firebaseAuth.signInWithGoogle(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
if (this._user == null) {
return false;
}
return true;
// do something with signed-in user
}
}
Here is my start page where the auth check is called.
import 'package:flutter/material.dart';
import 'auth.dart';
import 'login_screen.dart';
import 'chat_screen.dart';
class Splash extends StatefulWidget {
#override
_Splash createState() => _Splash();
}
class _Splash extends State<Splash> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: CircularProgressIndicator(
value: null,
),
),
);
}
#override
void initState() {
super.initState();
_handleStartScreen();
}
Future<void> _handleStartScreen() async {
Auth _auth = Auth();
if (await _auth.isLoggedIn()) {
Navigator.of(context).pushReplacementNamed("/chat");
}
Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => LoginScreen(auth: _auth,)));
}
}
I believe your problem is routing. In my apps I use FirebaseAuth and it works just as you say you wanted to, and I don't persist any login token. However, I don't know why your approach of using a getUser is not working.
Try to adjust your code to use onAuthStateChanged. EDIT: As of 2022, with Flutter 3, I noticed it worked better with userChanges instead.
Basically, on your MaterialApp, create a StreamBuilder listening to _auth.userChanges() and choose your page depending on the Auth status.
I'll copy and paste parts of my app so you can have an idea:
[...]
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<void> main() async {
FirebaseApp.configure(
name: '...',
options:
Platform.isIOS
? const FirebaseOptions(...)
: const FirebaseOptions(...),
);
[...]
runApp(new MaterialApp(
title: '...',
home: await getLandingPage(),
theme: ThemeData(...),
));
}
Future<Widget> getLandingPage() async {
return StreamBuilder<FirebaseUser>(
stream: _auth.userChanges(),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData && (!snapshot.data!.isAnonymous)) {
return HomePage();
}
return AccountLoginPage();
},
);
}
Sorry, it was my mistake. Forgot to put the push login screen in else.
Future<void> _handleStartScreen() async {
Auth _auth = Auth();
if (await _auth.isLoggedIn()) {
Navigator.of(context).pushReplacementNamed("/chat");
}
else {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => LoginScreen(auth: _auth,)));
}
}
void main() {
FirebaseAuth.instance.authStateChanges().listen((User user) {
if (user == null) {
runApp(MyApp(auth : false);
} else {
runApp(MyApp(auth : false);
}
});
}
class MyApp extends StatefulWidget {
final bool auth;
MyApp({this.auth});
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
return MaterialApp(
......
......
home: widget.auth ? MainScreen() : AuthScreen();
);
You can use shared_preferences to keep alive your session even when you kill the app.
Here is the documentation https://pub.dartlang.org/packages/shared_preferences.
Also I've heard that it's possible to use sqlite to persist the session.
Add this code. It should work fine.
FirebaseAuth auth = FirebaseAuth.instance;
auth.setPersistence(Persistence.SESSION);
You can use my code, You can use userChanges() instead of authStateChanges()
Notifies about changes to any user updates.
This is a superset of both [authStateChanges] and [idTokenChanges]. It provides events on all user changes, such as when credentials are linked, unlinked and when updates to the user profile are made. The purpose of this Stream is for listening to realtime updates to the user state (signed-in, signed-out, different user & token refresh) without manually having to call [reload] and then rehydrating changes to your application.
final Stream<User?> firebaseUserChanges = firebaseAuth.userChanges();
One more simple example:
Future<bool> isUserLoggedIn() async {
final User? user = FirebaseAuth.instance.currentUser;
return user != null;
}
class InitialScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<bool>(
future: isUserLoggedIn(),
builder: (_, snapshot) {
if (snapshot.hasData) {
if (snapshot.data ?? false) {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => UnauthScreen()));
} else {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => HomeScreen()));
}
}
return const Center(child: CircularProgressIndicator());
},
),
);
}
}
I was able to achieve it by checking the firebase instance currentUser value. if null I routed to my Signup page. If not, then I routed to my HomePage. Not sure if there is anything wrong with this implementation (its working well so far) but seems simpler than the StreamBuilder solution posted above.
home: getLandingPage(),
routes: {
(...)
}
Widget getLandingPage() {
if (_auth.currentUser == null) {
return SignupPage();
} else {
return HomePage();
}
}

Resources