【问题标题】:Using Tokens with Username/Password Combo使用带有用户名/密码组合的令牌
【发布时间】:2014-12-24 05:14:58
【问题描述】:

我正在尝试找出一种将令牌和用户名/密码组合集成到 Rails API 中的方法。 API 的前端是一个 iOS 应用程序,需要用户名/密码组合来跟踪用户的个人资料。

thoughtbot/ios-on-rails github repo 和thoughtbot's iOS on Rails book 之后,我能够通过使用设备令牌来处理身份验证来创建用户模型。

但是,正如thoughtbot 团队所指出的:

我们的应用不需要用户名/密码登录,相反,我们将 在应用程序的第一次运行时创建一个用户对象,然后始终如一地 以该用户的身份签署我们的请求。此行为对于应用程序很有用 不需要登录,或者有某种访客模式。

以下是此类基于令牌的身份验证 API 的迁移文件:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.timestamps null: false
      t.string :device_token
    end

    add_index :users, :device_token
  end
end

在我的例子中,我正在构建一个 iOS 应用程序,它需要在客户端上使用用户名/密码登录,我需要在 rails 中创建数据库模式来处理这个问题。以下是我对构建它的想法:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.timestamps null: false
      t.string :first_name, null: false
      t.string :last_name, null: false
    end
  end
end

class CreateTokens < ActiveRecord::Migration
  def change
    create_table :tokens do |t|
      t.timestamps null: false
      t.string :device_token
    end
  end
end

具有以下型号:

class User < ActiveRecord::Base
  has_many :tokens
end

class Token < ActiveRecord::Base
  belongs_to :user
end

我在正确的轨道上?请指教。

【问题讨论】:

    标签: ios ruby-on-rails api


    【解决方案1】:

    看起来不错,可能想要使用名为 authentication_tokens 的表名,如下面的帖子所示

    http://www.brianauton.com/posts/token-authentication-devise.html

    【讨论】:

      猜你喜欢
      • 2018-01-23
      • 2011-12-02
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 2015-03-09
      • 2019-12-25
      • 1970-01-01
      • 2021-11-27
      相关资源
      最近更新 更多