Add JSON format support to session destroy for native API clients
This is so that native API clients can get the session record deleted server-side. They still need to take care of clearing any cookies they've set in web views. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
committed by
Rosa Gutierrez
parent
ec1b8f2fed
commit
c059e0b216
@@ -20,7 +20,11 @@ class SessionsController < ApplicationController
|
|||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
terminate_session
|
terminate_session
|
||||||
redirect_to_logout_url
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.html { redirect_to_logout_url }
|
||||||
|
format.json { head :no_content }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
+16
@@ -118,6 +118,22 @@ __Error responses:__
|
|||||||
| `401 Unauthorized` | Invalid `pending_authentication_token` or `code` |
|
| `401 Unauthorized` | Invalid `pending_authentication_token` or `code` |
|
||||||
| `429 Too Many Requests` | Rate limit exceeded |
|
| `429 Too Many Requests` | Rate limit exceeded |
|
||||||
|
|
||||||
|
|
||||||
|
#### Delete server-side session (_log out_)
|
||||||
|
|
||||||
|
To log out and destroy the server-side session:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X DELETE \
|
||||||
|
-H "Accept: application/json" \
|
||||||
|
-H "Cookie: session_token=eyJfcmFpbHMi..." \
|
||||||
|
https://app.fizzy.do/session
|
||||||
|
```
|
||||||
|
|
||||||
|
__Response:__
|
||||||
|
|
||||||
|
Returns `204 No Content` on success.
|
||||||
|
|
||||||
## Caching
|
## Caching
|
||||||
|
|
||||||
Most endpoints return [ETag](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag) and [Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control) headers. You can use these to avoid re-downloading unchanged data.
|
Most endpoints return [ETag](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag) and [Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control) headers. You can use these to avoid re-downloading unchanged data.
|
||||||
|
|||||||
@@ -22,6 +22,26 @@ class ApiTest < ActionDispatch::IntegrationTest
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "logout with user credentials" do
|
||||||
|
identity = identities(:david)
|
||||||
|
|
||||||
|
untenanted do
|
||||||
|
post session_path(format: :json), params: { email_address: identity.email_address }
|
||||||
|
magic_link = MagicLink.last
|
||||||
|
|
||||||
|
assert_difference -> { identity.sessions.count }, +1 do
|
||||||
|
post session_magic_link_path(format: :json), params: { code: magic_link.code, pending_authentication_token: @response.parsed_body["pending_authentication_token"] }
|
||||||
|
end
|
||||||
|
assert cookies[:session_token].present?
|
||||||
|
|
||||||
|
assert_difference -> { identity.sessions.count }, -1 do
|
||||||
|
delete session_path(format: :json)
|
||||||
|
end
|
||||||
|
assert_response :no_content
|
||||||
|
assert_not cookies[:session_token].present?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test "authenticate with valid access token" do
|
test "authenticate with valid access token" do
|
||||||
get boards_path(format: :json), env: @davids_bearer_token
|
get boards_path(format: :json), env: @davids_bearer_token
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
|||||||
@@ -117,4 +117,15 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest
|
|||||||
assert_response :unprocessable_entity
|
assert_response :unprocessable_entity
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "destroy via JSON" do
|
||||||
|
sign_in_as :kevin
|
||||||
|
|
||||||
|
untenanted do
|
||||||
|
delete session_path(format: :json)
|
||||||
|
|
||||||
|
assert_response :no_content
|
||||||
|
assert_not cookies[:session_token].present?
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user