Web 应用程序和 IOS 应用程序之间的持久用户
Persisting User between Web App and IOS App
我有一个适用于桌面的应用程序,并为 IOS 和 ANDROID 使用 turbolinks 包装器。我对 ANDROID 没有任何问题。但是在 IOS 上,如果用户退出桌面应用程序,当 he/she 转到我的 iphone 应用程序图标时,用户必须重新登录(请参阅下面的完整步骤)。
我的问题是:
- 是什么原因造成的? (奇怪的是 ANDROID 不是问题)
- what/Where 我需要解决这个问题吗?
- 这是 rails 附带问题,turoblinks 问题还是
- 下面是来自 Rails 端的代码。注意:我没有 IOS 应用程序代码 -(使用移动程序员帮助我)
我有一个应用程序使用-
1- Ruby Rails gem "rails", "5.2.2"
2- GEM - 设计
gem "devise", ">= 4.2.0"
gem "devise-async"
3- 原生 Android/IOS 的 Turbolinks 包装器 -
4- Postgres
场景如下:
- 用户已在桌面上登录(IE/EDGE)
- 用户登录 IOS 应用程序并使用它。滑动关闭。点击图标。
登录正常。
- 用户滑动应用关闭
- 用户退出桌面应用程序
- 用户在 IOS 上点击应用程序图标 - 这需要他们重新登录。
(这不是 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
Turbolinks-iOS 应用程序每次关闭时都会清除会话 cookie。您可能正在使用持久性 cookie,当您注销时,如果设计正在清除 user.remember_created_at
,则 cookie 将失效。
尝试将此添加到设备配置中:
config.expire_all_remember_me_on_sign_out = false
我有一个适用于桌面的应用程序,并为 IOS 和 ANDROID 使用 turbolinks 包装器。我对 ANDROID 没有任何问题。但是在 IOS 上,如果用户退出桌面应用程序,当 he/she 转到我的 iphone 应用程序图标时,用户必须重新登录(请参阅下面的完整步骤)。
我的问题是: - 是什么原因造成的? (奇怪的是 ANDROID 不是问题) - what/Where 我需要解决这个问题吗? - 这是 rails 附带问题,turoblinks 问题还是 - 下面是来自 Rails 端的代码。注意:我没有 IOS 应用程序代码 -(使用移动程序员帮助我)
我有一个应用程序使用- 1- Ruby Rails gem "rails", "5.2.2" 2- GEM - 设计 gem "devise", ">= 4.2.0" gem "devise-async" 3- 原生 Android/IOS 的 Turbolinks 包装器 - 4- Postgres
场景如下:
- 用户已在桌面上登录(IE/EDGE)
- 用户登录 IOS 应用程序并使用它。滑动关闭。点击图标。 登录正常。
- 用户滑动应用关闭
- 用户退出桌面应用程序
- 用户在 IOS 上点击应用程序图标 - 这需要他们重新登录。 (这不是 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
Turbolinks-iOS 应用程序每次关闭时都会清除会话 cookie。您可能正在使用持久性 cookie,当您注销时,如果设计正在清除 user.remember_created_at
,则 cookie 将失效。
尝试将此添加到设备配置中:
config.expire_all_remember_me_on_sign_out = false