我在Rails7上的会话很难持续,我甚至没有在任何时候重定向,只是停留在一个页面上。
应用程序控制器.rb
class ApplicationController < ActionController::API
include ActionController::Cookies
end
application.rb
require_relative "boot"
require "rails/all"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module CsmBack
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
# Must add these lines!
# Adding back cookies and session middleware
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore, key: '_namespace_key'
# Use SameSite=Strict for all cookies to help protect against CSRF
config.action_dispatch.cookies_same_site_protection = :strict
end
end
会话控制器.rb
def login
if session[:user_id]
return render json: {errors: "Username or Password Wrong"}
end
user=User.find_by(username: params[:username])
if user&.authenticate(params[:password])
session[:user_id] = user.id
render json: user
else
render json: {errors: "Username or Password Wrong"}
end
end
如果最后一个成功了,我应该可以连续两次单击该按钮来触发它,并获得不同的值,但我甚至没有得到这些值。
1条答案
按热度按时间ibrsph3r1#
如果客户端在没有注销的情况下尝试登录,您似乎会向客户端抛出一个错误。根据我在此处看到的内容,您的服务器端代码(无论看起来多么糟糕)是正确的。您可能需要查看您的客户端,或许设置一个代理