Skip to content

Instantly share code, notes, and snippets.

@hadrienblanc
Created April 12, 2019 12:35
Show Gist options
  • Save hadrienblanc/1763b13bde20ab73eb3eb2a7b009052d to your computer and use it in GitHub Desktop.
Save hadrienblanc/1763b13bde20ab73eb3eb2a7b009052d to your computer and use it in GitHub Desktop.
devise_token_auth getting-started

Devise

Gemfile

gem 'devise'
gem 'devise_token_auth'

(bundle install)

Terminal :

rails generate devise:install
rails g devise_token_auth:install User /api/v1/auth (en vrai j’ai inversé avec le précédent 

rake db:migrate

gemfile

gem 'rack-cors', :require => 'rack/cors'

(Terminal : bundle install)

# config/application.rb
module YourApp
  class Application < Rails::Application
    config.middleware.use Rack::Cors do
      allow do
        origins '*'
        resource '*',
          headers: :any,
          expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
          methods: [:get, :post, :options, :delete, :put]
      end
    end
  end
end

Rpec

Gemfile :

group :development, :test do
  gem 'rspec-rails', '~> 3.8'
end

(Terminal : bundle install)

Terminal :

Download and install

$ bundle install

Generate boilerplate configuration files

(check the comments in each generated file for more information)

$ rails generate rspec:install create .rspec create spec create spec/spec_helper.rb create spec/rails_helper.rb

Gemfile :

gem 'factory_bot'

(Terminal : bundle install)

Add trackable info on user migration.

  ## Trackable
  t.add_column :sign_in_count, :integer, :default => 0
  t.add_column :current_sign_in_at, :datetime
  t.add_column :last_sign_in_at, :datetime
  t.add_column :current_sign_in_ip, :string
  t.add_column :last_sign_in_ip, :string

curl -XPOST -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000//api/v1/auth/sign_in -d '{"email" : "[email protected]", "password" : "1234567890"}'

Note: Unnecessary use of -X or --request, POST is already inferred.

  • Trying ::1...
  • TCP_NODELAY set
  • Connected to localhost (::1) port 3000 (#0)

POST //api/v1/auth/sign_in HTTP/1.1 Host: localhost:3000 User-Agent: curl/7.54.0 Content-Type: application/json Accept: application/json Content-Length: 64

  • upload completely sent off: 64 out of 64 bytes < HTTP/1.1 200 OK < Content-Type: application/json; charset=utf-8 < access-token: bXtTEGS3dy3way2eyomN5Q < token-type: Bearer < client: bNgC9Qrg6mVVTHJo7dYnqQ < expiry: 1556281938 < uid: [email protected] < Vary: Origin < ETag: W/"f52f88d5b501111e2a50ae6555e5fba1" < Cache-Control: max-age=0, private, must-revalidate < X-Request-Id: 1ebb9d3f-2c63-4841-8ecc-3a36fa91cdba < X-Runtime: 0.466860 < Transfer-Encoding: chunked <
  • Connection #0 to host localhost left intact {"data":{"id":1,"email":"[email protected]","provider":"email","uid":"[email protected]","allow_password_change":false,"name":null,"nickname":null,"image":null}}%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment