Skip to content

Commit f7bad71

Browse files
chore: wip
1 parent 1024f69 commit f7bad71

File tree

5 files changed

+62
-34
lines changed

5 files changed

+62
-34
lines changed
+22-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RequestInstance } from '@stacksjs/types'
22
import { Action } from '@stacksjs/actions'
3+
import { generateRegistrationOptions, getUserPasskeys } from '@stacksjs/auth'
34
import User from '../../../storage/framework/orm/src/models/User.ts'
45

56
export default new Action({
@@ -9,36 +10,31 @@ export default new Action({
910
async handle(request: RequestInstance) {
1011
const email = request.get('email') ?? ''
1112

12-
try {
13-
const user = await User.where('email', email).firstOrFail()
14-
}
15-
catch (error) {
16-
console.log(error)
17-
}
13+
// const user = await User.where('email', email).firstOrFail()
1814

19-
// if (!user)
20-
// return
15+
if (!user)
16+
return
2117

22-
// const userPasskeys = await getUserPasskeys(user?.id as number)
18+
const userPasskeys = await getUserPasskeys(user?.id as number)
2319

24-
// const userEmail = user?.email ?? ''
20+
const userEmail = user?.email ?? ''
2521

26-
// const options = await generateRegistrationOptions({
27-
// rpName: 'Stacks',
28-
// rpID: 'localhost',
29-
// userName: userEmail,
30-
// attestationType: 'none',
31-
// excludeCredentials: userPasskeys.map(passkey => ({
32-
// id: passkey.id,
33-
// transports: ['internal'], // TODO: Dynamic
34-
// })),
35-
// authenticatorSelection: {
36-
// residentKey: 'preferred',
37-
// userVerification: 'preferred',
38-
// authenticatorAttachment: 'platform',
39-
// },
40-
// })
22+
const options = await generateRegistrationOptions({
23+
rpName: 'Stacks',
24+
rpID: 'localhost',
25+
userName: userEmail,
26+
attestationType: 'none',
27+
excludeCredentials: userPasskeys.map(passkey => ({
28+
id: passkey.id,
29+
transports: ['internal'], // TODO: Dynamic
30+
})),
31+
authenticatorSelection: {
32+
residentKey: 'preferred',
33+
userVerification: 'preferred',
34+
authenticatorAttachment: 'platform',
35+
},
36+
})
4137

42-
// return options
38+
return options
4339
},
4440
})

storage/framework/actions/src/UserStoreOrmAction.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default new Action({
66
name: 'User Store',
77
description: 'User Store ORM Action',
88
method: 'POST',
9+
requestFile: 'UserRequest',
910
async handle(request: UserRequestType) {
1011
await request.validate()
1112

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -307,21 +307,27 @@ export class Router implements RouterInterface {
307307
// if fails, return validation error
308308
let requestInstance
309309

310+
console.log(actionModule.default)
311+
310312
if (actionModule.default.requestFile) {
311313
requestInstance = await findRequestInstance(actionModule.default.requestFile)
312314
}
313315
else {
314-
requestInstance = await extractDefaultRequest(modulePath)
316+
requestInstance = await extractDefaultRequest()
315317
}
316318

319+
317320
try {
318321
if (isObjectNotEmpty(actionModule.default.validations))
319322
await customValidate(actionModule.default.validations, requestInstance.all())
320323

321324
return await actionModule.default.handle(requestInstance)
322325
}
323326
catch (error: any) {
324-
return { status: error.status, errors: error.errors }
327+
if (!error.status)
328+
return { status: 500, errors: error }
329+
330+
return { status: error.status, errors: error }
325331
}
326332
}
327333

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

+30-5
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,17 @@ type CallbackWithStatus = Route['callback'] & { status: number }
137137
async function execute(foundRoute: Route, req: Request, { statusCode }: Options) {
138138
const foundCallback: CallbackWithStatus = await route.resolveCallback(foundRoute.callback)
139139

140+
// return new Response(`<html><body><h1>Error</h1><p>${foundCallback}</p><pre></pre></body></html>`, {
141+
// headers: {
142+
// 'Content-Type': 'text/html',
143+
// 'Access-Control-Allow-Origin': '*',
144+
// 'Access-Control-Allow-Headers': '*',
145+
// },
146+
// status: 500,
147+
// })
148+
140149
const middlewarePayload = await executeMiddleware(foundRoute)
150+
141151

142152
if (
143153
middlewarePayload !== null
@@ -239,7 +249,7 @@ async function execute(foundRoute: Route, req: Request, { statusCode }: Options)
239249

240250
if (isObject(foundCallback) && foundCallback.status) {
241251
if (foundCallback.status === 401) {
242-
const { status, ...rest } = foundCallback
252+
const { status, ...rest } = await foundCallback
243253

244254
return new Response(JSON.stringify(rest), {
245255
headers: {
@@ -251,8 +261,22 @@ async function execute(foundRoute: Route, req: Request, { statusCode }: Options)
251261
})
252262
}
253263

264+
if (foundCallback.status === 404) {
265+
const { status, ...rest } = await foundCallback
266+
267+
const { errors } = rest
268+
return new Response(JSON.stringify(errors), {
269+
headers: {
270+
'Content-Type': 'application/json',
271+
'Access-Control-Allow-Origin': '*',
272+
'Access-Control-Allow-Headers': '*',
273+
},
274+
status: 404,
275+
})
276+
}
277+
254278
if (foundCallback.status === 403) {
255-
const { status, ...rest } = foundCallback
279+
const { status, ...rest } = await foundCallback
256280

257281
return new Response(JSON.stringify(rest), {
258282
headers: {
@@ -265,7 +289,7 @@ async function execute(foundRoute: Route, req: Request, { statusCode }: Options)
265289
}
266290

267291
if (foundCallback.status === 422) {
268-
const { status, ...rest } = foundCallback
292+
const { status, ...rest } = await foundCallback
269293

270294
return new Response(JSON.stringify(rest), {
271295
headers: {
@@ -278,9 +302,10 @@ async function execute(foundRoute: Route, req: Request, { statusCode }: Options)
278302
}
279303

280304
if (foundCallback.status === 500) {
281-
const { status, ...rest } = foundCallback
305+
const { status, ...rest } = await foundCallback
282306

283-
return new Response(JSON.stringify(rest), {
307+
const { errors } = rest
308+
return new Response(`<html><body><h1>Error</h1><p>${errors}</p><pre></pre></body></html>`, {
284309
headers: {
285310
'Content-Type': 'application/json',
286311
'Access-Control-Allow-Origin': '*',

storage/framework/orm/src/models/User.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export class UserModel {
438438
const model = await this.query.selectAll().executeTakeFirst()
439439

440440
if (model === undefined)
441-
throw new Error(JSON.stringify({ status: 404, message: 'No model results found for query' }))
441+
throw { status: 404, message: 'No model results found for query' }
442442

443443
return this.parseResult(new UserModel(model))
444444
}

0 commit comments

Comments
 (0)