Coder Social home page Coder Social logo

cartedo / grape_devise_token_auth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from muhammadn/grape_devise_token_auth

0.0 2.0 0.0 30 KB

Grape compatibility for devise_token_auth + devise + rails setup

License: MIT License

Ruby 98.90% Shell 1.10%

grape_devise_token_auth's Introduction

GrapeDeviseTokenAuth

GrapeDeviseTokenAuth gem is a compatibility layer between devise_token_auth and grape. It is useful when mounting a grape API in a rails application where devise (or devise_token_auth + devise) is already present. It is reliant on devise_token_auth and devise, therefore it is not suitable for grape where these are not present. If you are looking for a pure grape solution, you should check out grape_token_auth.

The majority of the hard work and credit goes to Lyann Dylan Hurley and his fantastic devise_token_auth gem. I merely have ported this to work well with grape.

Installation

Add this line to your application's Gemfile:

gem 'grape_devise_token_auth'

And then execute:

$ bundle

Or install it yourself as:

$ gem install grape_devise_token_auth

Usage

Place this line in an initializer in your rails app or at least somewhere before the grape API will get loaded:

GrapeDeviseTokenAuth.setup!

Within the Grape API:

class Posts < Grape::API
  auth :grape_devise_token_auth, resource_class: :user

  helpers GrapeDeviseTokenAuth::AuthHelpers

  # ...
end

The resource class option allows you to specific the scope that will be authenticated, this corresponds to your devise mapping.

Individual endpoints can now be authenticated by calling authenticate_YOUR_MAPPING_HERE! (e.g. authenticate_user!) within them.

For Example:

get '/' do
  authenticate_user!
  present Post.all
end

alternatively to protect all routes place the call in a before block:

before do
  authenticate_user!
end

There is also a authenticate_user version of this helper (notice that it lacks of exclamation mark) that doen't fail nor returns 401.

A full example setup can be found here

Note about ignoring existing warden users

If you are having issues with users persisting across logins or you do not want to integrate with devise, you can disable the integration with devise during setup as so:

GrapeDeviseTokenAuth.setup! do |config|
  config.ignore_existing_warden_users = true
end

Testing and Example

Currently I am using this repo to test this gem, eventually I plan on migrating the tests into the grape_devise_token_auth repo. For now though, I refer you to that repo for how to integrate with an existing devise and devise_token_auth repo.

Development

After checking out the repo, run bin/setup to install dependencies. Then, 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 to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/grape_devise_token_auth/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

grape_devise_token_auth's People

Contributors

mcordell avatar muhammadn avatar cmolina avatar

Watchers

James Cloos avatar John Peebles avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.