firebase onAuthStateChanged user should not be null

Viewed 159

server

admin.auth().createCustomToken(uuid)
.then((customToken) => {
  admin.auth().createUser({
      email: 'user@example.com',
      emailVerified: false,
      phoneNumber: '+11234567890',
      password: 'secretPassword',
      displayName: 'John Doe',
      photoURL: 'http://www.example.com/12345678/photo.png',
      disabled: false
    })
    .then((userRecord) => {
      console.log('Successfully created new user:', userRecord.uid);
      res.redirect(`http://localhost:3000?token=${customToken}`);  
    })
    .catch((error) => {
      console.log('Error creating new user:', error);
    });
})

client

firebase.auth().signInWithCustomToken(token)
.then(() => {
  firebase.auth().onAuthStateChanged((user) => {
    console.log(user.email); // is null.
  });
})

I need to use admin.auth().createUser({}) on the server.

But, It is not working.

I don't know how to use it.

I want to connect to the another sns service.

1 Answers

I think this will work if you change your client to:

firebase.auth().signInWithCustomToken(token)
.then((user) => {
   console.log(user.email);
})

You can also have this separately

firebase.auth().onAuthStateChanged((user) => {
    console.log(user.email); 
});

Then you should basically have the user email printed twice when logging in if this helps.

Related