Home > Back-end >  Firebase Auth Interface UserMetadata
Firebase Auth Interface UserMetadata

Time:10-03

I'm learning how to access UserMetadata in firebase Auth through this documentation (https://firebase.google.com/docs/reference/js/auth.usermetadata)

I'm trying to get UserMetadata.lastSignInTime but I can't seem to console.log this code. Please have a look at my code.

I greatly appreciate your help. Thank you have a nice day :)

import React, { useState, useEffect } from 'react'
import { initializeApp } from "firebase/app"
import { getAuth, UserMetadata } from '@firebase/auth'
import { getFirestore, doc, getDoc} from "firebase/firestore"
import { useAuth } from "../contexts/AuthContext"
import { useHistory } from 'react-router-dom'


const Trainings = () => {
    const firebaseConfig = {
        //some configurations      
    
    }
    
    const app = initializeApp(firebaseConfig)
    
    const db = getFirestore(app);

    const auth = getAuth(app);

    console.log(UserMetadata.lastSignInDate);

  //some code below

Then it throws in an error that says:

"Attempted import error: 'UserMetadata' is not exported from '@firebase/auth'."

CodePudding user response:

This import is working on Firebase v9.1.1.

import { UserMetadata } from "firebase/auth"

Also that's an interface and not an object containing user's metadata. To get user's metadata try this:

const auth = getAuth(app)
const user = auth.currentUser

const userMetadata = user?.metadata

You don't need to explicitly import UserMetadata and use it like this:

const userMetadata: UserMetadata = user?.metadata
  • Related