Skip to content

Commit beb4e15

Browse files
committed
📝 Document example of JSON body support
1 parent b63cc7f commit beb4e15

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,53 @@ Then link users to `/auth/ldap` in your app (for example, in a Devise sign-in pa
333333

334334
This gem is compatible with JSON-encoded POST bodies as well as traditional form-encoded.
335335

336-
Set header `Content-Type` to `application/json`
336+
- Set header `Content-Type` to `application/json`.
337+
- Send a JSON object containing `username` and `password`.
338+
- Rails automatically exposes parsed JSON params via `env["action_dispatch.request.request_parameters"]`, which this strategy reads first. In non-Rails Rack apps, ensure you use a JSON parser middleware if you post raw JSON.
337339

338-
Send your credentials similar to below to use this gem:
339-
`{"username":"USERNAME","password":"PASSWORD"}`
340+
Examples
341+
342+
- curl (JSON):
343+
344+
```bash
345+
curl -i \
346+
-X POST \
347+
-H 'Content-Type: application/json' \
348+
-d '{"username":"alice","password":"secret"}' \
349+
http://localhost:3000/auth/ldap
350+
```
351+
352+
The request phase will redirect to `/auth/ldap/callback` when both fields are present.
353+
354+
- curl (form-encoded, still supported):
355+
356+
```bash
357+
curl -i \
358+
-X POST \
359+
-H 'Content-Type: application/x-www-form-urlencoded' \
360+
--data-urlencode 'username=alice' \
361+
--data-urlencode 'password=secret' \
362+
http://localhost:3000/auth/ldap
363+
```
364+
365+
- Browser (JavaScript fetch):
366+
367+
```js
368+
fetch('/auth/ldap', {
369+
method: 'POST',
370+
headers: { 'Content-Type': 'application/json' },
371+
body: JSON.stringify({ username: 'alice', password: 'secret' })
372+
}).then(res => {
373+
if (res.redirected) {
374+
window.location = res.url; // typically /auth/ldap/callback
375+
}
376+
});
377+
```
378+
379+
Notes
380+
381+
- You can still initiate authentication by visiting `GET /auth/ldap` to render the HTML form and then submitting it (form-encoded). JSON is an additional option, not a replacement.
382+
- In the callback phase (`POST /auth/ldap/callback`), the strategy reads JSON credentials the same way; Rails exposes them via `action_dispatch.request.request_parameters` and non-Rails apps should use a JSON parser middleware.
340383

341384
### Using a custom filter
342385

@@ -736,7 +779,7 @@ Please consider sponsoring me or the project.
736779

737780
To join the community or get help 👇️ Join the Discord.
738781

739-
[![Live Chat on Discord][✉️discord-invite-img-ftb]][✉️discord-invite]
782+
[![Live Chat on Discord][✉️discord-invite-img-ftb]][✉️discord]
740783

741784
To say "thanks!" ☝️ Join the Discord or 👇️ send money.
742785

0 commit comments

Comments
 (0)