Skip to content

Commit

Permalink
update display name of authProvider.firebaseUser
Browse files Browse the repository at this point in the history
Before, after signing in the name would be the default (null).
  • Loading branch information
IoanaAlexandru committed Mar 18, 2020
1 parent d46bb11 commit 0ef36a6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
25 changes: 14 additions & 11 deletions lib/authentication/service/auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ extension DatabaseUser on User {
}

class AuthProvider with ChangeNotifier {
FirebaseUser user;
FirebaseUser firebaseUser; // TODO: Make this private
StreamSubscription userAuthSub;

AuthProvider() {
userAuthSub = FirebaseAuth.instance.onAuthStateChanged.listen((newUser) {
print('AuthProvider - FirebaseAuth - onAuthStateChanged - $newUser');
user = newUser;
firebaseUser = newUser;
notifyListeners();
}, onError: (e) {
print('AuthProvider - FirebaseAuth - onAuthStateChanged - $e');
Expand Down Expand Up @@ -81,9 +81,9 @@ class AuthProvider with ChangeNotifier {
}

bool get isAnonymous {
assert(user != null);
assert(firebaseUser != null);
bool isAnonymousUser = true;
for (UserInfo info in user.providerData) {
for (UserInfo info in firebaseUser.providerData) {
if (info.providerId == 'facebook.com' ||
info.providerId == 'google.com' ||
info.providerId == 'password') {
Expand All @@ -95,22 +95,22 @@ class AuthProvider with ChangeNotifier {
}

bool get isAuthenticated {
return user != null;
return firebaseUser != null;
}

Future<bool> get isAuthenticatedAsync async {
if (user == null) {
user = await FirebaseAuth.instance.currentUser();
if (firebaseUser == null) {
firebaseUser = await FirebaseAuth.instance.currentUser();
}
return user != null;
return firebaseUser != null;
}

Future<User> getCurrentUser() async {
if (isAnonymous) {
return null;
}
DocumentSnapshot snapshot =
await Firestore.instance.collection('users').document(user.uid).get();
await Firestore.instance.collection('users').document(firebaseUser.uid).get();
return DatabaseUser.fromSnap(snapshot);
}

Expand Down Expand Up @@ -160,7 +160,7 @@ class AuthProvider with ChangeNotifier {
Future<void> signOut(BuildContext context) async {
if (isAnonymous) {
try {
user.delete();
firebaseUser.delete();
} catch (e) {
_errorHandler(e, null);
}
Expand Down Expand Up @@ -270,11 +270,14 @@ class AuthProvider with ChangeNotifier {
var userUpdateInfo = UserUpdateInfo();
userUpdateInfo.displayName = firstName + ' ' + lastName;
await res.user.updateProfile(userUpdateInfo);
Navigator.pop(context, true);
} catch (e) {
_errorHandler(e, context);
}

// Update user with updated info
firebaseUser = await FirebaseAuth.instance.currentUser();
notifyListeners();

// Create document in 'users'
var user = User(
uid: res.user.uid,
Expand Down
1 change: 0 additions & 1 deletion lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/pages/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class HomePage extends StatelessWidget {
child: Text(
!authProvider.isAuthenticated || authProvider.isAnonymous
? S.of(context).messageWelcomeSimple
: S.of(context)
.messageWelcomeName(authProvider.user.displayName))));
: S.of(context).messageWelcomeName(
authProvider.firebaseUser.displayName))));
}
}
2 changes: 1 addition & 1 deletion lib/pages/profile/profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ProfilePage extends StatelessWidget {
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(authProvider.user?.displayName ?? 'Anonymous'),
child: Text(authProvider.firebaseUser?.displayName ?? 'Anonymous'),
),
Expanded(
flex: 4,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description: A mobile application for students at ACS UPB.
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 0.3.8+1
version: 0.3.9+1

environment:
sdk: ">=2.6.0 <3.0.0"
Expand Down

0 comments on commit 0ef36a6

Please sign in to comment.