设计和认证与CURL! [英] Devise and Authentication with CURL !

查看:144
本文介绍了设计和认证与CURL!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过卷曲风格进行验证!

I would like to be able to authenticate through a curl style !

所以我想在登录时获得令牌:

So I would like to get a Token when I signed in :

curl -X POST 'http://localhost:3000/users/sign_in.json' -d 'user[email]=em...@provider.us&user[password]=pass' 

但是如果我执行这个操作,我只能得到:

But if I perform this action I only get :

{"email":"em...@provider.us","name":"Name of the user"} 

我如何添加一些特定的字段,如authentication_token?

How I could add some specific fields like authentication_token ?

我想做的是对吗?有没有其他更好的方法来做
这个?

Is what I want to do is right ? Is there any other better way to do this ?

谢谢!

推荐答案

我创建了一个名为SessionController的控制器

I created a Controller named SessionController

class Users::SessionsController < Devise::SessionsController
  append_view_path 'app/views/devise'
    def create
      respond_to do |format|
        format.html { super }
        format.json {
          warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
          current_user.ensure_authentication_token!
          render :json => {:user => current_user.api_attributes, :auth_token => current_user.authentication_token}.to_json, :status => :ok
        }
      end
    end

    def destroy
      respond_to do |format|
        format.html { super }
        format.json {
          warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
          current_user.empty_authentication_token!
          render :json => {}.to_json, :status => :ok
        }
      end
    end
end

并将此添加到根文件:

devise_for :users, :controllers => { :sessions => "users/sessions" }

它的工作原理: - )

And it works :-)

这篇关于设计和认证与CURL!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆