Home.md - wordjelly/Rails_TkAuth GitHub Wiki

Initialize the rails app

rails new RailsTkAuth --skip-active-record

GemFile

add the following dependencies to the GemFile

gem 'mongoid', '5.0.1'
gem 'bson_ext'
gem 'devise'
gem "omniauth-google-oauth2"
gem "omniauth-facebook"
gem 'materialize-sass', '0.97.1'
gem 'devise_token_auth', :git => "https://github.com/wordjelly/devise_token_auth.git"
gem 'rack-cors', :require => 'rack/cors'

##Changes to Mongoid and Mongoid COnfig need to make a change in the mongoid gem, to include a serialize method.

def self.serialize(*args)

end

now run from the command line

rails g mongoid:config

##Devise generators

now we need to run the devise generators

rails g devise:install
rails g devise User

##devise_token_auth configuration

Command_line

rails g devise_token_auth:install User /auth

/app/controllers/application_controller.rb

protect_from_forgery with: :null_session

/config/application.rb

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

At the end of all this, making a request to authenticate a user from an external domain, with auth token should work

Manually add the following line to the User model All the devise basic modules are added in the User concern in the devise_token_auth gem. You can add confirmable and others in the User model itself, like usual for devise.

include DeviseTokenAuth::Concerns::User

Now there is a route conflict since, native devise routes and devise_token_auth routes use the same names. So we make this change to the routes.rb file.

namespace :api do
    scope :v1 do
      mount_devise_token_auth_for 'User', at: 'auth'
    end
  end