Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep original args of handler so we have access to the context #58

Open
rduque1 opened this issue Dec 13, 2022 · 4 comments
Open

Keep original args of handler so we have access to the context #58

rduque1 opened this issue Dec 13, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@rduque1
Copy link

rduque1 commented Dec 13, 2022

In order to have the next context object could

Can this line:
https://github.com/j0lv3r4/next-csrf/blob/91774aca0b271512f69976fce1fbc8681a7ca229/src/middleware/setup.ts#L49

be changed to:

return handler(...args)
@j0lvera j0lvera self-assigned this Mar 29, 2023
@j0lvera j0lvera added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Mar 29, 2023
@machadolucas
Copy link

To anyone needing to access the context, I solved this by using the following initialization configuration:

import {nextCsrf} from "next-csrf";

const { csrf, setup : setupFramework } = nextCsrf({
    secret: process.env.CSRF_SECRET,
});

// Hack to allow the use of next-csrf with getServerSideProps, making the context available
const setup = (
    handler: any,
) => async (context: any) => {
    setupFramework(context);
    return handler(context);
};

export { csrf, setup };

@BranislavLazic
Copy link

BranislavLazic commented May 21, 2023

@machadolucas That example is not working. It will not set XSRF-TOKEN cookie and you will get a 403 status code.

@machadolucas
Copy link

@machadolucas That example is not working. It will not set XSRF-TOKEN cookie and you will get a 403 status code.

It does work with me. Have you also added the setup in the page? For example:

// Import setup from your config
import {setup} from "../util/yourCsrfConfig";

export const getServerSideProps = setup(async (context: GetServerSidePropsContext) => {
    // ...
});

@DerekWolfie
Copy link

Still doesn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants