-
I am trying to get a setup which allows both the HTML/REST authentication and auth via JWT. Once authenticated I hope to protect some Rails routes with this authentication. Authentication is working fine as long as I stick to the standard Rodauth routes for account management.
This is the method that triggers the 401. The chain from there is:
But when I look at the definition of
and from my debug session:
I can't find anywhere that this key would be set, and I don't understand why JWT would be looking at sessions, and more, only looking at sessions, to determine |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Additionally, why does the JWT feature deal with a Session at all?
And how critical is using cookie store for rodauth-rails? The documentation shows usage of Cookies as the session store, but this doesn't work for me, as the app is using |
Beta Was this translation helpful? Give feedback.
-
Rodauth operates with the session object as a hash, which should support all Rails session stores (cookie, redis, Active Record etc). In what way does the Redis session store not work you? JWT mode doesn't actually use Rails session, as you can see from the implementation. It stores session data in a plain hash, which ends up in the JWT token payload. This enables it to work exactly the same way as with Rails session, just with the JWT token acting as session data storage. I couldn't reproduce the routing constraint not working with JWTs. I made the below changes to the demo app, and accessing an authenticated route worked. Can you reproduce the issue in a fresh Rails app with a failing integration test? Changesdiff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index c4360a0..7d2d4a6 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -1,10 +1,15 @@
class PostsController < ApplicationController
- before_action :authenticate
+ # before_action :authenticate
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
def index
@posts = current_account.posts.all
+
+ respond_to do |format|
+ format.html
+ format.json { render json: @posts }
+ end
end
# GET /posts/1
diff --git a/config/routes.rb b/config/routes.rb
index d33e301..8e96fb0 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,6 +1,8 @@
Rails.application.routes.draw do
root to: "home#index"
- resources :posts
+ constraints Rodauth::Rails.authenticate do
+ resources :posts
+ end
namespace :admin do
root to: "home#index"
diff --git a/test/controllers/posts_controller_test.rb b/test/controllers/posts_controller_test.rb
index dde4dab..46dc0cf 100644
--- a/test/controllers/posts_controller_test.rb
+++ b/test/controllers/posts_controller_test.rb
@@ -2,13 +2,13 @@ require "test_helper"
class PostsControllerTest < ActionDispatch::IntegrationTest
test "should require authentication" do
- get posts_url
+ get posts_url, as: :json
- assert_redirected_to "/login"
+ assert_response :unauthorized
login
- get posts_url
+ get posts_url, as: :json, env: { "HTTP_AUTHORIZATION" => response.headers["Authorization"] }
assert_response :success
end
@@ -16,16 +16,9 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
private
def login(login: "user@example.com", password: "secret123")
- post "/create-account", params: {
- "name" => "Janko",
- "email" => login,
- "password" => password,
- "password-confirm" => password,
- }
-
post "/login", params: {
- "email" => login,
- "password" => password,
- }
+ "email" => "freddie@queen.com",
+ "password" => "password",
+ }, as: :json
end
end |
Beta Was this translation helpful? Give feedback.
Rodauth operates with the session object as a hash, which should support all Rails session stores (cookie, redis, Active Record etc). In what way does the Redis session store not work you?
JWT mode doesn't actually use Rails session, as you can see from the implementation. It stores session data in a plain hash, which ends up in the JWT token payload. This enables it to work exactly the same way as with Rails session, just with the JWT token acting as session data storage.
I couldn't reproduce the routing constraint not working with JWTs. I made the below changes to the demo app, and accessing an authenticated route worked. Can you reproduce the issue in a fresh Rails app with a failing i…