【问题标题】:Persisting User between Web App and IOS AppWeb App 和 IOS App 之间的持久化用户
【发布时间】:2019-03-06 15:34:42
【问题描述】:

我有一个适用于桌面的应用程序,并为 IOS 和 ANDROID 使用 turbolinks 包装器。我在 ANDROID 上没有任何问题。但是在 IOS 上,如果用户退出桌面应用程序,用户必须在他/她转到我的 iphone 应用程序图标时重新登录(请参阅下面的完整步骤)。

我的问题是: - 是什么原因造成的? (奇怪不是ANDROID的问题) - 我需要什么/在哪里解决这个问题。 - 这是 Rails 方面的问题,turoblinks 问题还是 - 下面是 Rails 方面的代码。注意:我没有 IOS 应用程序代码 -(使用移动程序员帮助我)

我有一个应用程序使用- 1- Ruby on Rails 宝石“rails”、“5.2.2” 2- 创业板 - 设计 宝石“设计”,> = 4.2.0“ 宝石“设计异步” 3- 原生 Android/IOS 的 Turbolinks 包装器 - 4- Postgres

这是场景:

  • 用户已在桌面 (IE/EDGE) 上登录
  • 用户登录IOS App并使用它。滑动关闭。点击图标。 重新登录正常。
  • 用户滑动应用关闭
  • 用户退出桌面应用程序
  • 用户点击 IOS 上的 APP 图标 - 这需要他们再次登录。 (这在 ANDROID 上不是问题

用户模型

before_save :ensure_authentication_token_is_present

265

    before_save :set_name
  266  
  ...
 1413    end
 1414  
 1415:   def ensure_authentication_token_is_present
 1416:     if authentication_token.blank?
 1417:       self.authentication_token = generate_authentication_token
 1418      end
 1419    end
 1420  
 1421:   def generate_authentication_token
 1422      loop do
 1423        token = Devise.friendly_token
 1424:       break token unless User.find_by(authentication_token: token)
 1425      end
 1426    end

会话控制器:

class Api::V1::SessionsController < Api::V1::BaseController



skip_before_action :authenticate_user!
  skip_before_action :authenticate_user_using_x_auth_token

  skip_before_action :verify_authenticity_token,   only: :destroy
  before_action      :authenticate_user_by_token!, only: :destroy

  def create

        user = User.find_for_database_authentication(email: params[:user] && 

params[:user][:

email])
        if invalid_password?(user)
          respond_with_error("Incorrect email or password", 401)
        else
          render(
            json: { auth_token: user.authentication_token },
            location: root_path,
            status: :created
          )
        end
      end

  def destroy
    Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)

    head :ok
  end

  private

  def invalid_password?(user)
    user.blank? || !user.valid_password?(params[:user][:password])
  end

end

认证方式

def authenticate_user_using_x_auth_token
user_email = params[:id].presence
auth_token = request.headers["X-Auth-Token"].presence

user = user_email && User.find_by(email: user_email)

if user && Devise.secure_compare(user.authentication_token, auth_token)
  sign_in user, store: false
else
  respond_with_error(
    "Could not authenticate with the provided credentials",
    401
  )
end

结束

def authenticate_user_by_token!
    auth_token = request.headers["X-Auth-Token"].presence
    user       = User.find_by(authentication_token: auth_token)

    if user.present?
      sign_in user, store: false
    else
      respond_with_error("Could not authenticate with the provided credentials", 401)
    end
  end

【问题讨论】:

  • 所以我看到了这篇文章:stackoverflow.com/questions/14924606/… 这是在设计中......我们没有使用。 # ==> Configuration for :token_authenticatable # 定义身份验证令牌的名称 params key # config.token_authentication_key = :auth_token 想法?

标签: ios ruby-on-rails swift devise turbolinks


【解决方案1】:

Turbolinks-iOS 应用程序每次关闭时都会清除会话 cookie。您可能正在使用持久性 cookie,当您注销时,如果设计正在清除 user.remember_created_at,则 cookie 无效。

尝试将此添加到设计配置中:

config.expire_all_remember_me_on_sign_out = false

来源:https://github.com/plataformatec/devise/blob/11026007206226c1189f6050ab05d2284f47a669/lib/devise.rb#L130-L132

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 2012-01-28
    • 2021-01-16
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    相关资源
    最近更新 更多