Add UID support to Devise. A lot of times, we want a unique ID representing the user model instead of its incremental ID in the database, for example, in API instead of exposing the primary key, we use a random generated unique string to indentify this user.
Add this line to your application's Gemfile:
gem 'devise_uid'
And then execute:
$ bundle
Add devise_uid to any of your Devise models using the following generator:
rails generate devise_uid MODEL
Replace MODEL with the class name you want to add devise_uid. This will add the :uid flag to your model's Devise modules. The generator will also create a migration file. Currently only ActiveRecord is supported.
Add :uid
to the devise
call in your model:
class User < ActiveRecord
devise :database_authenticable, :confirmable, :uid
end
Add uid
field to your Devise model migration:
class AddUidToUser< ActiveRecord::Migration
add_column :users, :uid, :string
add_index :users, :uid, :unique => true
end
An uid is generated when a Devise MODEL is created. Access it like this:
user = User.create(email: "foo@bar.com")
puts user.uid # => "V8aS9tucNzKyH39d4Bpq"
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request