Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Create custom token instead of jwt

Hakan Shehu001c3f1d0e85
diff --git a/server/package-lock.json b/server/package-lock.json
index a34d4a99..229c0575 100644
--- a/server/package-lock.json
+++ b/server/package-lock.json
@@ -15,7 +15,7 @@
"cors": "^2.8.5",
"express": "^4.21.0",
"js-base64": "^3.7.7",
- "jsonwebtoken": "^9.0.2",
+ "js-sha256": "^0.11.0",
"kafkajs": "^2.2.4",
"kysely": "^0.27.4",
"pg": "^8.13.0",
@@ -657,12 +657,6 @@
"node": ">=8"
}
},
- "node_modules/buffer-equal-constant-time": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
- "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==",
- "license": "BSD-3-Clause"
- },
"node_modules/bytes": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
@@ -1029,15 +1023,6 @@
"node": ">=0.3.1"
}
},
- "node_modules/ecdsa-sig-formatter": {
- "version": "1.0.11",
- "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
- "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
- "license": "Apache-2.0",
- "dependencies": {
- "safe-buffer": "^5.0.1"
- }
- },
"node_modules/ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@@ -1621,6 +1606,12 @@
"integrity": "sha512-7rCnleh0z2CkXhH67J8K1Ytz0b2Y+yxTPL+/KOJoa20hfnVQ/3/T6W/KflYI4bRHRagNeXeU2bkNGI3v1oS/lw==",
"license": "BSD-3-Clause"
},
+ "node_modules/js-sha256": {
+ "version": "0.11.0",
+ "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.11.0.tgz",
+ "integrity": "sha512-6xNlKayMZvds9h1Y1VWc0fQHQ82BxTXizWPEtEeGvmOUYpBRy4gbWroHLpzowe6xiQhHpelCQiE7HEdznyBL9Q==",
+ "license": "MIT"
+ },
"node_modules/json5": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
@@ -1634,55 +1625,6 @@
"node": ">=6"
}
},
- "node_modules/jsonwebtoken": {
- "version": "9.0.2",
- "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz",
- "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==",
- "license": "MIT",
- "dependencies": {
- "jws": "^3.2.2",
- "lodash.includes": "^4.3.0",
- "lodash.isboolean": "^3.0.3",
- "lodash.isinteger": "^4.0.4",
- "lodash.isnumber": "^3.0.3",
- "lodash.isplainobject": "^4.0.6",
- "lodash.isstring": "^4.0.1",
- "lodash.once": "^4.0.0",
- "ms": "^2.1.1",
- "semver": "^7.5.4"
- },
- "engines": {
- "node": ">=12",
- "npm": ">=6"
- }
- },
- "node_modules/jsonwebtoken/node_modules/ms": {
- "version": "2.1.3",
- "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
- "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
- "license": "MIT"
- },
- "node_modules/jwa": {
- "version": "1.4.1",
- "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
- "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
- "license": "MIT",
- "dependencies": {
- "buffer-equal-constant-time": "1.0.1",
- "ecdsa-sig-formatter": "1.0.11",
- "safe-buffer": "^5.0.1"
- }
- },
- "node_modules/jws": {
- "version": "3.2.2",
- "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz",
- "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
- "license": "MIT",
- "dependencies": {
- "jwa": "^1.4.1",
- "safe-buffer": "^5.0.1"
- }
- },
"node_modules/kafkajs": {
"version": "2.2.4",
"resolved": "https://registry.npmjs.org/kafkajs/-/kafkajs-2.2.4.tgz",
@@ -1729,48 +1671,6 @@
"dev": true,
"license": "MIT"
},
- "node_modules/lodash.includes": {
- "version": "4.3.0",
- "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
- "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==",
- "license": "MIT"
- },
- "node_modules/lodash.isboolean": {
- "version": "3.0.3",
- "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
- "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==",
- "license": "MIT"
- },
- "node_modules/lodash.isinteger": {
- "version": "4.0.4",
- "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
- "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==",
- "license": "MIT"
- },
- "node_modules/lodash.isnumber": {
- "version": "3.0.3",
- "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
- "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==",
- "license": "MIT"
- },
- "node_modules/lodash.isplainobject": {
- "version": "4.0.6",
- "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
- "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==",
- "license": "MIT"
- },
- "node_modules/lodash.isstring": {
- "version": "4.0.1",
- "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
- "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==",
- "license": "MIT"
- },
- "node_modules/lodash.once": {
- "version": "4.1.1",
- "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
- "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==",
- "license": "MIT"
- },
"node_modules/make-dir": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz",
diff --git a/server/package.json b/server/package.json
index e17ac8e0..6e820e8a 100644
--- a/server/package.json
+++ b/server/package.json
@@ -33,7 +33,7 @@
"cors": "^2.8.5",
"express": "^4.21.0",
"js-base64": "^3.7.7",
- "jsonwebtoken": "^9.0.2",
+ "js-sha256": "^0.11.0",
"kafkajs": "^2.2.4",
"kysely": "^0.27.4",
"pg": "^8.13.0",
diff --git a/server/src/data/migrations.ts b/server/src/data/migrations.ts
index c5aa0f7f..3316f8e6 100644
--- a/server/src/data/migrations.ts
+++ b/server/src/data/migrations.ts
@@ -167,6 +167,11 @@ const createAccountDevicesTable: Migration = {
.createTable('account_devices')
.addColumn('id', 'varchar(30)', (col) => col.notNull().primaryKey())
.addColumn('account_id', 'varchar(30)', (col) => col.notNull())
+ .addColumn('token_hash', 'varchar(100)', (col) => col.notNull())
+ .addColumn('token_salt', 'varchar(100)', (col) => col.notNull())
+ .addColumn('token_generated_at', 'timestamptz', (col) => col.notNull())
+ .addColumn('previous_token_hash', 'varchar(100)')
+ .addColumn('previous_token_salt', 'varchar(100)')
.addColumn('type', 'integer', (col) => col.notNull())
.addColumn('version', 'varchar(30)', (col) => col.notNull())
.addColumn('platform', 'varchar(30)')
diff --git a/server/src/data/schema.ts b/server/src/data/schema.ts
index 56489042..f95bdcff 100644
--- a/server/src/data/schema.ts
+++ b/server/src/data/schema.ts
@@ -62,6 +62,11 @@ export type UpdateWorkspaceUser = Updateable<WorkspaceUserTable>;
interface AccountDeviceTable {
id: ColumnType<string, string, never>;
account_id: ColumnType<string, string, never>;
+ token_hash: ColumnType<string, string, string>;
+ token_salt: ColumnType<string, string, string>;
+ token_generated_at: ColumnType<Date, Date, Date>;
+ previous_token_hash: ColumnType<string | null, string | null, string | null>;
+ previous_token_salt: ColumnType<string | null, string | null, string | null>;
type: ColumnType<number, number, number>;
version: ColumnType<string, string, string>;
platform: ColumnType<string | null, string | null, string | null>;
diff --git a/server/src/lib/jwt.ts b/server/src/lib/jwt.ts
deleted file mode 100644
index 0e6f7a11..00000000
--- a/server/src/lib/jwt.ts
+++ /dev/null
@@ -1,34 +0,0 @@
-import jwt from 'jsonwebtoken';
-
-const JwtSecretKey = process.env.JWT_SECRET ?? '';
-const JwtAudience = process.env.JWT_AUDIENCE ?? '';
-const JwtIssuer = process.env.JWT_ISSUER ?? '';
-
-export type JwtPayload = {
- id: string;
- name: string;
- email: string;
-};
-
-export const createJwtToken = (payload: JwtPayload): string => {
- const signOptions: jwt.SignOptions = {
- issuer: JwtIssuer,
- audience: JwtAudience,
- subject: payload.id,
- };
-
- return jwt.sign(payload, JwtSecretKey, signOptions);
-};
-
-export const verifyJwtToken = (token: string): JwtPayload | null => {
- try {
- const decoded = jwt.verify(token, JwtSecretKey, {
- issuer: JwtIssuer,
- audience: JwtAudience,
- });
-
- return decoded as JwtPayload;
- } catch (err) {
- return null;
- }
-};
diff --git a/server/src/lib/tokens.ts b/server/src/lib/tokens.ts
new file mode 100644
index 00000000..7380608b
--- /dev/null
+++ b/server/src/lib/tokens.ts
@@ -0,0 +1,72 @@
+import { database } from '@/data/database';
+import { uuid } from '@/lib/utils';
+import { NeuronRequestAccount } from '@/types/api';
+import { sha256 } from 'js-sha256';
+
+interface GenerateTokenResult {
+ token: string;
+ salt: string;
+ hash: string;
+}
+
+type VerifyTokenResult =
+ | {
+ authenticated: false;
+ }
+ | {
+ authenticated: true;
+ account: NeuronRequestAccount;
+ };
+
+export const generateToken = (id: string): GenerateTokenResult => {
+ const salt = uuid();
+ const secret = uuid() + uuid();
+ const token = id + secret;
+ const hash = sha256(secret + salt);
+
+ return {
+ token,
+ salt,
+ hash,
+ };
+};
+
+export const verifyToken = async (
+ token: string,
+): Promise<VerifyTokenResult> => {
+ const id = token.slice(0, 28);
+ const secret = token.slice(28);
+
+ const accountDevice = await database
+ .selectFrom('account_devices')
+ .selectAll()
+ .where('id', '=', id)
+ .executeTakeFirst();
+
+ if (!accountDevice) {
+ return {
+ authenticated: false,
+ };
+ }
+
+ if (
+ !verifySecret(secret, accountDevice.token_salt, accountDevice.token_hash)
+ ) {
+ return {
+ authenticated: false,
+ };
+ }
+
+ return {
+ authenticated: true,
+ account: {
+ id: accountDevice.account_id,
+ deviceId: accountDevice.id,
+ },
+ };
+};
+
+const verifySecret = (secret: string, salt: string, hash: string): boolean => {
+ const computedHash = sha256(secret + salt);
+ return computedHash === hash;
+};
diff --git a/server/src/lib/utils.ts b/server/src/lib/utils.ts
index 8ae709a1..29e06f02 100644
--- a/server/src/lib/utils.ts
+++ b/server/src/lib/utils.ts
@@ -30,3 +30,7 @@ export const getNameFromEmail = (email: string): string => {
return displayName;
};
+
+export const uuid = () => {
+ return crypto.randomUUID().replace(/-/g, '');
+};
diff --git a/server/src/middlewares/auth.ts b/server/src/middlewares/auth.ts
index df37e187..d3d7b4bf 100644
--- a/server/src/middlewares/auth.ts
+++ b/server/src/middlewares/auth.ts
@@ -4,9 +4,9 @@ import {
NeuronRequest,
NeuronResponse,
} from '@/types/api';
-import { verifyJwtToken } from '@/lib/jwt';
+import { verifyToken } from '@/lib/tokens';
-export const authMiddleware = (
+export const authMiddleware = async (
req: NeuronRequest,
res: NeuronResponse,
next: NeuronNextFunction,
@@ -20,14 +20,14 @@ export const authMiddleware = (
});
}
- const payload = verifyJwtToken(token);
- if (!payload) {
+ const result = await verifyToken(token);
+ if (!result.authenticated) {
return res.status(400).json({
code: ApiError.Unauthorized,
message: 'Invalid Token',
});
}
- req.accountId = payload.id as string;
+ req.account = result.account;
next();
};
diff --git a/server/src/routes/accounts.ts b/server/src/routes/accounts.ts
index b493ab7b..7b0169b1 100644
--- a/server/src/routes/accounts.ts
+++ b/server/src/routes/accounts.ts
@@ -13,8 +13,8 @@ import { NeuronId } from '@/lib/id';
import { database } from '@/data/database';
import bcrypt from 'bcrypt';
import { WorkspaceOutput } from '@/types/workspaces';
-import { createJwtToken } from '@/lib/jwt';
import { authMiddleware } from '@/middlewares/auth';
+import { generateToken } from '@/lib/tokens';
const GoogleUserInfoUrl = 'https://www.googleapis.com/oauth2/v1/userinfo';
const SaltRounds = 10;
@@ -202,45 +202,19 @@ accountsRouter.post('/login/google', async (req: Request, res: Response) => {
});
accountsRouter.delete(
- '/logout/:deviceId',
+ '/logout',
authMiddleware,
async (req: NeuronRequest, res: NeuronResponse) => {
- console.log('logout endpoint called', req.params.deviceId);
- if (!req.accountId) {
+ if (!req.account) {
return res.status(401).json({
code: ApiError.Unauthorized,
message: 'Unauthorized.',
});
}
- const deviceId = req.params.deviceId as string;
- if (!deviceId) {
- return res.status(401).json({
- code: ApiError.Unauthorized,
- message: 'Unauthorized.',
- });
- }
-
- const accountDevice = await database
- .selectFrom('account_devices')
- .selectAll()
- .where('id', '=', deviceId)
- .executeTakeFirst();
-
- if (!accountDevice) {
- return res.status(200).end();
- }
-
- if (accountDevice.account_id != req.accountId) {
- return res.status(403).json({
- code: ApiError.Forbidden,
- message: 'Forbidden',
- });
- }
-
await database
.deleteFrom('account_devices')
- .where('id', '=', deviceId)
+ .where('id', '=', req.account.deviceId)
.execute();
return res.status(200).end();
@@ -252,12 +226,6 @@ const buildLoginOutput = async (
name: string,
email: string,
): Promise<LoginOutput> => {
- const token = createJwtToken({
- id,
- name,
- email,
- });
-
const workspaceUsers = await database
.selectFrom('workspace_users')
.where('account_id', '=', id)
@@ -294,11 +262,17 @@ const buildLoginOutput = async (
}
}
+ const deviceId = NeuronId.generate(NeuronId.Type.Device);
+ const { token, salt, hash } = generateToken(deviceId);
+
const accountDevice = await database
.insertInto('account_devices')
.values({
- id: NeuronId.generate(NeuronId.Type.Device),
+ id: deviceId,
account_id: id,
+ token_hash: hash,
+ token_salt: salt,
+ token_generated_at: new Date(),
type: 1,
created_at: new Date(),
version: '0.1.0',
diff --git a/server/src/routes/sync.ts b/server/src/routes/sync.ts
index f2041f98..7bcfbe5c 100644
--- a/server/src/routes/sync.ts
+++ b/server/src/routes/sync.ts
@@ -90,7 +90,7 @@ syncRouter.post(
async (req: NeuronRequest, res: NeuronResponse) => {
const workspaceId = req.params.workspaceId as string;
const input = req.body as SyncLocalChangesInput;
- if (!req.accountId) {
+ if (!req.account) {
return res.status(401).json({
code: ApiError.Unauthorized,
message: 'Unauthorized.',
@@ -110,14 +110,14 @@ syncRouter.post(
});
}
- const workspaceAccount = await database
+ const workspaceUser = await database
.selectFrom('workspace_users')
.selectAll()
.where('workspace_id', '=', workspace.id)
- .where('account_id', '=', req.accountId)
+ .where('account_id', '=', req.account.id)
.executeTakeFirst();
- if (!workspaceAccount) {
+ if (!workspaceUser) {
return res.status(403).json({
code: ApiError.Forbidden,
message: 'Forbidden.',
@@ -127,7 +127,7 @@ syncRouter.post(
const results: ServerSyncChangeResult[] = [];
for (const mutation of input.changes) {
try {
- const result = await handleLocalChange(workspaceAccount, mutation);
+ const result = await handleLocalChange(workspaceUser, mutation);
results.push({
id: mutation.id,
status: result.status,
diff --git a/server/src/routes/workspaces.ts b/server/src/routes/workspaces.ts
index 949347d1..5fff529f 100644
--- a/server/src/routes/workspaces.ts
+++ b/server/src/routes/workspaces.ts
@@ -32,7 +32,7 @@ export const workspacesRouter = Router();
workspacesRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {
const input: WorkspaceInput = req.body;
- if (!req.accountId) {
+ if (!req.account) {
return res.status(401).json({
code: ApiError.Unauthorized,
message: 'Unauthorized.',
@@ -49,7 +49,7 @@ workspacesRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {
const account = await database
.selectFrom('accounts')
.selectAll()
- .where('id', '=', req.accountId)
+ .where('id', '=', req.account.id)
.executeTakeFirst();
if (!account) {
@@ -65,7 +65,7 @@ workspacesRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {
description: input.description,
avatar: input.avatar,
createdAt: new Date(),
- createdBy: req.accountId,
+ createdBy: account.id,
status: WorkspaceStatus.Active,
versionId: NeuronId.generate(NeuronId.Type.Version),
};
@@ -91,11 +91,11 @@ workspacesRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {
const workspaceUser: WorkspaceUser = {
id: userId,
- accountId: req.accountId,
+ accountId: account.id,
workspaceId: workspace.id,
role: WorkspaceRole.Owner,
createdAt: new Date(),
- createdBy: req.accountId,
+ createdBy: account.id,
status: WorkspaceUserStatus.Active,
versionId: NeuronId.generate(NeuronId.Type.Version),
};
@@ -164,7 +164,7 @@ workspacesRouter.put(
const id = req.params.id;
const input: WorkspaceInput = req.body;
- if (!req.accountId) {
+ if (!req.account) {
return res.status(401).json({
code: ApiError.Unauthorized,
message: 'Unauthorized.',
@@ -188,7 +188,7 @@ workspacesRouter.put(
.selectFrom('workspace_users')
.selectAll()
.where('workspace_id', '=', id)
- .where('account_id', '=', req.accountId)
+ .where('account_id', '=', req.account.id)
.executeTakeFirst();
if (!workspaceUser) {
@@ -226,7 +226,7 @@ workspacesRouter.put(
description: input.description,
avatar: input.avatar,
updated_at: new Date(),
- updated_by: req.accountId,
+ updated_by: req.account.id,
})
.where('id', '=', id)
.returningAll()
@@ -245,7 +245,7 @@ workspacesRouter.put(
description: updatedWorkspace.description,
avatar: updatedWorkspace.avatar,
versionId: updatedWorkspace.version_id,
- accountId: req.accountId,
+ accountId: req.account.id,
role: workspaceUser.role,
userId: workspaceUser.id,
};
@@ -259,7 +259,7 @@ workspacesRouter.delete(
async (req: NeuronRequest, res: NeuronResponse) => {
const id = req.params.id;
- if (!req.accountId) {
+ if (!req.account) {
return res.status(401).json({
code: ApiError.Unauthorized,
message: 'Unauthorized.',
@@ -283,7 +283,7 @@ workspacesRouter.delete(
.selectFrom('workspace_users')
.selectAll()
.where('workspace_id', '=', id)
- .where('account_id', '=', req.accountId)
+ .where('account_id', '=', req.account.id)
.executeTakeFirst();
if (!workspaceUser) {
@@ -313,7 +313,7 @@ workspacesRouter.get(
async (req: NeuronRequest, res: NeuronResponse) => {
const id = req.params.id;
- if (!req.accountId) {
+ if (!req.account) {
return res.status(401).json({
code: ApiError.Unauthorized,
message: 'Unauthorized.',
@@ -337,7 +337,7 @@ workspacesRouter.get(
.selectFrom('workspace_users')
.selectAll()
.where('workspace_id', '=', id)
- .where('account_id', '=', req.accountId)
+ .where('account_id', '=', req.account.id)
.executeTakeFirst();
if (!workspaceUser) {
@@ -353,7 +353,7 @@ workspacesRouter.get(
description: workspace.description,
avatar: workspace.avatar,
versionId: workspace.version_id,
- accountId: req.accountId,
+ accountId: req.account.id,
role: workspaceUser.role,
userId: workspaceUser.id,
};
@@ -363,7 +363,7 @@ workspacesRouter.get(
);
workspacesRouter.get('/', async (req: NeuronRequest, res: NeuronResponse) => {
- if (!req.accountId) {
+ if (!req.account) {
return res.status(401).json({
code: ApiError.Unauthorized,
message: 'Unauthorized.',
@@ -373,7 +373,7 @@ workspacesRouter.get('/', async (req: NeuronRequest, res: NeuronResponse) => {
const workspaceUsers = await database
.selectFrom('workspace_users')
.selectAll()
- .where('account_id', '=', req.accountId)
+ .where('account_id', '=', req.account.id)
.execute();
const workspaceIds = workspaceUsers.map((wa) => wa.workspace_id);
@@ -400,7 +400,7 @@ workspacesRouter.get('/', async (req: NeuronRequest, res: NeuronResponse) => {
description: workspace.description,
avatar: workspace.avatar,
versionId: workspace.version_id,
- accountId: req.accountId,
+ accountId: req.account.id,
role: workspaceAccount.role,
userId: workspaceAccount.id,
};
@@ -424,7 +424,7 @@ workspacesRouter.post(
});
}
- if (!req.accountId) {
+ if (!req.account) {
return res.status(401).json({
code: ApiError.Unauthorized,
message: 'Unauthorized.',
@@ -448,7 +448,7 @@ workspacesRouter.post(
.selectFrom('workspace_users')
.selectAll()
.where('workspace_id', '=', id)
- .where('account_id', '=', req.accountId)
+ .where('account_id', '=', req.account.id)
.executeTakeFirst();
if (!workspaceUser) {
@@ -575,7 +575,7 @@ workspacesRouter.post(
workspace_id: workspace.id,
role: WorkspaceRole.Collaborator,
created_at: new Date(),
- created_by: req.accountId,
+ created_by: req.account.id,
status: WorkspaceUserStatus.Active,
version_id: NeuronId.generate(NeuronId.Type.Version),
});
@@ -643,7 +643,7 @@ workspacesRouter.put(
const userId = req.params.userId;
const input: WorkspaceAccountRoleUpdateInput = req.body;
- if (!req.accountId) {
+ if (!req.account) {
return res.status(401).json({
code: ApiError.Unauthorized,
message: 'Unauthorized.',
@@ -667,7 +667,7 @@ workspacesRouter.put(
.selectFrom('workspace_users')
.selectAll()
.where('workspace_id', '=', id)
- .where('account_id', '=', req.accountId)
+ .where('account_id', '=', req.account.id)
.executeTakeFirst();
if (!currentWorkspaceUser) {
diff --git a/server/src/types/api.ts b/server/src/types/api.ts
index 8f764d23..5bf79c85 100644
--- a/server/src/types/api.ts
+++ b/server/src/types/api.ts
@@ -13,8 +13,13 @@ export enum ApiError {
BadRequest = 'BadRequest',
}
+export type NeuronRequestAccount = {
+ id: string;
+ deviceId: string;
+};
+
export type NeuronRequest = Request & {
- accountId?: string | null;
+ account?: NeuronRequestAccount;
};
export type NeuronResponse = Response;