Skip to content

Commit 1e92a22

Browse files
chore: wip
1 parent 2cd7044 commit 1e92a22

File tree

2 files changed

+78
-69
lines changed

2 files changed

+78
-69
lines changed

app/Middleware/Api.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
import { HttpError } from '@stacksjs/error-handling'
2-
import { Middleware, request } from '@stacksjs/router'
3-
import { AccessToken, Team } from '../../storage/framework/orm/src'
1+
// import { HttpError } from '@stacksjs/error-handling'
2+
// import { Middleware, request } from '@stacksjs/router'
3+
// import { AccessToken, Team } from '../../storage/framework/orm/src'
44

5-
export default new Middleware({
6-
name: 'API Authentication',
7-
priority: 1,
8-
async handle() {
9-
const bearerToken = request.bearerToken() || ''
5+
// export default new Middleware({
6+
// name: 'API Authentication',
7+
// priority: 1,
8+
// async handle() {
9+
// const bearerToken = request.bearerToken() || ''
1010

11-
if (!bearerToken)
12-
throw new HttpError(401, 'Unauthorized.')
11+
// if (!bearerToken)
12+
// throw new HttpError(401, 'Unauthorized.')
1313

14-
const parts = bearerToken.split(':')
14+
// const parts = bearerToken.split(':')
1515

16-
if (parts.length !== 3)
17-
throw new HttpError(401, 'Invalid bearer token format')
16+
// if (parts.length !== 3)
17+
// throw new HttpError(401, 'Invalid bearer token format')
1818

19-
const tokenId = Number(parts[0])
20-
const teamId = parts[1] as string
21-
const plainString = parts[2] as string
19+
// const tokenId = Number(parts[0])
20+
// const teamId = parts[1] as string
21+
// const plainString = parts[2] as string
2222

23-
const team = await Team.find(Number(teamId))
23+
// const team = await Team.find(Number(teamId))
2424

25-
if (!team)
26-
throw new HttpError(401, 'Invalid bearer token')
25+
// if (!team)
26+
// throw new HttpError(401, 'Invalid bearer token')
2727

28-
const accessTokens = await team.teamAccessTokens()
28+
// const accessTokens = await team.teamAccessTokens()
2929

30-
if (!accessTokens.length)
31-
throw new HttpError(401, 'Invalid bearer token')
30+
// if (!accessTokens.length)
31+
// throw new HttpError(401, 'Invalid bearer token')
3232

33-
const accessTokenIds = accessTokens.map(accessToken => accessToken.id)
33+
// const accessTokenIds = accessTokens.map(accessToken => accessToken.id)
3434

35-
if (!accessTokenIds.includes(tokenId))
36-
throw new HttpError(401, 'Invalid bearer token')
35+
// if (!accessTokenIds.includes(tokenId))
36+
// throw new HttpError(401, 'Invalid bearer token')
3737

38-
const teamBearerToken = await AccessToken.where('token', plainString).first()
38+
// const teamBearerToken = await AccessToken.where('token', plainString).first()
3939

40-
if (!teamBearerToken)
41-
throw new HttpError(401, 'Invalid bearer token')
42-
},
43-
})
40+
// if (!teamBearerToken)
41+
// throw new HttpError(401, 'Invalid bearer token')
42+
// },
43+
// })

storage/framework/core/router/src/router.ts

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type ActionPath = string
1212

1313
export class Router implements RouterInterface {
1414
private routes: Route[] = []
15-
private groupPrefix = ''
1615
private path = ''
1716

1817
private addRoute(
@@ -168,8 +167,6 @@ export class Router implements RouterInterface {
168167

169168
let cb: () => void
170169

171-
this.prepareGroupPrefix(options)
172-
173170
if (typeof options === 'function') {
174171
cb = options
175172
options = {}
@@ -191,21 +188,61 @@ export class Router implements RouterInterface {
191188
// Execute the callback. This will add routes to the new this.routes array
192189
cb()
193190

194-
// For each route added by the callback, adjust the URI and add to the original routes array
195-
this.routes.forEach((r) => {
196-
if (middleware.length)
197-
r.middleware = middleware
191+
if (typeof options === 'object') {
192+
this.routes.forEach((r) => {
193+
// Add middleware if any
194+
if (middleware.length)
195+
r.middleware = middleware
196+
197+
// Add the prefix to the route path
198+
199+
if (options.prefix) {
200+
r.path = `${options.prefix}/${r.uri}`
201+
r.uri = `${options.prefix}/${r.uri}`
202+
r.url = `${options.prefix}/${r.uri}`
203+
}
204+
205+
// Push the modified route to the original routes array
206+
originalRoutes.push(r)
207+
208+
return this
209+
})
210+
}
198211

199-
originalRoutes.push(r)
200-
return this
201-
})
212+
// this.prepareGroupPrefix(this.routes, options, middleware)
202213

203214
// Restore the original routes array.
204215
this.routes = originalRoutes
205216

206217
return this
207218
}
208219

220+
// private prepareGroupPrefix(routes: Route[], options: string | RouteGroupOptions, middleware: string | string[]): void {
221+
// if (typeof options !== 'string') {
222+
// routes.forEach((r) => {
223+
// // Add middleware if any
224+
// if (middleware.length)
225+
// r.middleware = middleware
226+
227+
// // Add the prefix to the route path
228+
// if (options.prefix) {
229+
// r.path = `/${options.prefix}${r.path}`
230+
// }
231+
232+
// // Push the modified route to the original routes array
233+
// originalRoutes.push(r)
234+
// return this
235+
// })
236+
237+
// return
238+
// }
239+
240+
// if (typeof options === 'string') {
241+
242+
// return
243+
// }
244+
// }
245+
209246
public name(name: string): this {
210247
this.routes[this.routes.length - 1].name = name
211248

@@ -231,33 +268,6 @@ export class Router implements RouterInterface {
231268
return this.routes
232269
}
233270

234-
private setGroupPrefix(prefix: string, options: RouteGroupOptions = {}) {
235-
if (prefix !== '') {
236-
prefix = `/${this.groupPrefix}/${prefix}`.replace(/\/\//g, '/') // remove double slashes in case there are any
237-
this.groupPrefix = prefix
238-
return
239-
}
240-
241-
// Ensure options is always treated as an object, even if it's undefined or a function
242-
const effectiveOptions = typeof options === 'object' ? options : {}
243-
244-
this.groupPrefix = effectiveOptions.prefix ?? prefix ?? ''
245-
}
246-
247-
private prepareGroupPrefix(options: string | RouteGroupOptions): void {
248-
if (this.groupPrefix !== '' && typeof options !== 'string') {
249-
this.setGroupPrefix(this.groupPrefix, options)
250-
return
251-
}
252-
253-
if (typeof options === 'string') {
254-
this.setGroupPrefix(options)
255-
return
256-
}
257-
258-
this.setGroupPrefix('', options)
259-
}
260-
261271
public async resolveCallback(callback: Route['callback']): Promise<Route['callback']> {
262272
if (callback instanceof Promise) {
263273
const actionModule = await callback
@@ -377,8 +387,7 @@ export class Router implements RouterInterface {
377387
if (path.startsWith('/'))
378388
path = path.slice(1)
379389

380-
path = `${this.groupPrefix}/${path}`
381-
390+
path = `/${path}`
382391
// if path ends in "/", then remove it
383392
// e.g. triggered when route is "/"
384393
return path.endsWith('/') ? path.slice(0, -1) : path

0 commit comments

Comments
 (0)