Skip to content

Commit

Permalink
Move devise modules to individual classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jartek committed Dec 15, 2014
1 parent ff4725d commit 2d398d0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create
end

# success redirect url is required
if DeviseTokenAuth.modules.include?(:confirmable) && !params[:confirm_success_url]
if resource_class.devise_modules.include?(:confirmable) && !params[:confirm_success_url]
return render json: {
status: 'error',
data: @resource,
Expand All @@ -29,12 +29,14 @@ def create
# override email confirmation, must be sent manually from ctrl
resource_class.skip_callback("create", :after, :send_on_create_confirmation_instructions)
if @resource.save

unless @resource.confirmed?
# user will require email authentication
@resource.send_confirmation_instructions({
client_config: params[:config_name],
redirect_url: params[:confirm_success_url]
})

else
# email auth has been bypassed, authenticate user
@client_id = SecureRandom.urlsafe_base64(nil, false)
Expand Down Expand Up @@ -74,6 +76,7 @@ def create

def update
if @resource

if @resource.update_attributes(account_update_params)
render json: {
status: 'success',
Expand Down
11 changes: 7 additions & 4 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ module DeviseTokenAuth::Concerns::User
extend ActiveSupport::Concern

included do
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise *DeviseTokenAuth.modules
# Hack to check if devise is already enabled
unless self.method_defined?(:devise_modules)
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
end

serialize :tokens, JSON

Expand Down Expand Up @@ -185,7 +188,7 @@ def extend_batch_buffer(token, client_id)
end

def confirmed?
DeviseTokenAuth.modules.exclude?(:confirmable) || super
self.devise_modules.exclude?(:confirmable) || super
end

protected
Expand Down
19 changes: 1 addition & 18 deletions lib/devise_token_auth/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,14 @@ class Engine < ::Rails::Engine
mattr_accessor :change_headers_on_each_request,
:token_lifespan,
:batch_request_buffer_throttle,
:omniauth_prefix,
:excluded_modules
:omniauth_prefix

self.change_headers_on_each_request = true
self.token_lifespan = 2.weeks
self.batch_request_buffer_throttle = 5.seconds
self.omniauth_prefix = '/omniauth'
self.excluded_modules = []

def self.setup(&block)
yield self
end

def self.modules
available_modules = [
:database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:trackable,
:validatable,
:confirmable,
:omniauthable
]

available_modules - excluded_modules
end
end
4 changes: 4 additions & 0 deletions lib/generators/devise_token_auth/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def create_user_model
inclusion = "include DeviseTokenAuth::Concerns::User"
unless parse_file_for_line(fname, inclusion)
inject_into_file fname, after: "class #{user_class} < ActiveRecord::Base\n" do <<-'RUBY'
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
include DeviseTokenAuth::Concerns::User
RUBY
end
Expand Down
4 changes: 4 additions & 0 deletions lib/generators/devise_token_auth/templates/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class <%= user_class %> < ActiveRecord::Base
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
include DeviseTokenAuth::Concerns::User
end

0 comments on commit 2d398d0

Please sign in to comment.