forked from 59naga/socket.io-middleware-firebase-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
45 lines (41 loc) · 1.19 KB
/
index.mjs
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
export default (app, options = {}) => {
const auth = app.auth();
const opts = Object.assign({ deny: true, cache: true }, options);
const cache = {};
const middleware = async (client, next) => {
const { session = "" } = client.handshake.query;
if (!session && !opts.deny) {
return next();
}
try {
const claims = await auth.verifySessionCookie(session, true);
if (opts.cache) {
middleware.setCache(client, claims);
client.on("disconnect", () => {
middleware.deleteCache(client);
});
}
} catch (error) {
if (opts.deny) {
return next(error);
}
}
next();
};
middleware.setCache = (client, value) => {
return (cache[client.id] = value);
};
middleware.getCache = client => {
return cache[client.id];
};
middleware.deleteCache = client => {
delete cache[client.id];
};
middleware.extra = {
verifyCustomTokenUrl:
"https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=",
createCustomToken: (...args) => auth.createCustomToken(...args),
createSessionCookie: (...args) => auth.createSessionCookie(...args)
};
return middleware;
};