next-token-auth 1.0.5 → 1.0.9
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 +38 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -287,6 +287,44 @@ The hook waits for `isLoading` to be `false` before acting, so it won't flash a
|
|
|
287
287
|
|
|
288
288
|
---
|
|
289
289
|
|
|
290
|
+
### Making Authenticated API Requests
|
|
291
|
+
|
|
292
|
+
When you need to call your own backend endpoints that require an access token, use `client.fetch` from the `useAuth` hook. It automatically injects `Authorization: Bearer <token>` and handles 401 → refresh → retry for you.
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
"use client";
|
|
296
|
+
|
|
297
|
+
import { useAuth } from "next-token-auth/react";
|
|
298
|
+
|
|
299
|
+
export default function Orders() {
|
|
300
|
+
const { client } = useAuth();
|
|
301
|
+
|
|
302
|
+
async function fetchOrders() {
|
|
303
|
+
const res = await client.fetch("https://api.example.com/orders");
|
|
304
|
+
const data = await res.json();
|
|
305
|
+
console.log(data);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return <button onClick={fetchOrders}>Load Orders</button>;
|
|
309
|
+
}
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
You can also read the token directly from the session if you need to pass it manually:
|
|
313
|
+
|
|
314
|
+
```ts
|
|
315
|
+
const { session } = useAuth();
|
|
316
|
+
|
|
317
|
+
const res = await fetch("https://api.example.com/orders", {
|
|
318
|
+
headers: {
|
|
319
|
+
Authorization: `Bearer ${session.tokens?.accessToken}`,
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
`client.fetch` is the recommended approach — it keeps your requests resilient to token expiry without any extra work on your end.
|
|
325
|
+
|
|
326
|
+
---
|
|
327
|
+
|
|
290
328
|
### Protecting API Routes with `withAuth`
|
|
291
329
|
|
|
292
330
|
Wrap App Router route handlers to require authentication:
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-token-auth",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "Production-grade authentication library for Next.js (App Router & Pages Router)",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/TonyMike/tonymike-next-auth-kit"
|
|
10
|
+
"url": "git+https://github.com/TonyMike/tonymike-next-auth-kit.git"
|
|
11
11
|
},
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|