我正在开发一个rails web应用程序,它也为移动设备提供了基于JSON的API.移动客户端需要先使用(Email/PASS)获取令牌,然后客户端将使用该令牌进行后续API调用.

I am pretty new to Devise, and I am looking for a Devise API look like authenticate(email, pass) and expect it to return true/false, then based on that I will either create and hand back the token or return a decline message. but seems Devise doesn't provide something like this.

我知道Devise1.3提供了基于JSON的身份验证,但这与我需要的稍有不同-我需要生成令牌并处理回客户端,然后在身份验证完成后使用令牌.

谁能给点建议吗?

推荐答案

There is a devise configuration called :token_authenticatable. So if you add that to the devise method in your "user", then you can authenticate in your API just by calling

"/api/v1/recipes?qs=sweet&auth_token=[@user.auth_token]"

你可能也希望你的用户也能做到这一点:

before_save :ensure_authentication_token

UPDATE (with API authorization code)

The method you're looking for are:

resource = User.find_for_database_authentication(:login=>params[:user_login][:login])
resource.valid_password?(params[:user_login][:password])

这是我的gist with a full scale JSON/API login with devise美元

Json相关问答推荐

使用jq解析多层json

带有PowerShell内核的NewtonSoft json.Net的奇怪行为

将 REST API - json 输出转换为表 Power BI

Jolt Spec 跳过数组中的第一个元素

将不带正文的 CURL POST 转换为 RESTRequest Delphi 代码 / 为 Dropbox API /get_current_account 端点编写 Delphi 代码

如何使用Powershell查找所有包含特定键值对的JSON对象并迭代所有对象?

在Databricks中如何将JSON文件作为字典读取

Oracle Apex - 将 JSON 对象分配给变量以返回

Jolt - 在同一级别添加时组合值的问题

如何比较 JSON 文档并返回与 Jackson 或 Gson 的差异?

JSON RPC - 什么是id?

Laravel5 Json 获取文件内容

苗条的 JSON 输出

如何在java中比较来自JsonObject的空值

JSON日期到Java日期?

与classic 规范化表相比,postgres JSON 索引是否足够高效?

Spring restTemplate 获取原始 json 字符串

JSON JQ 如果没有 else

处理 HTTP 请求正文中的可选 JSON 字段

Java HashMap 与 JSONObject