diff --git a/README.md b/README.md index 3fdb5ed..b007e59 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Socket.io Middleware firebase-admin

-a minimal BDD interface / reporter for [ESM Modules](https://nodejs.org/api/esm.html#esm_enabling) +a socket.io middleware [firebaseAdmin.verifySessionCookie](https://firebase.google.com/docs/auth/admin/manage-cookies) Installation --- @@ -20,7 +20,66 @@ yarn add socket.io-middleware-firebase-admin API --- -TODO + +## createIoMiddlewareFirebaseAdmin({ credential }, (options = {})): middleware + +[initialize "Firebase Admin SDK" using `credential`](https://firebase.google.com/docs/admin/setup) + +```js +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 ioMiddleware = createIoMiddlewareFirebaseAdmin({ credential }); + +io.use(ioMiddleware); + +server.listen(() => { + const { port } = server.address(); + const client = createIoClient(`http://localhost:${port}`, { + extraHeaders: { + Cookie: "session=" //+await firebaseAdmin.auth().createSessionCookie(idToken) + } + }); + client.on("connect", error => { + const decodedClaims = ioMiddleware.getCache(client.id); + 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(clientId): decodedClaims + +Returns the claim of the authenticated user. + +```js +console.log(ioMiddleware.getCache(client.id)); +// { 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: 'i59naga@icloud.com', +// email_verified: false, +// firebase: +// { identities: { email: [Array] }, sign_in_provider: 'custom' } +``` License --- diff --git a/package.json b/package.json index 8e6fec2..e893b04 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "socket.io-middleware-firebase-admin", "version": "0.0.0", - "description": "", + "description": "a socket.io middleware firebaseAdmin.verifySessionCookie", "scripts": { "start": "abby format, test --watch \"*.mjs\"", "test": "node --experimental-modules test.mjs", @@ -9,6 +9,10 @@ "build": "babel index.mjs --out-file index.js", "postversion": "git push --follow-tags && conventional-github-releaser -p atom" }, + "dependencies": { + "cookie": "^0.3.1", + "firebase-admin": "^6.0.0" + }, "devDependencies": { "@babel/cli": "^7.0.0-rc.1", "@babel/core": "^7.0.0-rc.1", @@ -24,10 +28,10 @@ "socket.io": "^2.1.1", "socket.io-client": "^2.1.1" }, - "dependencies": { - "cookie": "^0.3.1", - "firebase-admin": "^6.0.0" - }, + "keywords": [ + "socket.io-middleware", + "firebase-admin" + ], "repository": { "type": "git", "url": "git+https://github.com/59naga/socket.io-middleware-firebase-admin.git"