How to use the @rails/ujs.csrfToken function in @rails/ujs

To help you get started, we’ve selected a few @rails/ujs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github connorshea / vglist / app / javascript / src / components / library-table.vue View on Github external
onDelete(row) {
      if (window.confirm(`Remove ${row.game.name} from your library?`)) {
        // Post a delete request to the game purchase endpoint to delete the game.
        fetch(row.url, {
          method: 'DELETE',
          headers: {
            'X-CSRF-Token': Rails.csrfToken(),
            Accept: 'application/json'
          },
          credentials: 'same-origin'
        }).then(response => {
          if (response.ok) {
            // Emit a delete event to force the parent library component to
            // refresh.
            this.$emit('delete');
          }
        });
      }
    },
    loadGames() {
github connorshea / vglist / app / javascript / src / components / add-game-to-library.vue View on Github external
removeGameFromLibrary() {
      let removeGameFromLibraryPath = `/games/${this.gameId}/remove_game_from_library`;

      fetch(removeGameFromLibraryPath, {
        method: 'DELETE',
        headers: {
          'X-CSRF-Token': Rails.csrfToken(),
          Accept: 'application/json'
        },
        credentials: 'same-origin'
      }).then(response => {
        if (response.ok) {
          Turbolinks.visit(window.location.href);
        }
      });
    },
    onSubmit() {
github connorshea / vglist / app / javascript / src / utils.ts View on Github external
static async rawAuthenticatedFetch(route: string, method: string, body: string|null = null): Promise {
    let requestBody : RequestInit = {
      method: method,
      headers: {
        'Content-Type': 'application/json',
        'X-CSRF-Token': Rails.csrfToken(),
        Accept: 'application/json'
      },
      credentials: 'same-origin'
    };

    if (body !== null) {
      requestBody.body = body;
    }

    return fetch(route, requestBody);
  }