在我的应用中,我正在使用devise-jwt令牌进行用户身份验证。当用户尝试从设备A重设密码但他也登录到设备B上的同一帐户时,我遇到了这种情况。在这种情况下是否可以从设备B注销用户?
您可以使用设备安全性扩展名here is link for detail page
该gem中有7个其他devise模块,但您需要的是:session_limitable,可确保每个帐户一次只能使用一个会话。
短版安装步骤:
gem 'devise-security'
# then run bundle install
rails generate devise_security:install
# this to run generator
生成器将可选配置添加到config / initializers / devise-security.rb。启用要在初始化程序中使用的模块您已准备好在以下基础上添加Devise Security模块将模块设计为您的任何Devise模型:
您可以在模型用户或帐户中添加
devise :database_authenticatable, :registerable,.... , :session_limitable
尝试使用此服务
#check_token_service.rb
class CheckTokenService
TOKEN_REGEX = /(?<token_type>\w+)\s(?<token>\S+)/.freeze
def initialize(token)
@token_decoder = Warden::JWTAuth::TokenDecoder.new.call(token.match(TOKEN_REGEX)['token'])
end
end
然后在执行所有操作之前先致电服务
raise JWT::ExpiredSignature unless CheckTokenService.new(request.headers["HTTP_AUTHORIZATION"]).perform