I have used firebase to authenticate a user inside my app. However getting their metadata to show last sign in date and account creation data has been a hassle. The documentation here doesn’t help much. Is quite confusing.
I know I can import {UserMetadata} from "firebase/auth" but whatever I’ve tried after that does not work. It seems like the documentation is based on Typescript and I’ve been getting errors.
I am expeting to get the metadata in a similar format to this if possible
<div className="membership_start_date account_overview_card">
<h2 className='account_overview_card_title'>Member since</h2>
<p className='account_overview_card_detail'>{metadata}</p>
</div>
I have tried importing UserMetaData in this way:
import {UserMetadata} from "firebase/auth"
const Account = () => {
const metadata = UserMetadata()
return (
<p className='account_overview_card_detail'>{metadata.lastSignIn}</p>
)
}
export default Account
The whole code is quite long so I’ve tried to resume it to the most necessary part. What am I doing wrong?
2
Answers
First make sure you has fbAuth properly exported:
Then call at some point your desired authentication method:
Finally chek on your current user:
Full source code here. It’s written in vue but the general idea is very similar to it.
UserMetadata is part of a
user
object. Assuming you already have sign in method. Then in auth state subscription method you can access user’s data:You can even test a short version having firebase globally set up: