koajs / koa

Expressive middleware for node.js using ES2017 async functions
https://koajs.com
MIT License
35.22k stars 3.23k forks source link

Koa2: setting ctx.status prior to ctx.redirect call does not yield any effect. #857

Closed gnrlbzik closed 7 years ago

gnrlbzik commented 7 years ago

Is this a bug?

per documentation https://github.com/koajs/koa/blob/v2.x/docs/api/response.md#responseredirecturl-alt

if i set ctx.status prior ctx.redirect call my response status should be set to what ever is i specify it to, but this does not happen in my case. I have middle wear that redirects visitor for any path that does not fall under exception if they do not have authenticated session.

        if (authRequired && !ctx.isAuthenticated()) {
            ctx.status = authConfig.authRequiredRedirect.statusCode;
            ctx.redirect(authConfig.authRequiredRedirect.path);
        }

image

Am I missing something?

jonathanong commented 7 years ago

do you mind making a PR with a failing test case?

gnrlbzik commented 7 years ago

I will after i test few more things.

gnrlbzik commented 7 years ago

@jonathanong is there a guide how collaboration should be done?

or this is good enough?

jonathanong commented 7 years ago

yes, this a great example of making a failing test-case :)

however, i'm not sure why you're using redirect() to set a non-redirect status code. you might as well just set the location header

gnrlbzik commented 7 years ago

Maybe i am doing this wrong. Possibly my understanding of this subject is off. let me consult web for a bit.

Original goal was to respond to none-auth user with 401 and redirect them to login page.

gnrlbzik commented 7 years ago

yeah, i am doing it wrong. i will close both : ) thanks for help