Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/api/src/services/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { signAccessToken } from "../utils/jwt.js";

export async function registerUser(payload) {
// TODO: persist new user via Prisma
const id = `usr_${Date.now()}`;
return {
id: `usr_${Date.now()}`,
id,
email: payload.email,
role: payload.role,
token: signAccessToken({ sub: `usr_${Date.now()}`, role: payload.role })
token: signAccessToken({ sub: id, role: payload.role })
};
}

Expand Down
26 changes: 26 additions & 0 deletions apps/api/src/tests/authService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from "node:test";
import assert from "node:assert/strict";
import { registerUser } from "../services/authService.js";
import { verifyAccessToken } from "../utils/jwt.js";

test("registerUser signs a token for the returned user id", async () => {
const originalDateNow = Date.now;
const timestamps = [1700000000000, 1700000001234];
Date.now = () => timestamps.shift() ?? 1700000001234;

try {
const result = await registerUser({
email: "new-user@example.com",
password: "secure-password",
role: "client"
});

const decoded = verifyAccessToken(result.token);

assert.equal(result.id, "usr_1700000000000");
assert.equal(decoded.sub, result.id);
assert.equal(decoded.role, "client");
} finally {
Date.now = originalDateNow;
}
});
Loading