HasAToken
Generate simple/complex/readable tokens in Rails models.
Installation
Add this line to your application's Gemfile:
gem 'has_a_token'
And then execute:
$ bundle
Usage
Charsets include: :unambiguous
, :alphabetical
, and the default urlsafe_base64
Basic usage:
# Setup your model
class User < ActiveRecord::Base
include HasAToken::Concern
# ...
has_a_token :invite_code, charset: :unambiguous # generates an easily readable token
has_a_token :password_reset, length: 32 # generates a 32 character secure token
# ...
# You can use the generate_* methods to fetch a token when records are created
before_create :generate_invite_code
before_save :generate_password_reset_token, if: "password_reset_token.nil?"
# ...
end
# Reset a token
user = User
user.reset_password_reset_token! # creates a new token and persists the changes
Contributing
- Fork it ( https://github.com/[my-github-username]/has_a_token/fork )
- 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 a new Pull Request
Tests
You can run the tests using the rake test
command.