routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

koa server always responds 404

Open mingyang91 opened this issue 9 years ago • 7 comments

In koa async controller function, if I set @Authroized() , server always responds "404 Not Found"

I think this code Incorrect

const handleError = (result: any) => {
    if (!result) {
        let error = actionMetadata.authorizedRoles.length === 0 ? new AuthorizationRequiredError(action) : new AccessDeniedError(action);
        return this.handleError(error, actionMetadata, action);
    } else {
        next();
    }
};

if (isPromiseLike(checkResult)) {
    checkResult.then(result => handleError(result));
} else {
    handleError(checkResult);
}

https://github.com/pleerock/routing-controllers/blob/master/src/driver/koa/KoaDriver.ts#L101-L108

it should be modified as follows.

const handleError = (result: any) => {
    if (!result) {
        let error = actionMetadata.authorizedRoles.length === 0 ? new AuthorizationRequiredError(action) : new AccessDeniedError(action);
        return this.handleError(error, actionMetadata, action);
    } else {
        return next();
    }
};

if (isPromiseLike(checkResult)) {
    return checkResult.then(result => handleError(result));
} else {
    return Promise.resolve(handleError(checkResult));
}

https://github.com/mingyang91/routing-controllers/blob/master/src/driver/koa/KoaDriver.ts#L107-L114

mingyang91 avatar Jul 13 '17 11:07 mingyang91

Could you make a PR and test the changes? 😉

MichalLytek avatar Jul 13 '17 13:07 MichalLytek

Same is happening for me using koa@2 with a non-async controller action decorated with @Authorized(). The response gets sent back with a 404 status before the controller action even runs.

smithdalec avatar Jul 21 '17 19:07 smithdalec

@mingyang91 Will you find some time to make a PR as you described the problem and found the fix? https://github.com/mingyang91/routing-controllers/commit/38a68e6fa1ef73439c8ca7c40225608a9be62aa3#diff-112df671cfc9c4c162793234f846b6d0R112

MichalLytek avatar Aug 06 '17 14:08 MichalLytek

sure, i will make a pr in next week.

mingyang91 avatar Aug 06 '17 15:08 mingyang91

This is fixed in #240. Will be released in 0.7.2.

NoNameProvided avatar Aug 07 '17 09:08 NoNameProvided

Ok, so I'm closing this issue. @mingyang91 feel free to reopen if the issue will still exist in 0.7.2.

MichalLytek avatar Aug 07 '17 09:08 MichalLytek

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Dec 15 '20 00:12 github-actions[bot]