a socket.io middleware firebaseAdmin.verifySessionCookie
yarn add socket.io-middleware-firebase-admin
import { createServer } from "http";
import createIoServer from "socket.io";
import createIoClient from "socket.io-client";
import createIoMiddlewareFirebaseAdmin from "socket.io-middleware-firebase-admin";
import credential from "./.credential";
const server = createServer();
const io = createIoServer(server);
const app = firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(credential)
});
const ioMiddleware = createIoMiddlewareFirebaseAdmin(app);
io.use(ioMiddleware);
server.listen(() => {
const { port } = server.address();
const client = createIoClient(`http://localhost:${port}`, {
query: {
session: ""//await firebaseAdmin.auth().createSessionCookie(idToken)
}
});
client.on("connect", error => {
const decodedClaims = ioMiddleware.getCache(client);
console.log(decodedClaims);
});
});
-
options.deny
= true: boolean Deny connections of users who couldn't authenticate. -
options.cache
= true: boolean Cache claim of authenticated user.
Returns the claim of the authenticated user.
console.log(ioMiddleware.getCache(client));
// { iss:
// 'https://session.firebase.google.com/socketio-middleware-demo1',
// aud: 'socketio-middleware-demo1',
// auth_time: 1534417139,
// user_id: 'CUCPCuD50xgKeEfVggJ1fYQYNOj1',
// sub: 'CUCPCuD50xgKeEfVggJ1fYQYNOj1',
// iat: 1534417140,
// exp: 1534417440,
// email: '[email protected]',
// email_verified: false,
// firebase:
// { identities: { email: [Array] }, sign_in_provider: 'custom' }
MIT