Commit 24980223672ff9eaa383ddfc5bfd7704db078746
Exists in
master
and in
1 other branch
Merge pull request #581 from arthurnn/devise_update
Update devise to 3.1.1, and remove token_authenticatable module
Showing
5 changed files
with
31 additions
and
8 deletions
Show diff stats
Gemfile.lock
| @@ -94,7 +94,7 @@ GEM | @@ -94,7 +94,7 @@ GEM | ||
| 94 | debugger-linecache (1.2.0) | 94 | debugger-linecache (1.2.0) |
| 95 | debugger-ruby_core_source (1.2.3) | 95 | debugger-ruby_core_source (1.2.3) |
| 96 | decent_exposure (2.3.0) | 96 | decent_exposure (2.3.0) |
| 97 | - devise (3.1.0) | 97 | + devise (3.1.1) |
| 98 | bcrypt-ruby (~> 3.0) | 98 | bcrypt-ruby (~> 3.0) |
| 99 | orm_adapter (~> 0.1) | 99 | orm_adapter (~> 0.1) |
| 100 | railties (>= 3.2.6, < 5) | 100 | railties (>= 3.2.6, < 5) |
| @@ -187,7 +187,7 @@ GEM | @@ -187,7 +187,7 @@ GEM | ||
| 187 | rails (>= 3.2.0) | 187 | rails (>= 3.2.0) |
| 188 | railties (>= 3.2.0) | 188 | railties (>= 3.2.0) |
| 189 | moped (1.5.1) | 189 | moped (1.5.1) |
| 190 | - multi_json (1.8.0) | 190 | + multi_json (1.8.1) |
| 191 | multi_xml (0.5.5) | 191 | multi_xml (0.5.5) |
| 192 | multipart-post (1.2.0) | 192 | multipart-post (1.2.0) |
| 193 | net-scp (1.1.2) | 193 | net-scp (1.1.2) |
app/controllers/application_controller.rb
| 1 | class ApplicationController < ActionController::Base | 1 | class ApplicationController < ActionController::Base |
| 2 | protect_from_forgery | 2 | protect_from_forgery |
| 3 | 3 | ||
| 4 | + before_filter :authenticate_user_from_token! | ||
| 4 | before_filter :authenticate_user! | 5 | before_filter :authenticate_user! |
| 5 | before_filter :set_time_zone | 6 | before_filter :set_time_zone |
| 6 | 7 | ||
| @@ -45,4 +46,12 @@ protected | @@ -45,4 +46,12 @@ protected | ||
| 45 | Time.zone = current_user.time_zone if user_signed_in? | 46 | Time.zone = current_user.time_zone if user_signed_in? |
| 46 | end | 47 | end |
| 47 | 48 | ||
| 49 | + def authenticate_user_from_token! | ||
| 50 | + user_token = params[User.token_authentication_key].presence | ||
| 51 | + user = user_token && User.find_by(authentication_token: user_token) | ||
| 52 | + | ||
| 53 | + if user | ||
| 54 | + sign_in user, store: false | ||
| 55 | + end | ||
| 56 | + end | ||
| 48 | end | 57 | end |
app/models/user.rb
| @@ -34,6 +34,7 @@ class User | @@ -34,6 +34,7 @@ class User | ||
| 34 | ### Token_authenticatable | 34 | ### Token_authenticatable |
| 35 | field :authentication_token, :type => String | 35 | field :authentication_token, :type => String |
| 36 | 36 | ||
| 37 | + index :authentication_token => 1 | ||
| 37 | 38 | ||
| 38 | before_save :ensure_authentication_token | 39 | before_save :ensure_authentication_token |
| 39 | 40 | ||
| @@ -78,5 +79,22 @@ class User | @@ -78,5 +79,22 @@ class User | ||
| 78 | self[:github_login] = login | 79 | self[:github_login] = login |
| 79 | end | 80 | end |
| 80 | 81 | ||
| 81 | -end | 82 | + def ensure_authentication_token |
| 83 | + if authentication_token.blank? | ||
| 84 | + self.authentication_token = generate_authentication_token | ||
| 85 | + end | ||
| 86 | + end | ||
| 87 | + | ||
| 88 | + def self.token_authentication_key | ||
| 89 | + :auth_token | ||
| 90 | + end | ||
| 82 | 91 | ||
| 92 | + private | ||
| 93 | + | ||
| 94 | + def generate_authentication_token | ||
| 95 | + loop do | ||
| 96 | + token = Devise.friendly_token | ||
| 97 | + break token unless User.where(authentication_token: token).first | ||
| 98 | + end | ||
| 99 | + end | ||
| 100 | +end |
config/initializers/_load_config.rb
| @@ -53,7 +53,7 @@ unless defined?(Errbit::Config) | @@ -53,7 +53,7 @@ unless defined?(Errbit::Config) | ||
| 53 | # Set default devise modules | 53 | # Set default devise modules |
| 54 | Errbit::Config.devise_modules = [:database_authenticatable, | 54 | Errbit::Config.devise_modules = [:database_authenticatable, |
| 55 | :recoverable, :rememberable, :trackable, | 55 | :recoverable, :rememberable, :trackable, |
| 56 | - :validatable, :token_authenticatable, :omniauthable] | 56 | + :validatable, :omniauthable] |
| 57 | end | 57 | end |
| 58 | 58 | ||
| 59 | # Set default settings from config.example.yml if key is missing from config.yml | 59 | # Set default settings from config.example.yml if key is missing from config.yml |
config/initializers/devise.rb
| @@ -185,10 +185,6 @@ Devise.setup do |config| | @@ -185,10 +185,6 @@ Devise.setup do |config| | ||
| 185 | # Require the `devise-encryptable` gem when using anything other than bcrypt | 185 | # Require the `devise-encryptable` gem when using anything other than bcrypt |
| 186 | # config.encryptor = :sha512 | 186 | # config.encryptor = :sha512 |
| 187 | 187 | ||
| 188 | - # ==> Configuration for :token_authenticatable | ||
| 189 | - # Defines name of the authentication token params key | ||
| 190 | - config.token_authentication_key = :auth_token | ||
| 191 | - | ||
| 192 | # ==> Scopes configuration | 188 | # ==> Scopes configuration |
| 193 | # Turn scoped views on. Before rendering "sessions/new", it will first check for | 189 | # Turn scoped views on. Before rendering "sessions/new", it will first check for |
| 194 | # "users/sessions/new". It's turned off by default because it's slower if you | 190 | # "users/sessions/new". It's turned off by default because it's slower if you |