Skip to content

Commit

Permalink
[IMPORTANT] Wipe out old repo and replace with new Ember structure
Browse files Browse the repository at this point in the history
  • Loading branch information
denniscdmc committed Mar 29, 2024
1 parent d109464 commit 39eea33
Show file tree
Hide file tree
Showing 48 changed files with 10,886 additions and 447 deletions.
20 changes: 0 additions & 20 deletions Brocfile.js

This file was deleted.

25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# How To Contribute

## Installation

* `git clone <repository-url>`
* `cd rails-csrf`
* `yarn install`

## Linting

* `yarn lint`
* `yarn lint:fix`

## Running tests

* `ember test` – Runs the test suite on the current Ember version
* `ember test --server` – Runs the test suite in "watch mode"
* `ember try:each` – Runs the test suite against multiple Ember versions

## Running the dummy application

* `ember serve`
* Visit the dummy application at [http://localhost:4200](http://localhost:4200).

For more information on using ember-cli, visit [https://cli.emberjs.com/release/](https://cli.emberjs.com/release/).
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 changes: 39 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,60 @@

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

## Usage

* `npm install rails-csrf --save`
* In `app.js` add load initializers
## Compatibility

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


## Installation

```js
loadInitializers(App, 'rails-csrf');
```
ember install rails-csrf
```


* Add a before model to your application route so your token is
fetched automatically.
## Usage

In your app/adapters/application.js (make one if you don't have one)
```js
export default Ember.Route.extend({
beforeModel: function() {
return this.csrf.fetchToken();
}
});
@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
};
}
```

## Config
By default `rails-csrf` does a get request to `/api/csrf`, if you
want to customize the end-point use `setCsrfUrl` on app.js

In your app/routes/application.js
```js
import { setCsrfUrl } from 'rails-csrf/config';
@service csrfService;

setCsrfUrl('/api/your/own/endpoint');
...
loadInitializers(App, 'rails-csrf');
async beforeModel() {
return this.csrfService.fetchToken();
},
```

## Returning CSRF-token from Rails

The following controller will return the required payload to get
everything working.
If you use Pretender and want to mock CSRF in your specs, in tests/helpers/index.js
```js
import mockCsrf from 'rails-csrf/utils/mock-csrf-pretender';

```ruby
class Api::CsrfController < ApplicationController
def index
render json: { request_forgery_protection_token => form_authenticity_token }.to_json
end
end
hooks.beforeEach(function () {
this.pretender = new Pretender();
mockCsrf(this.pretender);
});
```

Add route

```
namespace :api do
get :csrf, to: 'csrf#index'
end
```
## Contributing

See the [Contributing](CONTRIBUTING.md) guide for details.


## License
rails-csrf is [MIT Licensed](https://github.com/abuiles/rails-csrf/blob/master/LICENSE).

This project is licensed under the [MIT License](LICENSE.md).
15 changes: 0 additions & 15 deletions addon/config.js

This file was deleted.

11 changes: 0 additions & 11 deletions addon/index.js

This file was deleted.

11 changes: 0 additions & 11 deletions addon/initializers/csrf.js

This file was deleted.

53 changes: 0 additions & 53 deletions addon/service.js

This file was deleted.

22 changes: 22 additions & 0 deletions addon/services/csrf-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Service from '@ember/service';
import { isEmpty } from '@ember/utils';
import fetch from 'fetch';

export default class CsrfService extends Service {
fetchToken() {
// See if the token is already in the meta tag:
// - if so (we are in Rails/3000), simply set it to this class.
// - if not (we are in 4200), fetch it from Rails and set it after it returns.
let token = $('meta[name="csrf-token"]').attr('content');
if (!isEmpty(token)) {
this.csrfToken = token;
}
else {
fetch('/api/csrf').then((response) => {
response.json().then((json) => {
this.csrfToken = json.authenticity_token;
});
})
}
}
}
7 changes: 7 additions & 0 deletions addon/utils/mock-csrf-pretender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function mockCsrf(pretenderServer) {
pretenderServer.get('/api/csrf', function() {
return [200, {"Content-Type": "application/json"}, JSON.stringify({
"authenticity_token": "token"
})];
});
}
1 change: 1 addition & 0 deletions app/services/csrf-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'rails-csrf/services/csrf-service';
1 change: 1 addition & 0 deletions app/utils/mock-csrf-pretender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'rails-csrf/utils/mock-csrf-pretender';
17 changes: 0 additions & 17 deletions bower.json

This file was deleted.

5 changes: 0 additions & 5 deletions config/environment.js

This file was deleted.

25 changes: 25 additions & 0 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');

module.exports = function (defaults) {
const app = new EmberAddon(defaults, {
// Add options here
});

/*
This build file specifies the options for the dummy test app of this
addon, located in `/tests/dummy`
This build file does *not* influence how the addon or the app using it
behave. You most likely want to be modifying `./index.js` or app's build file
*/

const { maybeEmbroider } = require('@embroider/test-setup');
return maybeEmbroider(app, {
skipBabel: [
{
package: 'qunit',
},
],
});
};
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = {
name: 'rails-csrf'
name: require('./package').name,
};
Loading

0 comments on commit 39eea33

Please sign in to comment.