Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding token_authenticatable to devise with migration

I have already created a User Model using devise, but I now want to add support for token_authenticable, so I need to migrate these additions. Is the following correct, and what type should token_authenticatable be?

class AddAuthenticationTokenToUser < ActiveRecord::Migration

  def change

    add_column :users, :token_authenticatable
    add_index  :users, :authentication_token, :unique => true

  end

end
like image 600
Undistraction Avatar asked Jan 12 '12 20:01

Undistraction


3 Answers

From the devise 2.0 generator (line 74) on Github:

# t.string :authentication_token

If you are going to be looking up a user based on their token, then adding an index is a good idea.

Here is the devise 1.5 file

like image 189
Gazler Avatar answered Nov 15 '22 21:11

Gazler


add_column :users, :token_authenticatable, :string

Don't forget to add devise :token_authenticatable to your user model.

like image 33
Nick Colgan Avatar answered Nov 15 '22 21:11

Nick Colgan


The full list of deleted helpers for migrations in devise 2 is as follows:

create_table(TABLE_NAME) do |t|
  ## Database authenticatable
  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""

  ## Recoverable
  t.string   :reset_password_token
  t.datetime :reset_password_sent_at

  ## Rememberable
  t.datetime :remember_created_at

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

  ## Encryptable
  # t.string :password_salt

  ## Confirmable
  # t.string   :confirmation_token
  # t.datetime :confirmed_at
  # t.datetime :confirmation_sent_at
  # t.string   :unconfirmed_email # Only if using reconfirmable

  ## Lockable
  # t.integer  :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
  # t.string   :unlock_token # Only if unlock strategy is :email or :both
  # t.datetime :locked_at

  # Token authenticatable
  # t.string :authentication_token

  ## Invitable
  # t.string :invitation_token

  t.timestamps
end

Taken from the devise wiki

like image 29
Alter Lagos Avatar answered Nov 15 '22 20:11

Alter Lagos