Skip to content

Lopuch/socket.io-middleware-firebase-admin

 
 

Repository files navigation

Socket.io Middleware firebase-admin

Npm version Build Status

a socket.io middleware firebaseAdmin.verifySessionCookie

Installation

yarn add socket.io-middleware-firebase-admin

API

createIoMiddlewareFirebaseAdmin(app, options = {}): middleware

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

  • options.deny= true: boolean Deny connections of users who couldn't authenticate.

  • options.cache= true: boolean Cache claim of authenticated user.

ioMiddleware.getCache(client): decodedClaims

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' }

License

MIT

About

a socket.io middleware firebaseAdmin.verifySessionCookie

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%