I have the following code...
export class OtherApplication{
constructor(){
this.app = new Koa();
this.app.use((ctx, next)=> {
console.log(ctx.request.path);
if(ctx.request.path === "/callback") next();
else ctx.redirect(`https:\/\/${AUTH_DOMAIN}/authorize?response_type=code&client_id=${AUTH_CLIENT_ID}&redirect_uri=${AUTH_REDIRECT}`);
})
this.hosted = OtherApplication.getHostedApp();
this.app.use(mount(this.hosted));
this.options = {
key: fs.readFileSync(KEY_FILE),
cert: fs.readFileSync(CERT_FILE)
}
this.server = http2.createSecureServer(this.options, this.app.callback());
this.server.on("error", (err) => console.error(err));
}
// TODO: Make own class
static getHostedApp(){
const app = new Koa();
const router = new Router();
router.get("/", function(ctx){
ctx.render('index');
})
router.get("/test",function(ctx){
ctx.body = "Test"
});
router.get("/callback",(ctx, next)=>{
const data = {
"grant_type":"authorization_code",
"client_id":AUTH_CLIENT_ID,
"code": ctx.request.query.code,
"redirect_uri": AUTH_REDIRECT
}
const options = {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: qs.stringify(data),
url: `https:\/\/${AUTH_DOMAIN}/oauth/token`,
};
axios(options).then((response)=>{
console.log("Made it here " + JSON.stringify(jwtDecode(response.data.id_token)));
ctx.body = "Test";
})
});
app.use(router.routes());
return app;
}
start(){
const listenAsync = promisify(this.server.listen.bind( this.server ));
return listenAsync(PORT);
}
}
When I run and successfully sign in it throws a 404 with no serverside errors. The console log is correct so I am not sure why it is throwing a 404.
If I replace (This one does not work!!!!)
axios(options).then((response)=>{
console.log("Made it here " + JSON.stringify(jwtDecode(response.data.id_token)));
ctx.body = "Test";
})
With (This one works!!!!)
Promise.resolve("").then((response)=>{
ctx.body = "Test";
})
So why is Axios causing Koa to throw a 404? I also tried doing it as an "async" function but that didn't help either, same exact response.
you need to return the promise in a Koa middleware otherwise it resolves and any "un-returned" promise will run out of context.
app.use(ctx => {
return Promise.resolve().then(() => {
ctx.body = 'test'
})
})
That didn't help :-( Same 404
Oops wrong button
I also tried redirect and I still get a 404
return axios(options).then((response)=>{
console.log("Made it here " + JSON.stringify(jwtDecode(response.data.id_token)));
ctx.cookies.set("foo", "bar", {httpOnly: false});
ctx.redirect("/");
})
Unfortunately, I need more information. Remove all unnecessary code and provide a minimal reproducible example and I can take a look at it.
However, you probably have a middleware where you call next without returning it.
I think you need to read up on how promises work because this isn't a Koa issue.
Using finally seemed to work for me...
return axios(options).then((response)=>{
console.log("Made it here " + JSON.stringify(jwtDecode(response.data.id_token)));
ctx.cookies.set("Authorization", JSON.stringify(response.data), {httpOnly: false})
}).finally(()=>{
ctx.redirect("/");
return next();
})
Still not sure why
because in this example you're returning the promise from next(), which you didn't in your original post. I still recommend you take a look at a promise tutorial.
Again, you're just pasting excerpts of your code which makes it impossible for me to actually debug/try. I can only head parse what little I see.
From the last example I can only conclude that axios is throwing, and finally runs in both cases of a promise resolve and a reject.
I also tried redirect and I still get a 404
return axios(options).then((response)=>{ console.log("Made it here " + JSON.stringify(jwtDecode(response.data.id_token))); ctx.cookies.set("foo", "bar", {httpOnly: false}); ctx.redirect("/"); })You didn't response
try
return axios(options).then((response)=>{
console.log("Made it here " + JSON.stringify(jwtDecode(response.data.id_token)));
ctx.body = "Test";
})