I have a legacy app which uses loopback 3 and exposes REST API and I wanna get my JWT token from the incoming requests from the clients. I wrote a hook for getting access to the req object to access the token.
Object.keys(app.dataSources).forEach((name: string) => {
if (camelCase(name) === name) {
app.dataSources[name].connector.observe('before execute', (ctx, next) => {
if (!ctx.req.uri) return next();
Object.keys(ctx.req).forEach(function(key) {
console.log(key, ctx.req[key]);
});
});
}
});
Route definition
accepts: [
{arg: 'codeValue', type: 'string', required: false, http: {source: 'query'}},
{arg: 'codeId', type: 'string', required: false, http: {source: 'query'}}
]
The output when the above hook executes
method GET
uri http://localhost:8010/api/v1/code/100
qs { codeValue: '' } <<== Issue 1
json true
form undefined
headers undefined <<== Issue 2
timeout undefined
Following are issues as highlighted above:
- Even though I have specified query param codeId, it is not mapped in req object
- The headers in the req is coming as undefined
Curl command
curl -H "codeId: TS01" -H "codeValue: 'TS 01" http://localhost:8081/api/v1/code/100
We have plans to move to loopback to version 4, but is a long term process. Any pointers is highly appreciated.