'Firebase Login as other user

The purpose is to let the admin login as a normal user and be able to see the same things that user sees.

Is there any way to achieve that in Firebase?



Solution 1:[1]

Not without either knowing the credentials of that user or building something custom for it.

The first thing that comes to mind for a custom solution would be to have a server that mints custom auth tokens where the auth.uid property is set to the uid of the impersonated user.

Solution 2:[2]

This is what I did:

server side (cloud functions)

const serviceAccount = require('./serviceAccount');

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

exports.impersonate = functions.https.onRequest(async (req, res) => {
    cors(req, res, async () => {
        try {
            const userId = 'aMYQ6ozkaHUsffff3gIyilg5wk1';
            const token = await admin.auth().createCustomToken(userId);
            return res.status(200).send({ data: { token } });
        }
        catch (error) {
            console.error(error);
            return res.status(400).send({ data: {} })
        }
    })
});

client side:

async function impersonate() {
    const { token } = await api.post('impersonate');
    await auth().signInWithCustomToken(auth, token);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 amiregelz