Skip to content

Commit

Permalink
‼️ change .getCache first argument clientId to client.
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed Dec 19, 2018
1 parent 0f1d409 commit 2b6b820
Show file tree
Hide file tree
Showing 5 changed files with 545 additions and 27 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ server.listen(() => {
}
});
client.on("connect", error => {
const decodedClaims = ioMiddleware.getCache(client.id);
const decodedClaims = ioMiddleware.getCache(client);
console.log(decodedClaims);
});
});
Expand All @@ -62,12 +62,12 @@ server.listen(() => {
* `options.cache`= true: boolean
Cache claim of authenticated user.

## ioMiddleware.getCache(clientId): decodedClaims
## ioMiddleware.getCache(client): decodedClaims

Returns the claim of the authenticated user.

```js
console.log(ioMiddleware.getCache(client.id));
console.log(ioMiddleware.getCache(client));
// { iss:
// 'https://session.firebase.google.com/socketio-middleware-demo1',
// aud: 'socketio-middleware-demo1',
Expand Down
16 changes: 8 additions & 8 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export default (app, options = {}) => {
try {
const claims = await auth.verifySessionCookie(session, true);
if (opts.cache) {
middleware.setCache(client.id, claims);
middleware.setCache(client, claims);
client.on("disconnect", () => {
middleware.deleteCache(client.id);
middleware.deleteCache(client);
});
}
} catch (error) {
Expand All @@ -24,14 +24,14 @@ export default (app, options = {}) => {
}
next();
};
middleware.setCache = (key, value) => {
return (cache[key] = value);
middleware.setCache = (client, value) => {
return (cache[client.id] = value);
};
middleware.getCache = key => {
return cache[key];
middleware.getCache = client => {
return cache[client.id];
};
middleware.deleteCache = key => {
delete cache[key];
middleware.deleteCache = client => {
delete cache[client.id];
};

middleware.extra = {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"scripts": {
"start": "abby format, test --watch \"*.mjs\"",
"test": "node --experimental-modules test.mjs",
"test": "eastern",
"format": "prettier \"*.mjs\" --write",
"build": "babel index.mjs --out-file index.js",
"postversion": "git push --follow-tags && conventional-github-releaser -p atom"
Expand All @@ -22,7 +22,8 @@
"assert-exception": "^2.0.0",
"babel-plugin-add-module-exports": "^0.3.3",
"bluebird": "^3.5.1",
"eastern": "^0.1.0",
"eastern": "^2.0.1",
"eastern-cli": "^1.0.0",
"firebase-admin": "^6.0.0",
"prettier": "^1.14.2",
"request": "^2.88.0",
Expand Down
11 changes: 5 additions & 6 deletions test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Promise from "bluebird";
import spec from "eastern";
import { strictEqual } from "assert";
import { rejects } from "assert-exception";

Expand All @@ -13,26 +12,26 @@ import createFirebaseAdminMiddlware from "./";
import credential from "./.credential";

let app, middleware, server, port;
spec.before(async () => {
it.before(async () => {
app = firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(credential)
});
middleware = createFirebaseAdminMiddlware(app);
server = await createIoServerAsync(middleware);
port = server.address().port;
});
spec.after(async () => {
it.after(async () => {
app.delete();
server.destroy();
});

spec("should deny unauthorized user using deny mode(default)", async () => {
it("should deny unauthorized user using deny mode(default)", async () => {
const error = await rejects(createIoClientAsync(port));
const expectMessage = "Decoding Firebase session cookie failed";
strictEqual(error.message.slice(0, expectMessage.length), expectMessage);
});

spec(
it(
"should allow authorized user, and middleware have user claims cache(default)",
async () => {
const session = await createSession(
Expand All @@ -41,7 +40,7 @@ spec(
);
server.io.on("connect", client => {
client.on("claims", resolve => {
resolve(middleware.getCache(client.id));
resolve(middleware.getCache(client));
});
});

Expand Down
Loading

0 comments on commit 2b6b820

Please sign in to comment.