No commit activity in last 3 years
No release in over 3 years
HasSecureToken provides you an easily way to geneatre uniques random tokens for any model in ruby on rails. **SecureRandom::base58** is used to generate the 24-character unique token, so collisions are highly unlikely.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

< 5.0, >= 3.0
~> 1.3
>= 0

Runtime

 Project Readme

active_model_secure_token

Provides an easy way to generate uniques random tokens for any ActiveModel. SecureRandom::base58 is used to generate the 24-character unique tokens, so collisions are highly unlikely.

Note If you're worried about possible collissions, there's a way to generate a race condition in the database in the same way that validates_uniqueness_of can. You're encouraged to add an unique index in the database to deal

Installation

Add this line to your application's Gemfile:

gem 'active_model_secure_token'

And then run:

$ bundle

Or install it yourself as:

$ gem install active_model_secure_token

This gem does not depend on ActiveRecord, only on ActiveModel. It works fine with ActiveRecord and Mongoid. You will need to include ActiveModel::SecureToken in your model/document before you can use it.

Setting your ActiveRecord model

The first step is to generate a migration in order to add the token key field.

rails g migration AddTokenToUsers token:string
=>
   invoke  active_record
   create    db/migrate/20150424010931_add_token_to_users.rb

Then run rake db:migrate in order to update users table in the database. The next step is to add has_secure_token to the model:

# Schema: User(token:string, auth_token:string)
class User < ActiveRecord::Base
  include ActiveModel::SecureToken
  has_secure_token
end

user = User.new
user.save
user.token # => "pX27zsMN2ViQKta1bGfLmVJE"
user.regenerate_token # => true

To use a custom column to store the token key field you can specify the column_name option. See example above (e.g: auth_token):

# Schema: User(token:string, auth_token:string)
class User < ActiveRecord::Base
  include ActiveModel::SecureToken
  has_secure_token :auth_token
end

user = User.new
user.save
user.auth_token # => "pX27zsMN2ViQKta1bGfLmVJE"
user.regenerate_auth_token # => true

Setting your Mongoid document

Add the field, include this gem and enable it like so:

class User
  include Mongoid::Document
  include ActiveModel::SecureToken
  field :token, type: String
  has_secure_token
end

The Mongoid document has all the same methods as the ActiveRecord model.

Running tests

Running

$ rake test

Should return

8 runs, 14 assertions, 0 failures, 0 errors, 0 skips