Updating the profile and login status

The Firebase user object contains more than phone numbers, it contains a set of information for another method of login, such as email, for example, and also contains properties that help to define the user's profile, such as a display name and photo URL. Here, in the last step of the login process, we can save the user profile with its displayName so that other users can identify easily. This is done in the _saveProfile() method as shown here:

// part of LoginPageState class
void _saveProfile() async {

setState(() {
_showProgress = true;
});

final user = await FirebaseAuth.instance.currentUser();

final updateInfo = UserUpdateInfo();
updateInfo.displayName = _displayName;

await user.updateProfile(updateInfo);

// ... the last part is explained below
}

The currentUser() method is useful for any action related to the logged in user. In this case, we get it and update the requested info (the display name, for now). UserUpdateInfo is a helper class to store the update data; in the next section, we will be using one more property of it to store the user profile picture URL.

As we know the user is logged in, we can redirect to the Favors page using the well-known Navigator class as follows:

// final part of _saveProfile() LoginPage    
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => FavorsPage(),
),
);

This screen is the initial screen of our app. However, we should not ask the user to fill all the information every time. Before anything, we must check whether the user is already logged in, and if they are, simply redirect as we did before. We can do that by using the FirebaseAuth.instance.currentUser() method again. A great place to check this is the initState() method of the LoginPageState class:

// part of login_page.dart
class LoginPageState extends State<LoginPage> {
...
@override
void initState() {
super.initState();

FirebaseAuth.instance.currentUser().then((user) {
if (user != null) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => FavorsPage(),
),
);
}
});
}
...
}

As you can see, if the current Firebase user is not null, we know that we can redirect the navigation to the next screen just like before.

What would be good user feedback if the current user is null? Have a think and find out.

That's it for phone authentication; in the next section, we are going to store our favors on the Cloud Firestore backend.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.81.240