简体   繁体   English

Rails 7 API 仅应用程序需要 session 存储与 Devise

[英]Rails 7 API only app requiring session store with Devise

I have a new app that I am trying to setup with devise and devise-jwt .我有一个新应用程序,我正在尝试使用devise和 devise devise-jwt进行设置。 For some reason my authenticate_user!出于某种原因,我的authenticate_user! call is causing an error because sessions have been disabled:由于会话已被禁用,调用导致错误:

{"status":500,"error":"Internal Server Error","exception":"ActionDispatch::Request::Session::DisabledSessionError: Your application has sessions disabled. To write to the session you must first configure a session store"

Routes:路线:

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :clients, only: [:show]
      devise_for :users,
             controllers: {
                 sessions: 'users/sessions',
                 registrations: 'users/registrations'
             }
    end
  end
end

User用户

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable,
         :jwt_authenticatable,
         jwt_revocation_strategy: JwtDenylist
end

Controllers:控制器:

class API::V1::Users::SessionsController < Devise::SessionsController
  respond_to :json
  private
  def respond_with(resource, _opts = {})
    render json: { message: 'Logged.' }, status: :ok
  end
  def respond_to_on_destroy
    current_user ? log_out_success : log_out_failure
  end
  def log_out_success
    render json: { message: "Logged out." }, status: :ok
  end
  def log_out_failure
    render json: { message: "Logged out failure."}, status: :unauthorized
  end
end

class Api::V1::ClientsController < ApplicationController
    before_action :authenticate_api_v1_user!

    def show
    end
end

What else do I need to do?我还需要做什么? When I make a request to http://localhost:3000/api/v1/clients/ID I'd expect the response to be:当我向http://localhost:3000/api/v1/clients/ID发出请求时,我希望响应是:

=> <html><body>You are being <a href="http://localhost:3000/users/sign_in">redirected</a>.</body></html>%

rather than this 500 error.而不是这个 500 错误。

I was able to find a solution on GitHub:我能够在 GitHub 上找到解决方案:

 config.session_store:cookie_store, key: '_interslice_session' config.middleware.use ActionDispatch::Cookies config.middleware.use config.session_store, config.session_options

from this post 从这个帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用设计会话为Rails / Ember应用程序验证门卫API - Use Devise Session to Authenticate Doorkeeper API for Rails/Ember app Rails管理员与Devise Admin用户不适用于仅API应用程序 - Rails Admin with Devise Admin user not working for API only app Rails:通过config.session_store设计:禁用 - Rails : Devise with config.session_store :disabled 在Rails上的Web API中使用Devise在Web API中创建会话 - Create session with Devise in web API on ruby on rails 使用设计和会话存储的 Rails 动态会话超时 - Rails dynamic session timeout using devise and session store 将 cookie session 商店添加回 Rails API 应用程序 - Adding cookie session store back to Rails API app Rails API Devise JWT 禁用会话存储 - Rails API Devise JWT disable sessions store 模型或控制器:My Rails应用程序进行要求OAuth令牌的API调用,这些令牌存储为会话变量。 该方法应该去哪里? - Model or controller: My Rails app makes API calls requiring OAuth tokens, stored as session variables. Where should the method go? Rails设计,通过session_store键找到current_user - Rails devise, find current_user by session_store key Rails API,设计和移动应用程序。 - Rails API, Devise & Mobile App.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM