How to properly use `eraseCredentials` in my User entity for default authentication in symfony app?

I am building a basic Symfony 7.1 application.

I created my own User entity, implementing both SymfonyComponentSecurityCoreUserUserInterface and PasswordAuthenticatedUserInterface.

I am using the user authentication process as provided by Symfony and I don’t want to do anything special but rather play it by the book.

The UserInterface mandates the method eraseCredentials and it says in its docs:

Removes sensitive data from the user. This is important if, at any
given point, sensitive information like the plain-text password is
stored on this object.

My User entity stores its password as a hashed through Symfony’s password hasher as defined in the

security.yaml:

    password_hashers:
        AppEntityUser:
            algorithm: bcrypt
            cost: 12

I was still under the impression that I should implement the eraseCredentials, and at first, I did so like this:

#AppEntityUser
public function eraseCredentials(): void
{
        $this->password = null;
}

This lead to weird behavior, as then my login would not work, and I was stuck in an infinite loop to my login action.

The authentication pretty much worked, during debug I even saw that I am redirected to my proper default_target_path, yet as soon as it tried to render that page, these messages popped in my dev.log:

[2024-09-09T11:03:26.901713+00:00] security.DEBUG: Cannot refresh token because user has changed. {"username":"MY_USER","provider":"Symfony\Bridge\Doctrine\Security\User\EntityUserProvider"} []
[2024-09-09T11:03:26.902036+00:00] security.DEBUG: Token was deauthenticated after trying to refresh it. [] []

I figured I could just leave the eraseCredentials method empty:

public function eraseCredentials(): void
{
}

And now I can log in, and the app seemingly works.

Yet, I am wondering if I am missing something here.

What is the intended way to handle the password management here?

As shown in the docs:


    /**
     * @see UserInterface
     */
    public function eraseCredentials(): void
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

Note that in this example, that this is only meant to erase plainPassword, not password (the PLAIN TEXT password, not the HASHED password).

If you are not storing the plain text password anywhere in your user entity, you can leave eraseCredentials() unimplemented (as you’ve already discovered):

    /**
     * @see UserInterface
     */
    public function eraseCredentials(): void
    {}

Persisting the plain text password has been considered a bad practice for quite some time, but there could be some circumstances and designs where one may need to store the plain text password in the entity (in memory, without actually persisting it).

Maybe for passing it along to other service in charge of actually hashing the password, etc. In a case like this, eraseCredentials() would be responsible for making sure that the sensitive values would be cleared before, for example, normalizing/serializing the entity values.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật