Skip to content

Commit

Permalink
Merge pull request errbit#581 from arthurnn/devise_update
Browse files Browse the repository at this point in the history
Update devise to 3.1.1, and remove token_authenticatable module
  • Loading branch information
shingara committed Oct 14, 2013
2 parents e1eba9d + 7d2dd9d commit 2498022
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ GEM
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.2.3)
decent_exposure (2.3.0)
devise (3.1.0)
devise (3.1.1)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
Expand Down Expand Up @@ -187,7 +187,7 @@ GEM
rails (>= 3.2.0)
railties (>= 3.2.0)
moped (1.5.1)
multi_json (1.8.0)
multi_json (1.8.1)
multi_xml (0.5.5)
multipart-post (1.2.0)
net-scp (1.1.2)
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ApplicationController < ActionController::Base
protect_from_forgery

before_filter :authenticate_user_from_token!
before_filter :authenticate_user!
before_filter :set_time_zone

Expand Down Expand Up @@ -45,4 +46,12 @@ def set_time_zone
Time.zone = current_user.time_zone if user_signed_in?
end

def authenticate_user_from_token!
user_token = params[User.token_authentication_key].presence
user = user_token && User.find_by(authentication_token: user_token)

if user
sign_in user, store: false
end
end
end
20 changes: 19 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class User
### Token_authenticatable
field :authentication_token, :type => String

index :authentication_token => 1

before_save :ensure_authentication_token

Expand Down Expand Up @@ -78,5 +79,22 @@ def github_login=(login)
self[:github_login] = login
end

end
def ensure_authentication_token
if authentication_token.blank?
self.authentication_token = generate_authentication_token
end
end

def self.token_authentication_key
:auth_token
end

private

def generate_authentication_token
loop do
token = Devise.friendly_token
break token unless User.where(authentication_token: token).first
end
end
end
2 changes: 1 addition & 1 deletion config/initializers/_load_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
# Set default devise modules
Errbit::Config.devise_modules = [:database_authenticatable,
:recoverable, :rememberable, :trackable,
:validatable, :token_authenticatable, :omniauthable]
:validatable, :omniauthable]
end

# Set default settings from config.example.yml if key is missing from config.yml
Expand Down
4 changes: 0 additions & 4 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@
# Require the `devise-encryptable` gem when using anything other than bcrypt
# config.encryptor = :sha512

# ==> Configuration for :token_authenticatable
# Defines name of the authentication token params key
config.token_authentication_key = :auth_token

# ==> Scopes configuration
# Turn scoped views on. Before rendering "sessions/new", it will first check for
# "users/sessions/new". It's turned off by default because it's slower if you
Expand Down

0 comments on commit 2498022

Please sign in to comment.