Skip to content

Commit

Permalink
feat: add lastLogin + lastSeen fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoffelen committed Sep 29, 2024
1 parent 3fa4845 commit 4c8070c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/api/src/routes/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Hono } from "hono";
import bcrypt from "bcryptjs";
import { v4 as uuid } from "uuid";

import { put, query } from "../../lib/database";
import { put, query, update } from "../../lib/database";

const app = new Hono();

Expand Down Expand Up @@ -39,6 +39,22 @@ app.post("/login", async (c) => {
true,
);

await update({
Key: {
pk: user.pk,
sk: user.sk,
},
UpdateExpression: `SET #lastLogin = :lastLogin, #lastSeen = :lastSeen`,
ExpressionAttributeValues: {
":lastLogin": new Date().toISOString(),
":lastSeen": new Date().toISOString(),
},
ExpressionAttributeNames: {
"#lastLogin": "lastLogin",
"#lastSeen": "lastSeen",
},
});

return c.json({ accessToken: id });
});

Expand Down
16 changes: 15 additions & 1 deletion packages/api/src/routes/auth/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bearerAuth } from "hono/bearer-auth";
import { query } from "../../lib/database";
import { query, update } from "../../lib/database";

const sessionCache = [];

Expand All @@ -20,6 +20,20 @@ export const auth = bearerAuth({
return false;
}

await update({
Key: {
pk: Items[0].sk,
sk: "user",
},
UpdateExpression: `SET #lastSeen = :lastSeen`,
ExpressionAttributeValues: {
":lastSeen": new Date().toISOString(),
},
ExpressionAttributeNames: {
"#lastSeen": "lastSeen",
},
});

sessionCache.push(token);
return true;
},
Expand Down

0 comments on commit 4c8070c

Please sign in to comment.