streamby-core 0.21.5 → 0.21.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,8 +4,12 @@ exports.projectRouter = projectRouter;
4
4
  const express_1 = require("express");
5
5
  const manager_1 = require("../../models/manager");
6
6
  const auth_1 = require("../../utils/auth");
7
+ const connectionManager_1 = require("../../adapters/database/connectionManager");
8
+ const sql_1 = require("../../adapters/database/sql");
9
+ const auth_2 = require("../../services/auth");
7
10
  function projectRouter(config) {
8
11
  const router = (0, express_1.Router)();
12
+ router.use((0, auth_2.authenticate)(config));
9
13
  const Project = (0, manager_1.getModel)('projects');
10
14
  const mapProjectToResponseFormat = (project, userId) => {
11
15
  const currentUserMember = project.members?.find((member) => member.userId === userId);
@@ -19,12 +23,7 @@ function projectRouter(config) {
19
23
  };
20
24
  router.get('/projects', async (req, res) => {
21
25
  try {
22
- const auth = await config.authProvider(req);
23
- if (!auth ||
24
- !auth.userId ||
25
- !auth.role) {
26
- return res.status(401).json({ error: 'Unauthorized' });
27
- }
26
+ const auth = req.auth;
28
27
  const archivedQuery = req.query.archived;
29
28
  const filterArchived = archivedQuery !== undefined ? String(archivedQuery).toLowerCase() === 'true' : undefined;
30
29
  const allProjects = await Project.find({}); // Fetch all projects
@@ -49,12 +48,7 @@ function projectRouter(config) {
49
48
  });
50
49
  router.post('/projects/create', async (req, res) => {
51
50
  try {
52
- const auth = await config.authProvider(req);
53
- if (!auth ||
54
- !auth.userId ||
55
- !auth.role) {
56
- return res.status(401).json({ error: 'Unauthorized' });
57
- }
51
+ const auth = req.auth;
58
52
  if (auth.role !== 'admin' && auth.role !== 'editor') {
59
53
  return res.status(403).json({ error: 'Permission denied' });
60
54
  }
@@ -84,12 +78,7 @@ function projectRouter(config) {
84
78
  });
85
79
  router.get('/projects/:id', async (req, res) => {
86
80
  try {
87
- const auth = await config.authProvider(req);
88
- if (!auth ||
89
- !auth.userId ||
90
- !auth.role) {
91
- return res.status(401).json({ error: 'Unauthorized' });
92
- }
81
+ const auth = req.auth;
93
82
  const projectId = req.params.id;
94
83
  const project = await Project.findOne({ _id: projectId });
95
84
  if (!project) {
@@ -106,12 +95,7 @@ function projectRouter(config) {
106
95
  });
107
96
  router.patch('/projects/:id', async (req, res) => {
108
97
  try {
109
- const auth = await config.authProvider(req);
110
- if (!auth ||
111
- !auth.userId ||
112
- !auth.role) {
113
- return res.status(401).json({ error: 'Unauthorized' });
114
- }
98
+ const auth = req.auth;
115
99
  if (auth.role !== 'admin' && auth.role !== 'editor') {
116
100
  return res.status(403).json({ error: 'Permission denied' });
117
101
  }
@@ -139,12 +123,7 @@ function projectRouter(config) {
139
123
  });
140
124
  router.delete('/projects/:id', async (req, res) => {
141
125
  try {
142
- const auth = await config.authProvider(req);
143
- if (!auth ||
144
- !auth.userId ||
145
- !auth.role) {
146
- return res.status(401).json({ error: 'Unauthorized' });
147
- }
126
+ const auth = req.auth;
148
127
  const projectId = req.params.id;
149
128
  const project = await Project.findOne({ _id: projectId });
150
129
  if (!project) {
@@ -162,18 +141,26 @@ function projectRouter(config) {
162
141
  });
163
142
  router.patch('/projects/:id/archive', async (req, res) => {
164
143
  try {
165
- const auth = await config.authProvider(req);
166
- if (!auth ||
167
- !auth.userId ||
168
- !auth.role) {
169
- return res.status(401).json({ error: 'Unauthorized' });
170
- }
144
+ const auth = req.auth;
171
145
  const projectId = req.params.id;
172
146
  const project = await Project.findOne({ _id: projectId });
173
147
  if (!project || !(0, auth_1.isProjectMember)(project, auth.userId)) {
174
148
  return res.status(403).json({ error: 'Unauthorized project access' });
175
149
  }
176
- await Project.update({ _id: projectId, "members.userId": auth.userId }, { "members.$.archived": true, "members.$.archivedBy": auth.userId, "members.$.archivedAt": new Date() });
150
+ if (project.dbType === 'sql') {
151
+ const connection = (0, connectionManager_1.getConnection)(config.databases?.find(db => db.main)?.id || '').client;
152
+ await sql_1.sqlAdapter.update(connection, 'project_members', { projectId: projectId, userId: auth.userId }, { archived: true, archivedBy: auth.userId, archivedAt: new Date() });
153
+ }
154
+ else {
155
+ const memberIndex = project.members.findIndex((member) => member.userId === auth.userId);
156
+ if (memberIndex === -1) {
157
+ return res.status(403).json({ error: 'Unauthorized project access' });
158
+ }
159
+ project.members[memberIndex].archived = true;
160
+ project.members[memberIndex].archivedBy = auth.userId;
161
+ project.members[memberIndex].archivedAt = new Date();
162
+ await Project.update({ _id: projectId }, { members: project.members });
163
+ }
177
164
  // Re-fetch all projects for the user to ensure the list is up-to-date
178
165
  const allProjects = await Project.find({});
179
166
  const projects = allProjects
@@ -190,18 +177,26 @@ function projectRouter(config) {
190
177
  });
191
178
  router.patch('/projects/:id/unarchive', async (req, res) => {
192
179
  try {
193
- const auth = await config.authProvider(req);
194
- if (!auth ||
195
- !auth.userId ||
196
- !auth.role) {
197
- return res.status(401).json({ error: 'Unauthorized' });
198
- }
180
+ const auth = req.auth;
199
181
  const projectId = req.params.id;
200
182
  const project = await Project.findOne({ _id: projectId });
201
183
  if (!project || !(0, auth_1.isProjectMember)(project, auth.userId)) {
202
184
  return res.status(403).json({ error: 'Unauthorized project access' });
203
185
  }
204
- await Project.update({ _id: projectId, "members.userId": auth.userId }, { "members.$.archived": false, "members.$.archivedBy": null, "members.$.archivedAt": null });
186
+ if (project.dbType === 'sql') {
187
+ const connection = (0, connectionManager_1.getConnection)(config.databases?.find(db => db.main)?.id || '').client;
188
+ await sql_1.sqlAdapter.update(connection, 'project_members', { projectId: projectId, userId: auth.userId }, { archived: false, archivedBy: null, archivedAt: null });
189
+ }
190
+ else {
191
+ const memberIndex = project.members.findIndex((member) => member.userId === auth.userId);
192
+ if (memberIndex === -1) {
193
+ return res.status(403).json({ error: 'Unauthorized project access' });
194
+ }
195
+ project.members[memberIndex].archived = false;
196
+ project.members[memberIndex].archivedBy = null;
197
+ project.members[memberIndex].archivedAt = null;
198
+ await Project.update({ _id: projectId }, { members: project.members });
199
+ }
205
200
  // Re-fetch all projects for the user to ensure the list is up-to-date
206
201
  const allProjects = await Project.find({});
207
202
  const projects = allProjects
@@ -218,12 +213,7 @@ function projectRouter(config) {
218
213
  });
219
214
  router.get('/projects/:id/members', async (req, res) => {
220
215
  try {
221
- const auth = await config.authProvider(req);
222
- if (!auth ||
223
- !auth.userId ||
224
- !auth.role) {
225
- return res.status(401).json({ error: 'Unauthorized' });
226
- }
216
+ const auth = req.auth;
227
217
  const projectId = req.params.id;
228
218
  const project = await Project.findOne({ _id: projectId });
229
219
  if (!project) {
@@ -255,12 +245,7 @@ function projectRouter(config) {
255
245
  });
256
246
  router.post('/projects/:id/image', async (req, res) => {
257
247
  try {
258
- const auth = await config.authProvider(req);
259
- if (!auth ||
260
- !auth.userId ||
261
- !auth.role) {
262
- return res.status(401).json({ error: 'Unauthorized' });
263
- }
248
+ const auth = req.auth;
264
249
  if (auth.role !== 'admin' && auth.role !== 'editor') {
265
250
  return res.status(403).json({ error: 'Permission denied' });
266
251
  }
@@ -1,2 +1,5 @@
1
- import { AuthContext } from '../types';
2
- export declare function checkRole(auth: AuthContext, required: 'viewer' | 'editor' | 'admin'): void;
1
+ import { Auth } from '../types';
2
+ import { Request, Response, NextFunction } from 'express';
3
+ import { StreamByConfig } from '../types';
4
+ export declare const authenticate: (config: StreamByConfig) => (req: Request, res: Response, next: NextFunction) => Promise<Response<any, Record<string, any>> | undefined>;
5
+ export declare function checkRole(auth: Auth, required: 'viewer' | 'editor' | 'admin'): void;
@@ -1,6 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.authenticate = void 0;
3
4
  exports.checkRole = checkRole;
5
+ const authenticate = (config) => async (req, res, next) => {
6
+ try {
7
+ const auth = await config.authProvider(req);
8
+ if (!auth || !auth.userId || !auth.role) {
9
+ return res.status(401).json({ error: 'Unauthorized' });
10
+ }
11
+ req.auth = auth;
12
+ next();
13
+ }
14
+ catch (error) {
15
+ console.error('Authentication failed:', error);
16
+ return res.status(401).json({ error: 'Unauthorized' });
17
+ }
18
+ };
19
+ exports.authenticate = authenticate;
4
20
  function checkRole(auth, required) {
5
21
  const roles = ['viewer', 'editor', 'admin'];
6
22
  const userIdx = roles.indexOf(auth.role);
@@ -61,12 +61,12 @@ export interface StreamByConfig {
61
61
  databases?: DatabaseCredential[];
62
62
  adapter?: StorageAdapter;
63
63
  }
64
- export interface AuthContext {
64
+ export interface Auth {
65
65
  userId: string;
66
66
  username: string;
67
67
  role: 'viewer' | 'editor' | 'admin';
68
68
  }
69
- export type AuthProvider = (req: Request) => Promise<AuthContext>;
69
+ export type AuthProvider = (req: Request) => Promise<Auth>;
70
70
  export interface Export {
71
71
  _id?: string;
72
72
  id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "streamby-core",
3
- "version": "0.21.5",
3
+ "version": "0.21.7",
4
4
  "description": "StreamBy middleware framework for media storage management",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -49,7 +49,7 @@
49
49
  "@types/pg": "^8.15.4",
50
50
  "@types/supertest": "^6.0.3",
51
51
  "cors": "^2.8.5",
52
- "supertest": "^7.1.0",
52
+ "supertest": "^7.1.4",
53
53
  "ts-node-dev": "^2.0.0",
54
54
  "typescript": "^5.8.3",
55
55
  "vitest": "^3.1.1"