【问题标题】:PhoneGap Mobile Rails Authentication (devise? authentication from scratch?)PhoneGap Mobile Rails 身份验证(设计?从头开始身份验证?)
【发布时间】:2012-11-29 10:21:49
【问题描述】:

我有一个带有 Rails 后端的 PhoneGap 应用。我正在尝试找出使用 json 从移动应用程序验证用户的最佳方法。

我目前正在使用设计,但我不必使用它。修改设计以在 Phonegap 中使用移动应用程序的最简单方法是什么?

我知道有很多关于这方面的帖子......但是,其中一些已经过时或者看起来像是非常复杂的黑客攻击。希望可以从一些久经考验的项目或教程中获得更多最新信息。

我发现的一篇文章也建议使用 jsonp,但它似乎也是一个非常复杂的 hack。你可以在这里找到它:http://vimeo.com/18763953

我还想知道我是否最好从头开始进行身份验证,如以下 Railscast 中所述:http://railscasts.com/episodes/250-authentication-from-scratch

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby cordova devise


    【解决方案1】:

    您应该覆盖设计的 sessionsregistrations 控制器。我只会向您展示如何覆盖会话控制器:

    首先,转到您的 User 模型并添加 Token Authenticatable 模块。像这样的:

    devise :token_authenticatable
    
    before_save :ensure_authentication_token
    

    然后编辑您的 devise.rb 文件以配置该模块:

    # You can skip storage for :http_auth and :token_auth by adding those symbols to the array below.
    config.skip_session_storage = [:token_auth]
    
    # Defines name of the authentication token params key
    config.token_authentication_key = :auth_token
    

    现在编辑您的路线并指向您的新控制器:

    devise_for :users, :controllers => { :registrations => 'registrations', :sessions => 'sessions' }
    

    然后像这样创建你的控制器:

    class SessionsController < Devise::SessionsController
      def create
        respond_to do |format|
          format.html {
            super
          }
          format.json {
            build_resource
            user = User.find_for_database_authentication(:email => params[:user][:email])
            return invalid_login_attempt unless resource
    
            if user.valid_password?(params[:user][:password])
              render :json => { :auth_token => user.authentication_token }, success: true, status: :created
            else
              invalid_login_attempt
            end
          }
        end
      end
    
      def destroy
        respond_to do |format|
          format.html {
            super
          }
          format.json {
            user = User.find_by_authentication_token(params[:auth_token])
            if user
              user.reset_authentication_token!
              render :json => { :message => 'Session deleted.' }, :success => true, :status => 204
            else
              render :json => { :message => 'Invalid token.' }, :status => 404
            end
          }
        end
      end
    
      protected
      def invalid_login_attempt
        warden.custom_failure!
        render json: { success: false, message: 'Error with your login or password' }, status: 401
      end
    end
    

    设计has a page about this,但它只指向一些已经过时的指南。但也许它会对你有所帮助。

    【讨论】:

    • +1 谢谢。你对在移动端存储会话了解多少?我听说 phonegap 没有 cookie,那么这是否意味着我必须使用 localStorage 来存储 cookie,然后以某种方式在每个请求中发布它?
    • btw @Ashitaka,你说得对,那些文档有点过时了。我真的很惊讶这种设置没有更多记录,因为移动应用程序和 Rails 似乎是这些天的趋势。
    • 只搜索本地存储,超级简单。在您的 Phonegap 应用程序中,登录时,对会话 url 进行 ajax 调用(您可以使用 rake routes 找到它),然后使用以下内容存储令牌:localStorage.setItem('auth_token', authentication_token);。然后您可以使用localStorage.getItem('auth_token') 检索它。
    • 一个月前,我和几个朋友使用 Devise 和 Phonegap 做了一个大学项目。你可以看一看,但要持保留态度。这是我们第一次使用 Phonegap,所以它不是一个漂亮的应用程序,但是嘿,我们完成了工作。这里是 the server,这里是 the client。希望对您有所帮助!
    • 嘿@Ashitaka 非常感谢!我会看看然后回复你:)
    猜你喜欢
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2018-04-15
    • 2011-02-21
    • 2020-08-28
    • 2015-09-09
    • 2015-02-15
    相关资源
    最近更新 更多