azirid-react 0.13.1 → 0.14.0
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 +58 -183
- package/dist/index.cjs +43 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -61
- package/dist/index.d.ts +15 -61
- package/dist/index.js +43 -65
- package/dist/index.js.map +1 -1
- package/dist/next-proxy.cjs +21 -4
- package/dist/next-proxy.cjs.map +1 -1
- package/dist/next-proxy.d.cts +19 -14
- package/dist/next-proxy.d.ts +19 -14
- package/dist/next-proxy.js +21 -4
- package/dist/next-proxy.js.map +1 -1
- package/dist/server.cjs +0 -35
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +1 -25
- package/dist/server.d.ts +1 -25
- package/dist/server.js +1 -35
- package/dist/server.js.map +1 -1
- package/package.json +8 -25
- package/dist/next-handlers.cjs +0 -148
- package/dist/next-handlers.cjs.map +0 -1
- package/dist/next-handlers.d.cts +0 -121
- package/dist/next-handlers.d.ts +0 -121
- package/dist/next-handlers.js +0 -138
- package/dist/next-handlers.js.map +0 -1
- package/dist/next.cjs +0 -181
- package/dist/next.cjs.map +0 -1
- package/dist/next.d.cts +0 -182
- package/dist/next.d.ts +0 -182
- package/dist/next.js +0 -169
- package/dist/next.js.map +0 -1
package/README.md
CHANGED
|
@@ -23,71 +23,19 @@ npm install react react-dom @tanstack/react-query
|
|
|
23
23
|
|
|
24
24
|
---
|
|
25
25
|
|
|
26
|
-
##
|
|
26
|
+
## Quick Start — Next.js
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
| | Proxy mode | Direct mode |
|
|
31
|
-
| ---------------- | --------------------------------------------- | ----------------------------- |
|
|
32
|
-
| **Best for** | Next.js (App Router) | React SPA (Vite, CRA, Remix) |
|
|
33
|
-
| **Security** | First-party cookies, no CORS | Requires CORS on API |
|
|
34
|
-
| **Setup** | Route handler + Provider | Provider only |
|
|
35
|
-
| **How it works** | Browser → your app `/api/auth/*` → Azirid API | Browser → Azirid API directly |
|
|
36
|
-
|
|
37
|
-
### Proxy mode (recommended for Next.js)
|
|
38
|
-
|
|
39
|
-
Requests go to your Next.js app's `/api/auth/*` route handler, which securely proxies them to the Azirid API. Cookies are first-party (same domain), so no CORS configuration is needed.
|
|
40
|
-
|
|
41
|
-
```tsx
|
|
42
|
-
// No apiUrl prop → proxy mode is activated automatically
|
|
43
|
-
<AziridProvider publishableKey="pk_live_...">
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### Direct mode (for React SPA / Vite)
|
|
47
|
-
|
|
48
|
-
Requests go directly to the Azirid API. Requires CORS to be configured on the API server.
|
|
49
|
-
|
|
50
|
-
```tsx
|
|
51
|
-
// apiUrl prop → direct mode
|
|
52
|
-
<AziridProvider apiUrl="https://api.azirid.com" publishableKey="pk_live_...">
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
---
|
|
56
|
-
|
|
57
|
-
## Quick Start — Next.js (Proxy Mode)
|
|
58
|
-
|
|
59
|
-
### 1. Create the route handler
|
|
60
|
-
|
|
61
|
-
```ts
|
|
62
|
-
// app/api/auth/[...path]/route.ts
|
|
63
|
-
export { GET, POST, PUT, PATCH, DELETE } from 'azirid-react/next/handlers'
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
That's it. The SDK handles proxy logic, cookie fixing, and header forwarding internally. Works with Next.js 14, 15, and 16+ automatically — including `output: 'standalone'` builds.
|
|
67
|
-
|
|
68
|
-
### 2. Set the API URL (optional)
|
|
69
|
-
|
|
70
|
-
```env
|
|
71
|
-
# .env (server-side only — never exposed to the browser)
|
|
72
|
-
# Default: https://api.azirid.com
|
|
73
|
-
# For local development with the API running locally:
|
|
74
|
-
AZIRID_API_URL=http://localhost:3000
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### 3. Wrap your app with `<AziridProvider>`
|
|
28
|
+
### 1. Wrap your app with `<AziridProvider>`
|
|
78
29
|
|
|
79
30
|
```tsx
|
|
80
31
|
// app/layout.tsx
|
|
81
32
|
import { AziridProvider } from 'azirid-react'
|
|
82
33
|
|
|
83
34
|
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
84
|
-
const router = useRouter()
|
|
85
|
-
|
|
86
35
|
return (
|
|
87
36
|
<AziridProvider
|
|
88
37
|
publishableKey={process.env.NEXT_PUBLIC_AZIRID_PK!}
|
|
89
|
-
|
|
90
|
-
onSessionExpired={() => (window.location.href = '/login')}
|
|
38
|
+
apiUrl={process.env.NEXT_PUBLIC_AZIRID_API_URL}
|
|
91
39
|
>
|
|
92
40
|
{children}
|
|
93
41
|
</AziridProvider>
|
|
@@ -95,7 +43,25 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
|
|
95
43
|
}
|
|
96
44
|
```
|
|
97
45
|
|
|
98
|
-
###
|
|
46
|
+
### 2. Add route protection (optional)
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
// proxy.ts (Next.js 16+)
|
|
50
|
+
import { createAziridProxy } from 'azirid-react/next/proxy'
|
|
51
|
+
|
|
52
|
+
export const proxy = createAziridProxy({
|
|
53
|
+
protectedRoutes: ['/dashboard'],
|
|
54
|
+
loginUrl: '/login',
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
export const config = {
|
|
58
|
+
matcher: ['/((?!_next|favicon.ico|api/).*)'],
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The middleware validates JWTs using JWKS and protects routes automatically.
|
|
63
|
+
|
|
64
|
+
### 3. Add the login page
|
|
99
65
|
|
|
100
66
|
```tsx
|
|
101
67
|
// app/login/page.tsx
|
|
@@ -106,21 +72,30 @@ export default function LoginPage() {
|
|
|
106
72
|
}
|
|
107
73
|
```
|
|
108
74
|
|
|
75
|
+
### 4. Configure your environment
|
|
76
|
+
|
|
77
|
+
```env
|
|
78
|
+
# .env
|
|
79
|
+
NEXT_PUBLIC_AZIRID_API_URL=https://api.azirid.com
|
|
80
|
+
NEXT_PUBLIC_AZIRID_PK=pk_live_...
|
|
81
|
+
# Server-side only (used by middleware for JWKS validation)
|
|
82
|
+
AZIRID_API_URL=https://api.azirid.com
|
|
83
|
+
```
|
|
84
|
+
|
|
109
85
|
---
|
|
110
86
|
|
|
111
|
-
## Quick Start — React SPA (
|
|
87
|
+
## Quick Start — React SPA (Vite)
|
|
112
88
|
|
|
113
89
|
### 1. Wrap your app with `<AziridProvider>`
|
|
114
90
|
|
|
115
91
|
```tsx
|
|
116
|
-
// main.tsx
|
|
92
|
+
// main.tsx
|
|
117
93
|
import { AziridProvider } from 'azirid-react'
|
|
118
94
|
|
|
119
95
|
createRoot(document.getElementById('root')!).render(
|
|
120
96
|
<AziridProvider
|
|
121
97
|
apiUrl={import.meta.env.VITE_AZIRID_API_URL || 'https://api.azirid.com'}
|
|
122
98
|
publishableKey={import.meta.env.VITE_AZIRID_PK}
|
|
123
|
-
onLoginSuccess={(data) => console.log('Logged in:', data.user)}
|
|
124
99
|
>
|
|
125
100
|
<App />
|
|
126
101
|
</AziridProvider>,
|
|
@@ -130,7 +105,6 @@ createRoot(document.getElementById('root')!).render(
|
|
|
130
105
|
### 2. Configure your environment
|
|
131
106
|
|
|
132
107
|
```env
|
|
133
|
-
# .env
|
|
134
108
|
VITE_AZIRID_API_URL=https://api.azirid.com
|
|
135
109
|
VITE_AZIRID_PK=pk_live_...
|
|
136
110
|
```
|
|
@@ -145,8 +119,6 @@ export default function LoginPage() {
|
|
|
145
119
|
}
|
|
146
120
|
```
|
|
147
121
|
|
|
148
|
-
No route handler or proxy needed — requests go directly to the API.
|
|
149
|
-
|
|
150
122
|
---
|
|
151
123
|
|
|
152
124
|
## Components
|
|
@@ -1293,14 +1265,13 @@ The badge renders automatically below each built-in form component (`LoginForm`,
|
|
|
1293
1265
|
Under the hood `AziridProvider` creates an `AccessClient` via `createAccessClient`. You can also create a client directly to make raw API calls.
|
|
1294
1266
|
|
|
1295
1267
|
```ts
|
|
1296
|
-
import { createAccessClient,
|
|
1268
|
+
import { createAccessClient, AUTH_BASE_PATH } from 'azirid-react'
|
|
1297
1269
|
import type { AccessClientConfig } from 'azirid-react'
|
|
1298
1270
|
|
|
1299
|
-
// Direct mode — point to the API
|
|
1300
1271
|
const client = createAccessClient(
|
|
1301
1272
|
{
|
|
1302
1273
|
baseUrl: 'https://api.azirid.com',
|
|
1303
|
-
basePath:
|
|
1274
|
+
basePath: AUTH_BASE_PATH, // '/v1/users/auth'
|
|
1304
1275
|
},
|
|
1305
1276
|
{ publishableKey: 'pk_live_...' },
|
|
1306
1277
|
)
|
|
@@ -1335,14 +1306,13 @@ function createAccessClient(
|
|
|
1335
1306
|
| Prop | Type | Default | Description |
|
|
1336
1307
|
| ------------------ | ------------------------- | ------- | -------------------------------------------------------------------------------- |
|
|
1337
1308
|
| `children` | `ReactNode` | — | **Required.** Your app tree |
|
|
1338
|
-
| `apiUrl` | `string` |
|
|
1309
|
+
| `apiUrl` | `string` | `https://api.azirid.com` | Azirid API URL. Set for local dev or custom deployments |
|
|
1339
1310
|
| `publishableKey` | `string` | — | Publishable key (e.g. `pk_live_...`) |
|
|
1340
1311
|
| `tenantId` | `string` | — | Tenant ID for multi-tenant apps |
|
|
1341
1312
|
| `fetchOptions` | `Record<string, string>` | — | Extra headers to send with every request |
|
|
1342
1313
|
| `autoBootstrap` | `boolean` | `true` | Auto-restore session on mount. Runs once (safe in React 18 Strict Mode) |
|
|
1343
1314
|
| `refreshInterval` | `number` | `50000` | Token refresh interval in ms. `0` to disable |
|
|
1344
|
-
| `
|
|
1345
|
-
| `onAuthStateChange`| `() => void` | — | Called after login, signup, or logout. **In Next.js, pass `router.refresh()`** to sync server actions with updated cookies |
|
|
1315
|
+
| `onAuthStateChange`| `() => void` | — | Called after login, signup, or logout |
|
|
1346
1316
|
| `onLoginSuccess` | `(data) => void` | — | Called after successful login |
|
|
1347
1317
|
| `onSignupSuccess` | `(data) => void` | — | Called after successful signup |
|
|
1348
1318
|
| `onLogoutSuccess` | `() => void` | — | Called after logout |
|
|
@@ -1454,78 +1424,14 @@ export function Providers({ children }: { children: React.ReactNode }) {
|
|
|
1454
1424
|
|
|
1455
1425
|
> **Not using server actions?** (e.g. pure client-side SPA with Vite) — you can skip `onAuthStateChange`.
|
|
1456
1426
|
|
|
1457
|
-
###
|
|
1458
|
-
|
|
1459
|
-
Create the file `app/api/auth/[...path]/route.ts` — one line is all you need:
|
|
1460
|
-
|
|
1461
|
-
```ts
|
|
1462
|
-
// app/api/auth/[...path]/route.ts
|
|
1463
|
-
export { GET, POST, PUT, PATCH, DELETE } from 'azirid-react/next/handlers'
|
|
1464
|
-
```
|
|
1427
|
+
### Middleware — Route Protection & JWT Validation
|
|
1465
1428
|
|
|
1466
|
-
|
|
1467
|
-
It works with Next.js 14, 15, and 16+ automatically — including `output: 'standalone'` builds.
|
|
1468
|
-
|
|
1469
|
-
### Custom API URL or debug logging
|
|
1470
|
-
|
|
1471
|
-
```ts
|
|
1472
|
-
// app/api/auth/[...path]/route.ts
|
|
1473
|
-
import { createAziridRouteHandlers } from 'azirid-react/next/handlers'
|
|
1474
|
-
|
|
1475
|
-
export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({
|
|
1476
|
-
apiUrl: 'https://my-custom-api.com',
|
|
1477
|
-
debug: true, // logs proxy requests to console
|
|
1478
|
-
})
|
|
1479
|
-
```
|
|
1480
|
-
|
|
1481
|
-
### Environment variable
|
|
1482
|
-
|
|
1483
|
-
The proxy reads `AZIRID_API_URL` (server-side only) to know where to forward requests:
|
|
1484
|
-
|
|
1485
|
-
```env
|
|
1486
|
-
# .env
|
|
1487
|
-
# Default: https://api.azirid.com
|
|
1488
|
-
# For local development:
|
|
1489
|
-
AZIRID_API_URL=http://localhost:3000
|
|
1490
|
-
```
|
|
1491
|
-
|
|
1492
|
-
> **Important:** Use `AZIRID_API_URL` (without `NEXT_PUBLIC_` prefix) — the API URL should never be exposed to the browser. The proxy runs server-side only.
|
|
1493
|
-
|
|
1494
|
-
### Next.js Config
|
|
1495
|
-
|
|
1496
|
-
#### Next.js 16+ (`next.config.ts`)
|
|
1497
|
-
|
|
1498
|
-
Turbopack is the default bundler — `transpilePackages` is no longer needed:
|
|
1499
|
-
|
|
1500
|
-
```ts
|
|
1501
|
-
// next.config.ts
|
|
1502
|
-
import type { NextConfig } from 'next'
|
|
1503
|
-
|
|
1504
|
-
const nextConfig: NextConfig = {}
|
|
1505
|
-
|
|
1506
|
-
export default nextConfig
|
|
1507
|
-
```
|
|
1508
|
-
|
|
1509
|
-
#### Next.js 14/15 (`next.config.js`)
|
|
1510
|
-
|
|
1511
|
-
```js
|
|
1512
|
-
// next.config.js
|
|
1513
|
-
const { withAziridProxy } = require('azirid-react/next')
|
|
1514
|
-
|
|
1515
|
-
/** @type {import('next').NextConfig} */
|
|
1516
|
-
module.exports = withAziridProxy()({
|
|
1517
|
-
transpilePackages: ['azirid-react'],
|
|
1518
|
-
})
|
|
1519
|
-
```
|
|
1520
|
-
|
|
1521
|
-
### Route Protection (optional)
|
|
1522
|
-
|
|
1523
|
-
> **Not required for basic usage.** Only add this if you need to protect specific routes from unauthenticated users.
|
|
1429
|
+
The middleware validates `__session` JWT cookies using JWKS (RS256) and protects routes.
|
|
1524
1430
|
|
|
1525
1431
|
#### Next.js 16+ (`proxy.ts`)
|
|
1526
1432
|
|
|
1527
1433
|
```ts
|
|
1528
|
-
// proxy.ts
|
|
1434
|
+
// proxy.ts
|
|
1529
1435
|
import { createAziridProxy } from 'azirid-react/next/proxy'
|
|
1530
1436
|
|
|
1531
1437
|
export const proxy = createAziridProxy({
|
|
@@ -1539,28 +1445,25 @@ export const config = {
|
|
|
1539
1445
|
}
|
|
1540
1446
|
```
|
|
1541
1447
|
|
|
1542
|
-
|
|
1448
|
+
| Option | Type | Default | Description |
|
|
1449
|
+
| ------------------ | ---------- | ------------- | ------------------------------------------------ |
|
|
1450
|
+
| `cookieName` | `string` | `"__session"` | Cookie carrying the access token JWT |
|
|
1451
|
+
| `protectedRoutes` | `string[]` | — | Routes that require authentication |
|
|
1452
|
+
| `loginUrl` | `string` | `"/login"` | Redirect target for unauthenticated users |
|
|
1453
|
+
| `publicRoutes` | `string[]` | login/signup | Always-accessible routes |
|
|
1454
|
+
| `jwksUrl` | `string` | auto | Override JWKS endpoint URL |
|
|
1543
1455
|
|
|
1544
|
-
|
|
1545
|
-
// middleware.ts — only needed if you want route protection
|
|
1546
|
-
import { createAziridMiddleware } from 'azirid-react/next/proxy'
|
|
1456
|
+
Environment: set `AZIRID_API_URL` (server-side only) so the middleware knows where to fetch JWKS from.
|
|
1547
1457
|
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
loginUrl: '/login',
|
|
1551
|
-
publicRoutes: ['/login', '/signup', '/forgot-password'],
|
|
1552
|
-
})
|
|
1553
|
-
|
|
1554
|
-
export const config = {
|
|
1555
|
-
matcher: ['/((?!_next|favicon.ico|api/).*)'],
|
|
1556
|
-
}
|
|
1458
|
+
```env
|
|
1459
|
+
AZIRID_API_URL=http://localhost:3000
|
|
1557
1460
|
```
|
|
1558
1461
|
|
|
1559
1462
|
---
|
|
1560
1463
|
|
|
1561
1464
|
## Server-side (Next.js App Router)
|
|
1562
1465
|
|
|
1563
|
-
For **Server Components**, **Server Actions**, and **Route Handlers**, use the `azirid-react/server` entry point to read the session token from the
|
|
1466
|
+
For **Server Components**, **Server Actions**, and **Route Handlers**, use the `azirid-react/server` entry point to read the session token from the `__session` cookie.
|
|
1564
1467
|
|
|
1565
1468
|
### Setup
|
|
1566
1469
|
|
|
@@ -1615,36 +1518,9 @@ export default async function DashboardPage() {
|
|
|
1615
1518
|
|
|
1616
1519
|
### Options
|
|
1617
1520
|
|
|
1618
|
-
| Option | Type | Default | Description
|
|
1619
|
-
| ------------ | -------- | ------------- |
|
|
1620
|
-
| `cookieName` | `string` | `"__session"` | Name of the
|
|
1621
|
-
|
|
1622
|
-
### `createSessionSyncHandler`
|
|
1623
|
-
|
|
1624
|
-
Creates a route handler that syncs the access token to a local httpOnly cookie. Useful for cross-origin development setups where the API is on a different domain.
|
|
1625
|
-
|
|
1626
|
-
```ts
|
|
1627
|
-
// app/api/auth/session/route.ts
|
|
1628
|
-
import { createSessionSyncHandler } from 'azirid-react/server'
|
|
1629
|
-
|
|
1630
|
-
export const { POST, DELETE } = createSessionSyncHandler()
|
|
1631
|
-
```
|
|
1632
|
-
|
|
1633
|
-
With custom options:
|
|
1634
|
-
|
|
1635
|
-
```ts
|
|
1636
|
-
export const { POST, DELETE } = createSessionSyncHandler({
|
|
1637
|
-
cookieName: '__session', // default
|
|
1638
|
-
secure: true, // set Secure flag on cookie
|
|
1639
|
-
maxAge: 3600, // cookie max age in seconds (default: 1h)
|
|
1640
|
-
})
|
|
1641
|
-
```
|
|
1642
|
-
|
|
1643
|
-
| Option | Type | Default | Description |
|
|
1644
|
-
| ------------ | --------- | ------------- | ----------------------------------- |
|
|
1645
|
-
| `cookieName` | `string` | `"__session"` | Name of the httpOnly cookie |
|
|
1646
|
-
| `secure` | `boolean` | `false` | Set the `Secure` flag on the cookie |
|
|
1647
|
-
| `maxAge` | `number` | `3600` | Cookie max age in seconds |
|
|
1521
|
+
| Option | Type | Default | Description |
|
|
1522
|
+
| ------------ | -------- | ------------- | ------------------------------------ |
|
|
1523
|
+
| `cookieName` | `string` | `"__session"` | Name of the cookie with the token |
|
|
1648
1524
|
|
|
1649
1525
|
---
|
|
1650
1526
|
|
|
@@ -1741,13 +1617,12 @@ const { mutate, isPending } = useUpdateProfile({
|
|
|
1741
1617
|
})
|
|
1742
1618
|
```
|
|
1743
1619
|
|
|
1744
|
-
### `
|
|
1620
|
+
### `AUTH_BASE_PATH` and `buildPaths`
|
|
1745
1621
|
|
|
1746
1622
|
```tsx
|
|
1747
|
-
import {
|
|
1623
|
+
import { AUTH_BASE_PATH, buildPaths } from 'azirid-react'
|
|
1748
1624
|
|
|
1749
|
-
|
|
1750
|
-
BASE_PATHS.direct // '/v1/users/auth'
|
|
1625
|
+
AUTH_BASE_PATH // '/v1/users/auth'
|
|
1751
1626
|
|
|
1752
1627
|
// Build custom path map from a base path
|
|
1753
1628
|
const paths = buildPaths('/custom/auth')
|
package/dist/index.cjs
CHANGED
|
@@ -131,19 +131,14 @@ var SUFFIXES = {
|
|
|
131
131
|
branches: "branches",
|
|
132
132
|
paymentMethods: "payment-methods"
|
|
133
133
|
};
|
|
134
|
-
var
|
|
135
|
-
/** Proxy mode (Next.js): requests go to the same origin via route handler */
|
|
136
|
-
proxy: "/api/auth",
|
|
137
|
-
/** Direct mode: requests go straight to the Azirid API */
|
|
138
|
-
direct: "/v1/users/auth"
|
|
139
|
-
};
|
|
134
|
+
var AUTH_BASE_PATH = "/v1/users/auth";
|
|
140
135
|
function buildPaths(basePath) {
|
|
141
136
|
const bp = basePath.replace(/\/$/, "");
|
|
142
137
|
return Object.fromEntries(
|
|
143
138
|
Object.entries(SUFFIXES).map(([key, suffix]) => [key, `${bp}/${suffix}`])
|
|
144
139
|
);
|
|
145
140
|
}
|
|
146
|
-
var PATHS = buildPaths(
|
|
141
|
+
var PATHS = buildPaths(AUTH_BASE_PATH);
|
|
147
142
|
function createAccessClient(config, appContext) {
|
|
148
143
|
const baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
149
144
|
const paths = config.basePath ? buildPaths(config.basePath) : PATHS;
|
|
@@ -210,6 +205,13 @@ function createAccessClient(config, appContext) {
|
|
|
210
205
|
}
|
|
211
206
|
function setAccessToken(token) {
|
|
212
207
|
accessToken = token;
|
|
208
|
+
if (typeof document !== "undefined") {
|
|
209
|
+
if (token) {
|
|
210
|
+
document.cookie = `__session=${token}; Path=/; SameSite=Lax; Max-Age=900`;
|
|
211
|
+
} else {
|
|
212
|
+
document.cookie = "__session=; Path=/; SameSite=Lax; Max-Age=0";
|
|
213
|
+
}
|
|
214
|
+
}
|
|
213
215
|
}
|
|
214
216
|
function getAccessToken() {
|
|
215
217
|
return accessToken;
|
|
@@ -776,40 +778,12 @@ function AziridProviderInner({
|
|
|
776
778
|
[props.locale, props.messages]
|
|
777
779
|
);
|
|
778
780
|
const brandingValue = react.useMemo(() => ({ branding, messages }), [branding, messages]);
|
|
779
|
-
const isProxyMode = !props.apiUrl;
|
|
780
|
-
const syncUrl = react.useMemo(() => {
|
|
781
|
-
if (props.sessionSyncUrl === false) return null;
|
|
782
|
-
if (props.sessionSyncUrl) return props.sessionSyncUrl;
|
|
783
|
-
if (isProxyMode) return null;
|
|
784
|
-
if (client.appContext?.publishableKey?.startsWith("pk_dev")) {
|
|
785
|
-
return "/api/auth/session";
|
|
786
|
-
}
|
|
787
|
-
return null;
|
|
788
|
-
}, [props.sessionSyncUrl, client.appContext?.publishableKey, isProxyMode]);
|
|
789
|
-
const syncSession = react.useCallback(
|
|
790
|
-
(token) => {
|
|
791
|
-
if (!syncUrl) return;
|
|
792
|
-
if (token) {
|
|
793
|
-
fetch(syncUrl, {
|
|
794
|
-
method: "POST",
|
|
795
|
-
headers: { "Content-Type": "application/json" },
|
|
796
|
-
body: JSON.stringify({ token })
|
|
797
|
-
}).catch(() => {
|
|
798
|
-
});
|
|
799
|
-
} else {
|
|
800
|
-
fetch(syncUrl, { method: "DELETE" }).catch(() => {
|
|
801
|
-
});
|
|
802
|
-
}
|
|
803
|
-
},
|
|
804
|
-
[syncUrl]
|
|
805
|
-
);
|
|
806
781
|
const updateAccessToken = react.useCallback(
|
|
807
782
|
(token) => {
|
|
808
783
|
setAccessToken(token);
|
|
809
784
|
client.setAccessToken(token);
|
|
810
|
-
syncSession(token);
|
|
811
785
|
},
|
|
812
|
-
[client
|
|
786
|
+
[client]
|
|
813
787
|
);
|
|
814
788
|
const saveSessionTokens = react.useCallback(
|
|
815
789
|
(data) => {
|
|
@@ -1013,11 +987,9 @@ function AziridProvider(props) {
|
|
|
1013
987
|
publishableKey: props.publishableKey,
|
|
1014
988
|
tenantId: props.tenantId
|
|
1015
989
|
} : void 0;
|
|
1016
|
-
const
|
|
1017
|
-
const baseUrl = isProxy ? "" : props.apiUrl.replace(/\/$/, "");
|
|
1018
|
-
const basePath = isProxy ? BASE_PATHS.proxy : BASE_PATHS.direct;
|
|
990
|
+
const baseUrl = (props.apiUrl ?? "https://api.azirid.com").replace(/\/$/, "");
|
|
1019
991
|
return createAccessClient(
|
|
1020
|
-
{ baseUrl, basePath, headers: props.fetchOptions },
|
|
992
|
+
{ baseUrl, basePath: AUTH_BASE_PATH, headers: props.fetchOptions },
|
|
1021
993
|
appContext
|
|
1022
994
|
);
|
|
1023
995
|
}, [props.publishableKey, props.tenantId, props.fetchOptions, props.apiUrl]);
|
|
@@ -4029,29 +4001,8 @@ function HandoffCallback({
|
|
|
4029
4001
|
onError?.(new Error("No API URL in handoff link"));
|
|
4030
4002
|
return;
|
|
4031
4003
|
}
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
method: "POST",
|
|
4035
|
-
headers: { "Content-Type": "application/json" },
|
|
4036
|
-
credentials: "include",
|
|
4037
|
-
body: JSON.stringify({ code })
|
|
4038
|
-
}).then(async (res) => {
|
|
4039
|
-
if (!res.ok) {
|
|
4040
|
-
const body = await res.json().catch(() => null);
|
|
4041
|
-
throw new Error(body?.error?.message ?? body?.message ?? "Handoff exchange failed");
|
|
4042
|
-
}
|
|
4043
|
-
return res.json();
|
|
4044
|
-
}).then((raw) => {
|
|
4045
|
-
const json = raw && typeof raw === "object" && "data" in raw && "meta" in raw ? raw.data : raw;
|
|
4046
|
-
json.at ?? json.accessToken;
|
|
4047
|
-
const refreshToken = json.rt ?? json.refreshToken;
|
|
4048
|
-
const csrfToken = json.xc ?? json.csrfToken;
|
|
4049
|
-
try {
|
|
4050
|
-
if (refreshToken) sessionStorage.setItem("__azrt", refreshToken);
|
|
4051
|
-
if (csrfToken) sessionStorage.setItem("__azxc", csrfToken);
|
|
4052
|
-
} catch {
|
|
4053
|
-
}
|
|
4054
|
-
onSuccess?.(json.user);
|
|
4004
|
+
performHandoff(apiUrl, code).then((user) => {
|
|
4005
|
+
onSuccess?.(user);
|
|
4055
4006
|
}).catch((err) => {
|
|
4056
4007
|
setStatus("error");
|
|
4057
4008
|
const error = err instanceof Error ? err : new Error("Handoff exchange failed");
|
|
@@ -4064,6 +4015,33 @@ function HandoffCallback({
|
|
|
4064
4015
|
}
|
|
4065
4016
|
return /* @__PURE__ */ jsxRuntime.jsx("div", { style: wrapperStyle, children: /* @__PURE__ */ jsxRuntime.jsx("p", { style: messageStyle2, children: loadingText }) });
|
|
4066
4017
|
}
|
|
4018
|
+
async function performHandoff(apiUrl, code) {
|
|
4019
|
+
const exchangeUrl = `${apiUrl.replace(/\/+$/, "")}/users/auth/handoff/exchange`;
|
|
4020
|
+
const res = await fetch(exchangeUrl, {
|
|
4021
|
+
method: "POST",
|
|
4022
|
+
headers: { "Content-Type": "application/json" },
|
|
4023
|
+
credentials: "include",
|
|
4024
|
+
body: JSON.stringify({ code })
|
|
4025
|
+
});
|
|
4026
|
+
if (!res.ok) {
|
|
4027
|
+
const body = await res.json().catch(() => null);
|
|
4028
|
+
throw new Error(body?.error?.message ?? body?.message ?? "Handoff exchange failed");
|
|
4029
|
+
}
|
|
4030
|
+
const raw = await res.json();
|
|
4031
|
+
const json = raw && typeof raw === "object" && "data" in raw && "meta" in raw ? raw.data : raw;
|
|
4032
|
+
const refreshToken = json.rt ?? json.refreshToken;
|
|
4033
|
+
const csrfToken = json.xc ?? json.csrfToken;
|
|
4034
|
+
try {
|
|
4035
|
+
if (refreshToken) sessionStorage.setItem("__azrt", refreshToken);
|
|
4036
|
+
if (csrfToken) sessionStorage.setItem("__azxc", csrfToken);
|
|
4037
|
+
} catch {
|
|
4038
|
+
}
|
|
4039
|
+
const at = json.at ?? json.accessToken;
|
|
4040
|
+
if (at) {
|
|
4041
|
+
document.cookie = `__session=${at}; Path=/; SameSite=Lax; Max-Age=900`;
|
|
4042
|
+
}
|
|
4043
|
+
return json.user;
|
|
4044
|
+
}
|
|
4067
4045
|
var wrapperStyle = {
|
|
4068
4046
|
display: "flex",
|
|
4069
4047
|
flexDirection: "column",
|
|
@@ -4875,11 +4853,11 @@ function usePasswordToggle() {
|
|
|
4875
4853
|
}
|
|
4876
4854
|
|
|
4877
4855
|
// src/index.ts
|
|
4878
|
-
var SDK_VERSION = "0.
|
|
4856
|
+
var SDK_VERSION = "0.14.0";
|
|
4879
4857
|
|
|
4858
|
+
exports.AUTH_BASE_PATH = AUTH_BASE_PATH;
|
|
4880
4859
|
exports.AuthForm = AuthForm;
|
|
4881
4860
|
exports.AziridProvider = AziridProvider;
|
|
4882
|
-
exports.BASE_PATHS = BASE_PATHS;
|
|
4883
4861
|
exports.CheckoutButton = CheckoutButton;
|
|
4884
4862
|
exports.ForgotPasswordForm = ForgotPasswordForm;
|
|
4885
4863
|
exports.HandoffCallback = HandoffCallback;
|