@reauth-dev/sdk 0.1.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.
@@ -0,0 +1,188 @@
1
+ import { e as ReauthServerConfig, A as AuthResult, f as RequestLike, d as UserDetails, h as ChargeOptions, i as DepositOptions, b as TransactionsPaginationOptions, B as BalanceTransaction } from './types-D8oOYbeC.mjs';
2
+
3
+ /**
4
+ * Create a reauth client for server-side authentication.
5
+ * Uses local JWT verification for fast, reliable auth checks.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { createServerClient } from '@reauth-dev/sdk/server';
10
+ *
11
+ * const reauth = createServerClient({
12
+ * domain: 'yourdomain.com',
13
+ * apiKey: 'sk_live_...',
14
+ * });
15
+ *
16
+ * // In your API route handler
17
+ * export async function GET(request: Request) {
18
+ * const result = await reauth.authenticate({
19
+ * headers: {
20
+ * authorization: request.headers.get('authorization') ?? undefined,
21
+ * cookie: request.headers.get('cookie') ?? undefined,
22
+ * },
23
+ * });
24
+ *
25
+ * if (!result.valid || !result.user) {
26
+ * return Response.json({ error: result.error || 'Unauthorized' }, { status: 401 });
27
+ * }
28
+ *
29
+ * // Access user info from JWT claims (no network call needed!)
30
+ * console.log('User ID:', result.user.id);
31
+ * console.log('Roles:', result.user.roles);
32
+ * console.log('Subscription:', result.user.subscription);
33
+ *
34
+ * return Response.json({ user: result.user });
35
+ * }
36
+ * ```
37
+ */
38
+ declare function createServerClient(config: ReauthServerConfig): {
39
+ /**
40
+ * Verify a JWT token locally using HKDF-derived secret.
41
+ * No network call required - fast and reliable.
42
+ *
43
+ * The domain_id is extracted from the token claims automatically.
44
+ *
45
+ * @param token - The JWT token to verify
46
+ * @returns AuthResult with user info from claims
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const result = await reauth.verifyToken(token);
51
+ * if (result.valid && result.user) {
52
+ * console.log('User ID:', result.user.id);
53
+ * console.log('Roles:', result.user.roles);
54
+ * console.log('Subscription:', result.user.subscription);
55
+ * }
56
+ * ```
57
+ */
58
+ verifyToken(token: string): Promise<AuthResult>;
59
+ /**
60
+ * Extract a token from a request object.
61
+ * Tries Authorization: Bearer header first, then falls back to cookies.
62
+ *
63
+ * @param request - Object with headers (authorization and/or cookie)
64
+ * @returns The token string or null if not found
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const token = reauth.extractToken({
69
+ * headers: {
70
+ * authorization: req.headers.authorization,
71
+ * cookie: req.headers.cookie,
72
+ * },
73
+ * });
74
+ * ```
75
+ */
76
+ extractToken(request: RequestLike): string | null;
77
+ /**
78
+ * Authenticate a request by extracting and verifying the token.
79
+ * This is a convenience method combining extractToken and verifyToken.
80
+ *
81
+ * @param request - Object with headers (authorization and/or cookie)
82
+ * @returns AuthResult with user info from claims
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * // Express/Node.js
87
+ * async function authMiddleware(req, res, next) {
88
+ * const result = await reauth.authenticate({
89
+ * headers: {
90
+ * authorization: req.headers.authorization,
91
+ * cookie: req.headers.cookie,
92
+ * },
93
+ * });
94
+ *
95
+ * if (!result.valid || !result.user) {
96
+ * res.status(401).json({ error: result.error || 'Unauthorized' });
97
+ * return;
98
+ * }
99
+ *
100
+ * req.user = result.user;
101
+ * next();
102
+ * }
103
+ *
104
+ * // Next.js App Router
105
+ * export async function GET(request: NextRequest) {
106
+ * const result = await reauth.authenticate({
107
+ * headers: {
108
+ * authorization: request.headers.get('authorization') ?? undefined,
109
+ * cookie: request.headers.get('cookie') ?? undefined,
110
+ * },
111
+ * });
112
+ *
113
+ * if (!result.valid) {
114
+ * return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
115
+ * }
116
+ *
117
+ * return NextResponse.json({ user: result.user });
118
+ * }
119
+ * ```
120
+ */
121
+ authenticate(request: RequestLike): Promise<AuthResult>;
122
+ /**
123
+ * Get user details by ID from the backend.
124
+ * Use this when you need full user info like email, frozen status, etc.
125
+ * that isn't available in the JWT claims.
126
+ *
127
+ * @param userId - The user ID to fetch
128
+ * @returns UserDetails or null if not found
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * // After verifying token, fetch full user details if needed
133
+ * const result = await reauth.authenticate(request);
134
+ * if (result.valid && result.user) {
135
+ * // If you need email or other details not in JWT
136
+ * const details = await reauth.getUserById(result.user.id);
137
+ * if (details) {
138
+ * console.log('Email:', details.email);
139
+ * console.log('Frozen:', details.isFrozen);
140
+ * }
141
+ * }
142
+ * ```
143
+ */
144
+ getUserById(userId: string): Promise<UserDetails | null>;
145
+ /**
146
+ * Get a user's current balance.
147
+ *
148
+ * @param userId - The user ID to check
149
+ * @returns Object with the current balance
150
+ */
151
+ getBalance(userId: string): Promise<{
152
+ balance: number;
153
+ }>;
154
+ /**
155
+ * Charge (deduct) credits from a user's balance.
156
+ *
157
+ * @param userId - The user ID to charge
158
+ * @param opts - Charge options (amount, requestUuid for idempotency, optional note)
159
+ * @returns Object with the new balance after charge
160
+ * @throws Error with status 402 if insufficient balance, 400 if invalid amount
161
+ */
162
+ charge(userId: string, opts: ChargeOptions): Promise<{
163
+ newBalance: number;
164
+ }>;
165
+ /**
166
+ * Deposit (add) credits to a user's balance.
167
+ *
168
+ * @param userId - The user ID to deposit to
169
+ * @param opts - Deposit options (amount, requestUuid for idempotency, optional note)
170
+ * @returns Object with the new balance after deposit
171
+ */
172
+ deposit(userId: string, opts: DepositOptions): Promise<{
173
+ newBalance: number;
174
+ }>;
175
+ /**
176
+ * Get a user's balance transaction history.
177
+ *
178
+ * @param userId - The user ID to get transactions for
179
+ * @param opts - Optional pagination (limit, offset)
180
+ * @returns Object with array of transactions (newest first)
181
+ */
182
+ getTransactions(userId: string, opts?: TransactionsPaginationOptions): Promise<{
183
+ transactions: BalanceTransaction[];
184
+ }>;
185
+ };
186
+ type ServerClient = ReturnType<typeof createServerClient>;
187
+
188
+ export { type ServerClient, createServerClient };
@@ -0,0 +1,188 @@
1
+ import { e as ReauthServerConfig, A as AuthResult, f as RequestLike, d as UserDetails, h as ChargeOptions, i as DepositOptions, b as TransactionsPaginationOptions, B as BalanceTransaction } from './types-D8oOYbeC.js';
2
+
3
+ /**
4
+ * Create a reauth client for server-side authentication.
5
+ * Uses local JWT verification for fast, reliable auth checks.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { createServerClient } from '@reauth-dev/sdk/server';
10
+ *
11
+ * const reauth = createServerClient({
12
+ * domain: 'yourdomain.com',
13
+ * apiKey: 'sk_live_...',
14
+ * });
15
+ *
16
+ * // In your API route handler
17
+ * export async function GET(request: Request) {
18
+ * const result = await reauth.authenticate({
19
+ * headers: {
20
+ * authorization: request.headers.get('authorization') ?? undefined,
21
+ * cookie: request.headers.get('cookie') ?? undefined,
22
+ * },
23
+ * });
24
+ *
25
+ * if (!result.valid || !result.user) {
26
+ * return Response.json({ error: result.error || 'Unauthorized' }, { status: 401 });
27
+ * }
28
+ *
29
+ * // Access user info from JWT claims (no network call needed!)
30
+ * console.log('User ID:', result.user.id);
31
+ * console.log('Roles:', result.user.roles);
32
+ * console.log('Subscription:', result.user.subscription);
33
+ *
34
+ * return Response.json({ user: result.user });
35
+ * }
36
+ * ```
37
+ */
38
+ declare function createServerClient(config: ReauthServerConfig): {
39
+ /**
40
+ * Verify a JWT token locally using HKDF-derived secret.
41
+ * No network call required - fast and reliable.
42
+ *
43
+ * The domain_id is extracted from the token claims automatically.
44
+ *
45
+ * @param token - The JWT token to verify
46
+ * @returns AuthResult with user info from claims
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const result = await reauth.verifyToken(token);
51
+ * if (result.valid && result.user) {
52
+ * console.log('User ID:', result.user.id);
53
+ * console.log('Roles:', result.user.roles);
54
+ * console.log('Subscription:', result.user.subscription);
55
+ * }
56
+ * ```
57
+ */
58
+ verifyToken(token: string): Promise<AuthResult>;
59
+ /**
60
+ * Extract a token from a request object.
61
+ * Tries Authorization: Bearer header first, then falls back to cookies.
62
+ *
63
+ * @param request - Object with headers (authorization and/or cookie)
64
+ * @returns The token string or null if not found
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const token = reauth.extractToken({
69
+ * headers: {
70
+ * authorization: req.headers.authorization,
71
+ * cookie: req.headers.cookie,
72
+ * },
73
+ * });
74
+ * ```
75
+ */
76
+ extractToken(request: RequestLike): string | null;
77
+ /**
78
+ * Authenticate a request by extracting and verifying the token.
79
+ * This is a convenience method combining extractToken and verifyToken.
80
+ *
81
+ * @param request - Object with headers (authorization and/or cookie)
82
+ * @returns AuthResult with user info from claims
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * // Express/Node.js
87
+ * async function authMiddleware(req, res, next) {
88
+ * const result = await reauth.authenticate({
89
+ * headers: {
90
+ * authorization: req.headers.authorization,
91
+ * cookie: req.headers.cookie,
92
+ * },
93
+ * });
94
+ *
95
+ * if (!result.valid || !result.user) {
96
+ * res.status(401).json({ error: result.error || 'Unauthorized' });
97
+ * return;
98
+ * }
99
+ *
100
+ * req.user = result.user;
101
+ * next();
102
+ * }
103
+ *
104
+ * // Next.js App Router
105
+ * export async function GET(request: NextRequest) {
106
+ * const result = await reauth.authenticate({
107
+ * headers: {
108
+ * authorization: request.headers.get('authorization') ?? undefined,
109
+ * cookie: request.headers.get('cookie') ?? undefined,
110
+ * },
111
+ * });
112
+ *
113
+ * if (!result.valid) {
114
+ * return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
115
+ * }
116
+ *
117
+ * return NextResponse.json({ user: result.user });
118
+ * }
119
+ * ```
120
+ */
121
+ authenticate(request: RequestLike): Promise<AuthResult>;
122
+ /**
123
+ * Get user details by ID from the backend.
124
+ * Use this when you need full user info like email, frozen status, etc.
125
+ * that isn't available in the JWT claims.
126
+ *
127
+ * @param userId - The user ID to fetch
128
+ * @returns UserDetails or null if not found
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * // After verifying token, fetch full user details if needed
133
+ * const result = await reauth.authenticate(request);
134
+ * if (result.valid && result.user) {
135
+ * // If you need email or other details not in JWT
136
+ * const details = await reauth.getUserById(result.user.id);
137
+ * if (details) {
138
+ * console.log('Email:', details.email);
139
+ * console.log('Frozen:', details.isFrozen);
140
+ * }
141
+ * }
142
+ * ```
143
+ */
144
+ getUserById(userId: string): Promise<UserDetails | null>;
145
+ /**
146
+ * Get a user's current balance.
147
+ *
148
+ * @param userId - The user ID to check
149
+ * @returns Object with the current balance
150
+ */
151
+ getBalance(userId: string): Promise<{
152
+ balance: number;
153
+ }>;
154
+ /**
155
+ * Charge (deduct) credits from a user's balance.
156
+ *
157
+ * @param userId - The user ID to charge
158
+ * @param opts - Charge options (amount, requestUuid for idempotency, optional note)
159
+ * @returns Object with the new balance after charge
160
+ * @throws Error with status 402 if insufficient balance, 400 if invalid amount
161
+ */
162
+ charge(userId: string, opts: ChargeOptions): Promise<{
163
+ newBalance: number;
164
+ }>;
165
+ /**
166
+ * Deposit (add) credits to a user's balance.
167
+ *
168
+ * @param userId - The user ID to deposit to
169
+ * @param opts - Deposit options (amount, requestUuid for idempotency, optional note)
170
+ * @returns Object with the new balance after deposit
171
+ */
172
+ deposit(userId: string, opts: DepositOptions): Promise<{
173
+ newBalance: number;
174
+ }>;
175
+ /**
176
+ * Get a user's balance transaction history.
177
+ *
178
+ * @param userId - The user ID to get transactions for
179
+ * @param opts - Optional pagination (limit, offset)
180
+ * @returns Object with array of transactions (newest first)
181
+ */
182
+ getTransactions(userId: string, opts?: TransactionsPaginationOptions): Promise<{
183
+ transactions: BalanceTransaction[];
184
+ }>;
185
+ };
186
+ type ServerClient = ReturnType<typeof createServerClient>;
187
+
188
+ export { type ServerClient, createServerClient };