@beepbox.net/gofetch-client 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -7
- package/dist/index.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,20 +44,38 @@ await client.user.me({ headers: { Authorization: `Bearer ${token}` } })
|
|
|
44
44
|
|
|
45
45
|
## Auth
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
Browser apps should use the `returnTo` redirect flow:
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
Your app runs the login UX (redirect to `/v0/auth/discord`, handle the callback response, store the JWT). Then pass the token into the client:
|
|
49
|
+
1. Send the user to `buildLoginUrl({ returnTo: 'http://localhost:3000/callback' })`
|
|
50
|
+
2. Add a `/callback` route that reads the hash fragment with `parseOAuthCallback()`
|
|
51
|
+
3. Store the JWT and clear the hash with `clearOAuthCallbackHash()`
|
|
54
52
|
|
|
55
53
|
```ts
|
|
56
|
-
|
|
54
|
+
import {
|
|
55
|
+
buildLoginUrl,
|
|
56
|
+
clearOAuthCallbackHash,
|
|
57
|
+
createClient,
|
|
58
|
+
parseOAuthCallback,
|
|
59
|
+
} from '@beepbox.net/gofetch-client'
|
|
60
|
+
|
|
61
|
+
// Login button
|
|
62
|
+
window.location.href = buildLoginUrl({
|
|
63
|
+
returnTo: `${window.location.origin}/callback`,
|
|
64
|
+
})
|
|
57
65
|
|
|
66
|
+
// On /callback
|
|
67
|
+
const result = parseOAuthCallback()
|
|
68
|
+
if (result?.ok) {
|
|
69
|
+
localStorage.setItem('bds-admin-token', result.accessToken)
|
|
70
|
+
clearOAuthCallbackHash()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const api = createClient({ token: result?.ok ? result.accessToken : undefined })
|
|
58
74
|
const { data: me } = await api.v1.user.me.get()
|
|
59
75
|
```
|
|
60
76
|
|
|
77
|
+
The gofetch server must allow your portal origin via `OAUTH_RETURN_ALLOWLIST`. Without `returnTo`, `GET /v0/auth/discord/callback` still returns JSON for API testing.
|
|
78
|
+
|
|
61
79
|
Per-request headers also work:
|
|
62
80
|
|
|
63
81
|
```ts
|