-
Notifications
You must be signed in to change notification settings - Fork 1
Update adapters to use AuthPlugin service for authentication #591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,6 +106,42 @@ export class ObjectStackController { | |
| @All('auth/*') | ||
| async auth(@Req() req: any, @Res() res: any, @Body() body: any) { | ||
| try { | ||
| // Try AuthPlugin service first (preferred path) | ||
| const kernel = this.service.getKernel(); | ||
| const authService = typeof kernel.getService === 'function' | ||
| ? kernel.getService('auth') | ||
| : null; | ||
|
|
||
| if (authService && typeof authService.handleRequest === 'function') { | ||
| // Construct a Web standard Request from the Express/Fastify request | ||
| const protocol = req.protocol || 'http'; | ||
| const host = req.get?.('host') || req.headers?.host || 'localhost'; | ||
| const url = `${protocol}://${host}${req.originalUrl || req.url}`; | ||
| const headers = new Headers(); | ||
| if (req.headers) { | ||
| Object.entries(req.headers).forEach(([k, v]) => { | ||
| if (typeof v === 'string') headers.set(k, v); | ||
| else if (Array.isArray(v)) headers.set(k, v.join(', ')); | ||
| }); | ||
| } | ||
| const init: RequestInit = { method: req.method, headers }; | ||
| if (req.method !== 'GET' && req.method !== 'HEAD' && body) { | ||
| init.body = JSON.stringify(body); | ||
| if (!headers.has('content-type')) { | ||
| headers.set('content-type', 'application/json'); | ||
| } | ||
| } | ||
| const webRequest = new Request(url, init); | ||
| const response = await authService.handleRequest(webRequest); | ||
|
|
||
| // Convert Web Response to Express/Fastify response | ||
| res.status(response.status); | ||
| response.headers.forEach((v: string, k: string) => res.setHeader(k, v)); | ||
| const text = await response.text(); | ||
| return res.send(text); | ||
|
Comment on lines
+137
to
+141
|
||
| } | ||
|
|
||
| // Fallback to legacy dispatcher | ||
| const path = req.params[0] || req.url.split('/auth/')[1]?.split('?')[0] || ''; | ||
| const result = await this.service.dispatcher.handleAuth(path, req.method, body, { request: req, response: res }); | ||
| return this.normalizeResponse(result, res); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -61,6 +61,21 @@ export function createRouteHandler(options: NextAdapterOptions) { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // --- 1. Auth --- | ||||||||||||||||||||||||
| if (segments[0] === 'auth') { | ||||||||||||||||||||||||
| // Try AuthPlugin service first (preferred path) | ||||||||||||||||||||||||
| const authService = typeof options.kernel.getService === 'function' | ||||||||||||||||||||||||
| ? options.kernel.getService('auth') | ||||||||||||||||||||||||
| : null; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (authService && typeof authService.handleRequest === 'function') { | ||||||||||||||||||||||||
| const response = await authService.handleRequest(req); | ||||||||||||||||||||||||
| // Convert Web Response to NextResponse | ||||||||||||||||||||||||
| const body = await response.text(); | ||||||||||||||||||||||||
| const headers: Record<string, string> = {}; | ||||||||||||||||||||||||
| response.headers.forEach((v: string, k: string) => { headers[k] = v; }); | ||||||||||||||||||||||||
| return new NextResponse(body, { status: response.status, headers }); | ||||||||||||||||||||||||
|
Comment on lines
+71
to
+75
|
||||||||||||||||||||||||
| // Convert Web Response to NextResponse | |
| const body = await response.text(); | |
| const headers: Record<string, string> = {}; | |
| response.headers.forEach((v: string, k: string) => { headers[k] = v; }); | |
| return new NextResponse(body, { status: response.status, headers }); | |
| // Convert Web Response to NextResponse while preserving streaming body and multi-value headers | |
| const headers = new Headers(response.headers); | |
| return new NextResponse(response.body, { | |
| status: response.status, | |
| headers, | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same AuthPlugin-detection logic (checking
kernel.getService('auth')andhandleRequest) is now duplicated across adapters. To reduce drift, consider extracting a small shared helper (e.g., in@objectstack/runtimeor a local adapters util) that returns an auth handler ornull, and reuse it here.