使用Devise注册,登录并使用iOS应用程序登录 [英] Using Devise to sign up, sign in and sign out with an iOS app

查看:94
本文介绍了使用Devise注册,登录并使用iOS应用程序登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的Rails应用程序,使用Devise进行用户注册,登录等。我想将Devise公开到iOS应用程序。有很多线程是高级别的解释,但我是Rails的新手。我基本上是向用户/ sign_up发出请求,如下所示:

I have a very basic Rails app that uses Devise for user sign up, sign in, etc. I'd like to expose Devise to an iOS app. There's a lot of threads that are high level explanations, but I'm very new to Rails. I'm basically posting a request to users/sign_up like so:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://localhost:5000"]];
//[client defaultValueForHeader:@"Accept"];
[client setParameterEncoding:AFJSONParameterEncoding];
NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
                                                            @"first.last@gmail.com",
                                                            @"testtest",
                                                            @"testtest",
                                                            nil]
                                                   forKeys:[NSArray arrayWithObjects:
                                                            @"email",
                                                            @"password",
                                                            @"password_confirmation",
                                                            nil]];

[client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
NSURLRequest *request = [client requestWithMethod:@"GET" path:@"/users/sign_up" parameters:params];
AFHTTPRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"%@", JSON);
    NSLog(@"%@", [response allHeaderFields]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];

[operation setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html", nil]];
[operation start];

这似乎在iOS端正在经历,因为我收到200 OK:

This seems to be going through on the iOS side as I receive a 200 OK:

   2013-01-21 13:38:00.412 Starfish[35773:c07] I 
restkit.network:RKHTTPRequestOperation.m:152 GET 'http://localhost:5000/signup?password=testtest&password_confirmation=testtest&email=first.last%40gmail.com'
2013-01-21 13:38:01.073 Starfish[35773:c07] (null)
2013-01-21 13:38:01.073 Starfish[35773:7207] I restkit.network:RKHTTPRequestOperation.m:179 GET 'http://localhost:5000/signup?password=testtest&password_confirmation=testtest&email=first.last%40gmail.com' (200 OK) [0.6611 s]
2013-01-21 13:38:01.074 Starfish[35773:c07] {
    "Cache-Control" = "max-age=0, private, must-revalidate";
    Connection = close;
    "Content-Type" = "text/html; charset=utf-8";
    Etag = "\"5dfe88700e41a015a8f524850446e327\"";
    Server = "thin 1.5.0 codename Knife";
    "Set-Cookie" = "_geo-photo_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTEyZTJjMjFkZjAwZGZlMWIzMTJmNTg5ODNkNjM5OGI0BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTN4eUZGUzVhb1pIdXc1QlJJUnBxSFVrZmYvUDdpeGtaeklxNGhOejFOeEk9BjsARg%3D%3D--5130edc939facbe7004570141c069d40f5f7feca; path=/; HttpOnly";
    "X-Request-Id" = a2480798afed4e7aca17f3f1e2d3d44d;
    "X-Runtime" = "0.219984";
    "X-UA-Compatible" = "IE=Edge";
}

在Rails端:

Started GET "/signup?password=[FILTERED]&password_confirmation=[FILTERED]&email=first.last%40gmail.com" for 127.0.0.1 at 2013-01-21 13:38:00 -0800
13:43:15 web.1  | Processing by Devise::RegistrationsController#new as */*
13:43:15 web.1  |   Parameters: {"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"first.last@gmail.com"}
13:43:15 web.1  |   Rendered devise/shared/_links.erb (2.1ms)
13:43:15 web.1  |   Rendered devise/registrations/new.html.erb within layouts/application (23.7ms)
13:43:15 web.1  | Completed 200 OK in 142ms (Views: 64.9ms | ActiveRecord: 6.9ms)

-box注册:

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>

我不知道的Rails足以解决的问题是,我不想任何提交的视图。我只想将注册或登录凭据作为JSON对象传递,并接收用户创建或登录的验证。我上面所提到的不是创建用户,因为它试图渲染视图。任何帮助将不胜感激。谢谢。

The problem, which I don't know Rails enough to solve, is that I don't want any views rendered. I simply want to pass the sign up or login credentials as JSON objects and receive a validation that the user was created or signed in. What I have above isn't creating the user because it's attempting to render a view. Any help would be greatly appreciated. Thanks.

推荐答案

我正在做一个类似的事情,并通过使用JSON的CURL将其注册部分工作到目前为止(应该易于转换为iOS),也可以通过浏览器。使用的修改来自 NeverBe 的此答案:

I'm doing a similar thing, and have the sign up part of it working so far via CURL using JSON (which should translate to iOS easily), and also via the browser. Using a modification of this answer from NeverBe:

我已修改 config / initializers / devise.rb 并添加了以下行:

I modified config/initializers/devise.rb and added the line:

config.navigational_formats = ["*/*", "/", :html, :json]






我修改了 config / routes.rb 并替换这一行:

devise_for :users

与此行:

devise_for :users, :controllers => {:registrations => "registrations"}






我创建了一个新文件 app / controllers / registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

  def create
    respond_to do |format|
      format.html {
        super
      }
      format.json {
        if params[:user][:email].nil?
          render :status => 400,
                 :json => {:message => 'User request must contain the user email.'}
          return
        elsif params[:user][:password].nil?
          render :status => 400,
                 :json => {:message => 'User request must contain the user password.'}
          return
        end

        if params[:user][:email]
          duplicate_user = User.find_by_email(params[:user][:email])
          unless duplicate_user.nil?
            render :status => 409,
                   :json => {:message => 'Duplicate email. A user already exists with that email address.'}
            return
          end
        end

        @user = User.create(params[:user])

        if @user.save
          render :json => {:user => @user}
        else
          render :status => 400,
                 :json => {:message => @user.errors.full_messages}
        end
      }
    end
  end
end

附加说明:即使没有所有自定义错误处理代码,您仍然会收到错误处理,但是我想返回状态代码错误(400和409)而不是200,其中包含自定义错误消息我从默认设计行为中获得了什么。

Additional note: You still get error handling even without all that custom error handling code, but I wanted to return status code errors (400 and 409) instead of 200 with custom error messages which is what I was getting from the default devise behavior.

这篇关于使用Devise注册,登录并使用iOS应用程序登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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