Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I authenticate using Devise in a Rails REST API App?

I'm creating a Rails app which have both a GUI part, and a REST/JSON-API.

The REST/JSON API is fairly simple, and the controller returns data like this:

def get_players
    @players = Player.all
    render json: @players
end

The GUI part of the app is using Devise for authentication, and it works fine.

Now I want to add authentication for the REST/JSON Api too, how do I do that?

Also, how do I test the REST API using curl when the Authentication is added?

---- edit ----

as it turns out, Devise wasnt necessary in this case. A home-cooked token-authentication method works for now. (token created when Player is created, and returned on correct e-mail/password combo).

like image 734
Peter Andersson Avatar asked Sep 08 '16 18:09

Peter Andersson


1 Answers

After getting a few tips, I found a number of great sites. There are several ways to do this, however I don't know which one is best, but these sites help a long way:

  • https://github.com/lynndylanhurley/devise_token_auth (An extension to Devise)
  • https://labs.kollegorna.se/blog/2015/04/build-an-api-now/ (Manual way)
  • Token based authentication for Rails JSON APIs (SO Question)
  • Rails API : Best way to implement authentication? (SO Question)
  • Rails API : Best way to implement authentication?
like image 125
Peter Andersson Avatar answered Oct 12 '22 01:10

Peter Andersson