This repository has been archived by the owner on Oct 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
impersonate.ts
52 lines (48 loc) · 1.78 KB
/
impersonate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// TODO: Fix this - Doesn't work
// Attribution: amiregelz
// https://stackoverflow.com/questions/36759627/firebase-login-as-other-user/71808501#71808501
/*exports.impersonate = functions.https.onCall((data, context) => {
if (context.app == undefined) {
throw new functions.https.HttpsError(
'failed-precondition',
'The function must be called from an App Check verified app.'
);
}
// Check if request is made by an admin
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
if (context.auth.token.role !== 1000) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called by an admin.');
}
const remoteConfig = admin.remoteConfig();
// Get whitelist array as string from remote config
return remoteConfig.getTemplate().then((template) => {
// @ts-ignore
const whitelist: string = template.parameters.adminWhitelist.defaultValue?.value;
if (context.auth?.uid) {
return admin
.auth()
.getUser(context.auth.uid)
.then((userRecord) => {
if (userRecord.email && whitelist.includes(userRecord.email)) {
const userId = data.uid;
return admin
.auth()
.createCustomToken(userId)
.then((customToken) => {
return {
token: customToken,
};
});
} else {
throw new functions.https.HttpsError('failed-precondition', 'You are not whitelisted.');
}
})
.catch((error) => {
return { message: `${error}` };
});
}
throw new functions.https.HttpsError('failed-precondition', 'Your uid was not provided.');
});
});*/