Firebase JS - authenticate user in all sub domains

Viewed 155

My idea is to have a modal login in all my apps app1.myapp.com, app2.myapp.com.

If I log on app1.myapp.com, i'm not authenticated in app2.myapp.com.

I'm trying to use signInWithCustomToken().

There are 3 ways to auth:

  1. login with password & email

    firebase.auth().signInWithEmailAndPassword(email, password).then(function (result) { 
    //logged in 
    })
    
  2. Sign in

    firebase.auth().createUserWithEmailAndPassword(email,password).then(function (result) {
    //logged in 
    })
    
  3. Facebook login

    firebase.auth().signInWithRedirect(new firebase.auth.FacebookAuthProvider())
    

With onAuthStateChanged i check if user is really logged

firebase.auth().onAuthStateChanged(function (user) {
  if (typeof user == 'object' && typeof user.uid != 'string') {
    console.log('no user');
    return false;
  }
  // set Auth Token
});

So, I do not understand how create custom token: I try to use getIdTokenResult inside event onAuthStateChanged like that

firebase.auth().currentUser.getIdTokenResult()
.then((idTokenResult) => {
    console.log(idTokenResult.token);
    Cookies.set('mpf_tkn', idTokenResult.token, { domain: mycookiedomain });
})
.catch((error) => {
    console.log(error);
});

and on ready document I do this

var mpf_tkn = Cookies.get('mpf_tkn', { domain: mycookiedomain });
if (mpf_tkn) {
    console.log('signin with custom token');
    firebase.auth().signInWithCustomToken(mpf_tkn);
}

but it returns error!

How can I authenticate a user in all app subdomain?

0 Answers
Related