[英]Problem with create authenticate api using grape in ruby on rails
我想使用葡萄來驗證 API。 對於身份驗證,我使用了 Devise gem。 我嘗試將 devise::sessioncontroller 包含到我的葡萄 api 文件中,但它被忽略了。
class SignIn < BaseAPI
resource :sign_in do
desc 'Sign in page'
params do
requires :username, type: String
end
post do
User.authenticate(params)
end
end
end
試試下面的代碼。 您應該能夠進行身份驗證。 您需要設置一些額外的東西。 請按照此文檔了解更多詳細信息。
resource :sign_in do
desc "Authenticate user"
params do
requires :login, type: String
requires :password, type: String
end
post :login do
user = User.find_by_email(params[:login].downcase)
if user && user.authenticate(params[:password])
token = TokenGenerator.create(user_id: user.id)
{token: token.access_token}
else
error!('Unauthorized.', 401)
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.