Skip to content
9 changes: 9 additions & 0 deletions auth/src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,14 @@ void PhoneAuthProvider::Listener::OnCodeAutoRetrievalTimeOut(
///
User::User() { auth_data_ = nullptr; }

bool User::operator==(const User& user) const {
// This is based on the assumption that there is only one user before
// Auth rewrite.
// TODO(AuthRewrite): Must change during Auth Rewrite
return is_valid() == user.is_valid();
}

bool User::operator!=(const User& user) const { return !(*this == user); }

} // namespace auth
} // namespace firebase
6 changes: 6 additions & 0 deletions auth/src/include/firebase/auth/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ class User : public UserInfoInterface {
/// Assignment operator.
User& operator=(const User&);

/// Equality operator.
bool operator==(const User&) const;

/// Inequality operator.
bool operator!=(const User&) const;
Copy link
Contributor

@jonsimantov jonsimantov May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


~User();

/// Returns whether this User object represents a valid user. Could be false
Expand Down
50 changes: 50 additions & 0 deletions auth/tests/user_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,5 +521,55 @@ TEST_F(UserTest, TestGetter) {
EXPECT_FALSE(firebase_user_->uid().empty());
EXPECT_TRUE(firebase_user_->photo_url().empty());
}

TEST_F(UserTest, TestComparisonOperator) {
{
// Two invalid users
User user_invalid1;
User user_invalid2;
EXPECT_EQ(user_invalid1, user_invalid2);
}

{
// A invalid user
User user_invalid;
EXPECT_NE(&firebase_user_, user_invalid);
}

{
// Copied valid user
User user_copy(&firebase_user_);
EXPECT_EQ(&firebase_user_, user_copy);
}

{
// Copied invalid user
User user_invalid;
User user_invalid_copy(user_invalid);
EXPECT_EQ(user_invalid, user_invalid_copy);
}

{
// Two copied of current_user()
User user1 = firebase_auth_->current_user();
User user2 = firebase_auth_->current_user();
EXPECT_EQ(user1, user2);
}

{
// Two copied of current_user_DEPRECATED()
User* user1 = firebase_auth_->current_user_DEPRECATED();
User* user2 = firebase_auth_->current_user_DEPRECATED();
EXPECT_EQ(&user1, &user2);
}

{
// User from deprecated API and new API
User* user_deprecated = firebase_auth_->current_user_DEPRECATED();
User user_new = firebase_auth_->current_user();
EXPECT_EQ(user_deprecated, user_new);
}
}

} // namespace auth
} // namespace firebase