如何在Rails REST API应用程序中使用Devise进行身份验证?

时间:2021-10-20 15:27:23

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

我正在创建一个包含GUI部分和REST / JSON-API的Rails应用程序。

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

REST / JSON API非常简单,控制器返回如下数据:

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.

应用程序的GUI部分使用Devise进行身份验证,它工作正常。

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

现在我想为REST / JSON Api添加身份验证,我该怎么做?

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

此外,如何在添加身份验证时使用curl测试REST API?

---- 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).

事实证明,在这种情况下,Devise不是必需的。家庭烹饪的令牌认证方法现在有效。 (创建播放器时创建的令牌,并使用正确的电子邮件/密码组合返回)。

1 个解决方案

#1


1  

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:

得到一些提示后,我发现了许多很棒的网站。有几种方法可以做到这一点,但我不知道哪一种最好,但这些网站有很长的路要走:

#1


1  

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:

得到一些提示后,我发现了许多很棒的网站。有几种方法可以做到这一点,但我不知道哪一种最好,但这些网站有很长的路要走: