javascript – Google authentication 2 steps: understanding and implementation

I want to implement Google 2 step authentication with php or javascript on my website

I do not know how it works and I did a lot of searches on the Internet, but I can not understand how it works.

I found a couple of php libraries of people on the Internet and, by copying and pasting, and changing a couple of data, I was able to scan the bar code qr and get the code in the Google authenticator on my phone, but that was it, only works if the user scans the barcode, I could not generate new code in the application

How can I implement it on my website in a way that some websites do? If the user is new, then the user must scan the barcode and if it is an existing user, the user only needs to obtain the code from the Google Authenticator application.

And how can I find the Google library for this? I would like to do it myself without any library.