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

Invalid CSRF Token #6

Closed
lasha94 opened this issue Jan 13, 2023 · 3 comments
Closed

Invalid CSRF Token #6

lasha94 opened this issue Jan 13, 2023 · 3 comments

Comments

@lasha94
Copy link

lasha94 commented Jan 13, 2023

Hello, im using your package, when i try with postman it works but if send requests from angular im getting Invalid CSRF Token

@huy97
Copy link
Owner

huy97 commented Jan 13, 2023

Hi @lasha94 ,
First thanks for using my library. I'm really sorry by issue you have, but please check for solution here.
My library using cookie to store a secret key, it default name is _csrf, so your request need auto attach this cookie before sending to server.
With axios you can enable mode withCredentials: true to attach cookie automatically. I have a same solution for angular here.
Finally, let's talk to me if the issue is still unresolved, many thanks

@lasha94
Copy link
Author

lasha94 commented Jan 13, 2023

I have already added it. This is my script.

  async getToken() {
    return await firstValueFrom(this.appService.getToken());
  }

  async checkCsrf() {
    const csrfToken = await this.getToken();
    // console.log(csrfToken);
    const resp = await firstValueFrom(this.appService.checkCsrf(csrfToken));
    // console.log(resp);
  }

When i make get request to http://localhost:3000/api/token it doesnt add cookie, and when i make post request im getting {"statusCode":403,"message":"CSRF token not found"} then it adds cookie, and after that im getting this on every request {"statusCode":403,"message":"Invalid CSRF Token"}

@lasha94
Copy link
Author

lasha94 commented Jan 14, 2023

I thought it withCredentials: true needed only for post request. I added it to getToken request too and now it works. Thank you. Dependencies need update, package doesnt work on latest nest project.

@lasha94 lasha94 closed this as completed Jan 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants