All Projects → LaunchPadLab → token-master

LaunchPadLab / token-master

Licence: MIT License
Minimal and Simple user management for Ruby and Rails applications.

Programming Languages

ruby
36898 projects - #4 most used programming language
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to token-master

Angular Token
🔑 Token based authentication service for Angular with interceptor and multi-user support. Works best with devise token auth for Rails. Example:
Stars: ✭ 376 (+1988.89%)
Mutual labels:  devise, token
jwt-token
Json web token generation and validation.
Stars: ✭ 14 (-22.22%)
Mutual labels:  token
Invite-Manager
Invite manager is an open-source discord bot that allows you to track the invites of people who join your server.
Stars: ✭ 90 (+400%)
Mutual labels:  invite
axios-token-interceptor
An interceptor which makes it easier to work with tokens in axios.
Stars: ✭ 34 (+88.89%)
Mutual labels:  token
limestone-accounts
Boilerplate Rails 5.2 multitenant SaaS application with webpack and Docker integration. Billing is scoped to accounts.
Stars: ✭ 97 (+438.89%)
Mutual labels:  devise
EL1T3
🖤 Ƭ𝘩𝘦 𝘮𝘰𝘴𝘵 𝘱𝘰𝘸𝘦𝘳𝘧𝘶𝘭𝘭 𝘢𝘯𝘥 𝘉𝘦𝘵𝘵𝘦𝘳 𝘵𝘰𝘬𝘦𝘯 𝘴𝘵𝘦𝘢𝘭𝘦𝘳.
Stars: ✭ 41 (+127.78%)
Mutual labels:  token
XGoServer
一个基础性、模块完整且安全可靠的轻量级 Go 服务端框架
Stars: ✭ 21 (+16.67%)
Mutual labels:  token
node-uid-generator
Generates cryptographically strong pseudo-random UIDs with custom size and base-encoding
Stars: ✭ 21 (+16.67%)
Mutual labels:  token
jwtauth-plugin
JWTAuth Plugin for WinterCMS
Stars: ✭ 25 (+38.89%)
Mutual labels:  token
AspNetCore.Weixin
An ASP.NET Core middleware for Wechat/Weixin message handling and apis. (微信公众平台/接口调用服务)
Stars: ✭ 24 (+33.33%)
Mutual labels:  token
airflow-user-management-plugin
A plugin for Apache Airflow that allows you to manage the users that can login
Stars: ✭ 13 (-27.78%)
Mutual labels:  user-management
crowdsale-smart-contract
No description or website provided.
Stars: ✭ 39 (+116.67%)
Mutual labels:  token
tokenizr
String Tokenization Library for JavaScript
Stars: ✭ 70 (+288.89%)
Mutual labels:  token
adminetic
Admin starter kit with user, role and permission, activity, settings and preference management along with CRUD, ACL, BREAD Permission, Repo Pattern, SuperAdmin Generator
Stars: ✭ 19 (+5.56%)
Mutual labels:  user-management
NEMPay
Adaptable Android & iOS Mosaic Wallet for NEM Blockchain
Stars: ✭ 36 (+100%)
Mutual labels:  token
yii2-jwt-user
JWT (JSON Web Token) User component for Yii 2
Stars: ✭ 16 (-11.11%)
Mutual labels:  token
tokensubscription.com
⏰💰🤠 Set-it-and-forget-it token subscriptions on the Ethereum mainnet. #Winner #WyoHackathon
Stars: ✭ 81 (+350%)
Mutual labels:  token
mobile-message
基于移动端的弹窗组件,默认提供info、success、warning、error、alert、confirm、multiple、vertical、bottomSheet、prompt,可自定义弹窗。它可以包含任何Html内容可以自定义弹窗的样式,也可以加入自定以的弹窗动画。
Stars: ✭ 13 (-27.78%)
Mutual labels:  confirm
Razor.SweetAlert2
A Razor class library for interacting with SweetAlert2
Stars: ✭ 98 (+444.44%)
Mutual labels:  confirm
reactjs-login-register-crud
ReactJS CRUD Application, ReactJS FileUpload, ReactJS Sample application, ReactJS Boilerplate, ReactJS Login, ReactJS FileUpload, ReactJS Register
Stars: ✭ 47 (+161.11%)
Mutual labels:  token

Token Master Logo

Token Master

GitHub Documentation Inline docs Gem Version Build Status Test Coverage License

Simple token logic for providing (temporary) restricted access. No routing, views, controllers, or mailers, just logic that you can use wherever and whenever you want.

Tokens can be used for any action that needs the access, such as inviting, confirming, or resetting passwords. These actions can be considered tokenable actions.

Tokenable actions can be attributed to any model, not just users. These models then become tokenable models.

Quick Start

Installation

Add this line to your application's Gemfile:

gem 'token_master'

And then execute:

$ bundle

Or install it yourself as:

$ gem install token_master

Usage

These examples assume Rails 5, but anything >= 4 will work

Let's say you want to add email confirmation flow to your User. Your tokenable model then is the User model, and the tokenable action might be something like confirm (although you can name it anything, as long as you are consistent).

  1. Create and run a migration to add the necessary columns to the users table like so:
bundle exec rails generate token_master User confirm
bundle exec rails db:migrate
  1. Add the Token Master token_master hook to the User class, and pass in the symbol for your tokenable action:
class User < ApplicationRecord
  token_master :confirm
end
  1. Somewhere during the signup flow, generate and send the token:
class UsersController < ApplicationController

  def create

    # Creating the user is up to you, here is an example
    user = User.create!(
      email: params[:email],
      password: params[:password],
      password_confirmation: params[:password_confirmation]
    )

    # Generate and save a unique token on the new user
    token = user.set_confirm_token!

    # Mark the token as sent
    user.send_confirm_instructions! do
      # Sending the email is up to you, by passing a block here:
      UserMailer.send_confirm(user) # or some other logic
    end
  end

  def resend_confirmation_instructions

    # if you have a 'resend instructions?' flow you can generate a new token and send instructions again in one step
    user.resend_confirm_instructions! do
      # Sending the email is up to you, by passing a block here:
      UserMailer.send_confirm(user) # or some other logic
    end
  end

end
  1. Somewhere during the confirmation flow, find and confirm the user:
class UsersController < ApplicationController
  def confirm

    # finds the user by the token, and mark the token as completed
    user = User.confirm_by_token!(params[:token])

    ...

  end
end

Details

Let's revisit the Quick Start and fill in the details.

The Generator

When you ran the generator

bundle exec rails generate token_master User confirm

you provided two arguments:

  • User - The class name of the model to which you are adding the tokenable action
  • confirm - The name of the tokenable action

Both of these could be anything, as long as you use the same class and name later on. If you like, you can create multiple tokenables at the same time, just add more space-separated tokenable names when calling the generator:

bundle exec rails generate token_master User confirm invite reset

Running the generator does two things:

  1. Creates a migration file in #{Rails.root}/db/migrate that looks like:
class AddConfirmTokenableToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :confirm_token,        :string,    default: nil
    add_column :users, :confirm_created_at,   :timestamp, default: nil
    add_column :users, :confirm_completed_at, :timestamp, default: nil
    add_column :users, :confirm_sent_at,      :timestamp, default: nil

    add_index :users, :confirm_token
  end
end

where the :users table is determined from the User argument and :confirm_* is determined from the confirm argument.

  1. Creates an initializer #{Rails.root}/config/initializers/token_master.rb that looks like:
TokenMaster.config do |config|
  # Set up your configurations for each *tokenable* using the methods at the bottom of this file.
  # Example: For `confirm` logic:
  #
  # Default values:
  #   token_lifetime  = 15 # days
  #   required_params = []
  #   token_length    = 20 # characters

  config.add_tokenable_options :confirm,
    token_lifetime:  15, # days
    required_params: [:email],
    token_length:    30 # characters
end

The default values will be used unless you configure them otherwise. These options can be set for each tokenable action.

The Model

When you added the Token Master hook and tokenable action to your model

class User < ApplicationRecord
  token_master :confirm
end

just make sure the class User and tokenable(s) :confirm (this can be multiple tokenables) match what you used in your generator.

Ex.

token_master :confirm, :invite, :reset
  1. The token_master hook is included automatically by Token Master in your ApplicationRecord base class.

However, if necessary, you can add this yourself by including the following in your class:

include TokenMaster::Model

This adds the token_master class method we used above, and you can make the same calls we described in the confirm example above.

  1. When you call the token_master class method, for each tokenable action you provide, a handful of methods are added to the class for each tokenable action, and named accordingly.

Assuming the tokenable action below is confirm, the methods would look like this:

Instance methods

  • set_confirm_token!
  • send_confirm_instructions!
  • resend_confirm_instructions!
  • confirm_status
  • force_confirm!

Class methods

  • confirm_by_token!

In addition to the three you have already seen in action, there is also:

confirm_status - returns the current status of the tokenable action. This is one of:

  • 'no token'
  • 'created'
  • 'sent'
  • 'completed'
  • 'expired'

force_confirm! - forcibly completes the given tokenable action

See the Api Docs for more details.

Advanced

Sometimes in order to redeem a token, we want to make sure some additional information is present and possibly save that to our model. For example, when implementing a password reset flow, we want to update the User with the new password and make sure it's valid.

Assuming we are using has_secure_password or something similar all we need to do is:

  1. Configure the tokenable action to require these fields when redeeming the token

../initializers/token_master.rb

TokenMaster.config do |config|
  config.add_tokenable_options :reset_password,
    token_lifetime:  1
    required_params: [:password, :password_confirmation]
    token_length:    30
end
  1. Include those parameters when redeeming the token (If you don't you will get an error!)
User.reset_password_by_token!(
  token,
  password: password,
  password_confirmation: password_confirmation
)

Under the hood, Token Master calls update! on the model, so if the model is not valid, it won't be saved and the token will not be redeemed.

FAQ

Can I use this without Rails?

Yes! However, there is a small dependency on ActiveRecord, see below.

Can I use this without ActiveRecord?

Almost! There is only a slight dependence on a few ActiveRecord methods and its on our radar to refactor this a bit. In the meantime, a workaround is to make sure the class you are using implements update, update!, save, and find_by. In addition, you have to either add Token Master to your class with include TokenMaster::Model or use the Token Master core module explicitly:

TokenMaster::Core.set_token!(User, :confirm) (which is equivalent to user.set_confirm_token!(token))

See the Api Docs for more details.

Who is Launchpad Lab?

We are product builders, check us out at Launchpad Lab

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/LaunchpadLab/token-master.

License

The gem is available as open source under the terms of the MIT License.


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].