0.0
No commit activity in last 3 years
No release in over 3 years
Use JSON Web Tokens (JWT) to authenticate with Challah
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.1.0
>= 12.3.3

Runtime

~> 1.4
~> 1.5
 Project Readme

Challah JWT

Authenticate your Challah users with JSON Web Tokens (JWT).

Installation

  1. Add this line to your application's Gemfile and then bundle install:
gem 'challah-jwt'
  1. In your Challah initializer, add the following line:
Challah.register_technique :jwt, Challah::Jwt::Technique
  1. Include the tokenizer concern in your user model:
class User < ApplicationRecord
  include Challah::Userable
  include Challah::Jwt::Tokenizer
end

Usage

You'll need to include the JWT in your sign in response, e.g.:

{
  "user": {
    "jwt": "adi8e98uie.saxbbbgudinocgeigc84y9834.8ui9odeion",
    "id": "1",
    "first_name": "Slick",
    "last_name": "McSpeedy",
  }
}

Send the JWT in the Authorization header like this:

GET /
Authorization: Bearer adi8e98uie.saxbbbgudinocgeigc84y9834.8ui9odeion

Tokenizer

Challah-JWT adds a few methods to your User model that make it easy to tokenize and look up users:

user = User.first
# => #<User id=1...>

# Convert the user to a JWT
jwt = user.to_jwt

# Look up user by JWT
user = User.find_by_jwt(jwt)

The tokenizer only includes the user's ID in the payload by deafult, to override this behavior, override the jwt_attrs method in your user model:

class User < ApplicationRecord
  include Challah::Jwt::Tokenizer
  
  def jwt_attrs
    # make sure you include id, otherwise the lookup will fail
    serializable_hash.slice("id", "email", "status")
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/philtr/challah-jwt.