Skip to content

Latest commit

 

History

History
61 lines (40 loc) · 1.02 KB

README.md

File metadata and controls

61 lines (40 loc) · 1.02 KB

rails-csrf

ember-cli addon to keep track of your Rails CSRF-token.

Compatibility

  • Ember.js v4.8 or above
  • Ember CLI v4.8 or above
  • Node.js v18 or above

Installation

ember install rails-csrf

Usage

In your app/adapters/application.js (make one if you don't have one)

@service csrfService;

// This makes sure that every single API request Ember makes passes in the CSRF token
get headers() {
  return {
    'X-CSRF-Token': this.csrfService.csrfToken
  };
}

In your app/routes/application.js

@service csrfService;

async beforeModel() {
  return this.csrfService.fetchToken();
},

If you use Pretender and want to mock CSRF in your specs, in tests/helpers/index.js

import mockCsrf from 'rails-csrf/utils/mock-csrf-pretender';

hooks.beforeEach(function () {
  this.pretender = new Pretender();
  mockCsrf(this.pretender);
});

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.