N
Nuxt7mo ago
bnason

Express compatible request object

Is there an Express compatible request object available from a server route? I am trying to integrate samlify and it needs to parse the request but it seems the nitro request object is incompatible.
10 Replies
manniL
manniL7mo ago
you can try using event.node.req but it'll go away in the next major or toNodeMiddleware with samlify
bnason
bnasonOP7mo ago
yea I'm using event.node.req right now. Is toNodeMiddleware avaiable in v3.12 with v4 compatibilityVersion set? const { extract } = await serviceProvider.parseLoginResponse(identityProvider, 'post', event.node.req) aaah maybe this will work: https://nuxt.com/docs/guide/directory-structure/server#legacy-handler-or-middleware
manniL
manniL7mo ago
Yes it should be Ah sorry yes From not to 👀
bnason
bnasonOP7mo ago
Ok so it looks like the error I'm getting is because request.body is undefined
manniL
manniL7mo ago
Did you read the body beforehand?
bnason
bnasonOP7mo ago
no I haven't does fromNodeMiddleware parse the body or do I need to install the express bodyParser somehow?
manniL
manniL7mo ago
If you would need it with samlify maybe (Never used that lib) How did you try using it with fromNodeMiddleware?
bnason
bnasonOP7mo ago
Same code as above: const { extract } = await serviceProvider.parseLoginResponse(identityProvider, 'post', request) its expecting request.body to exist
manniL
manniL7mo ago
Can you show the whole code?
bnason
bnasonOP7mo ago
import jwt from 'jsonwebtoken'

import { identityProvider, serviceProvider } from '~/server/utils/saml'

export default fromNodeMiddleware(async (request, response) => {
try {
const { extract } = await serviceProvider.parseLoginResponse(identityProvider, 'post', request)
const { session } = extract.attributes

const cookie = jwt.sign(session, process.env.SESSION_SECRET || 'foobar', { expiresIn: '1h' })

// setCookie(event, 'SESSION', cookie)
} catch (e) {
console.error('[FATAL] when parsing login response sent from IdP', e)
}
})
import jwt from 'jsonwebtoken'

import { identityProvider, serviceProvider } from '~/server/utils/saml'

export default fromNodeMiddleware(async (request, response) => {
try {
const { extract } = await serviceProvider.parseLoginResponse(identityProvider, 'post', request)
const { session } = extract.attributes

const cookie = jwt.sign(session, process.env.SESSION_SECRET || 'foobar', { expiresIn: '1h' })

// setCookie(event, 'SESSION', cookie)
} catch (e) {
console.error('[FATAL] when parsing login response sent from IdP', e)
}
})
hmm i bet its the async nope =\ I even installed bodyParser as a server middleware but that just gives me an empty object {} instead of undefined export default fromNodeMiddleware(bodyParser.text()) Weird, even using a normal event handler, the body is { '{}': '' }
export default defineEventHandler(async (event) => {
const body = await readBody(event)
console.log(JSON.stringify(body))
})
export default defineEventHandler(async (event) => {
const body = await readBody(event)
console.log(JSON.stringify(body))
})
ok I don't know why it's looking at the body... the damn thing is urlencoded! aha! export default fromNodeMiddleware(bodyParser.urlencoded({ extended: false })) Now to get that body parser only on that route
Want results from more Discord servers?
Add your server