@qyh213/easyauth-client 1.0.0-beta.1
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/LICENSE +21 -0
- package/README.md +425 -0
- package/dist/chunk-H65ZLXQJ.mjs +495 -0
- package/dist/client-RPDHpVsj.d.mts +300 -0
- package/dist/client-RPDHpVsj.d.ts +300 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +530 -0
- package/dist/index.mjs +18 -0
- package/dist/next.d.mts +106 -0
- package/dist/next.d.ts +106 -0
- package/dist/next.js +638 -0
- package/dist/next.mjs +124 -0
- package/dist/react.d.mts +68 -0
- package/dist/react.d.ts +68 -0
- package/dist/react.js +733 -0
- package/dist/react.mjs +220 -0
- package/package.json +99 -0
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
import { e as EasyAuthConfig } from './client-RPDHpVsj.js';
|
|
4
|
+
export { A as ApiKey, a as AuthToken, C as CreateApiKeyRequest, b as CreateApiKeyResponse, c as CreateScopeRequest, d as CreateUserRequest, E as EasyAuthClient, f as EasyAuthError, I as IntrospectRequest, g as IntrospectResponse, L as LocalStorageTokenStorage, h as LoginCredentials, M as MemoryTokenStorage, O as OnboardRequest, i as OnboardResponse, R as RegisterWebhookRequest, S as Scope, j as ScopeError, k as Service, T as TokenStorage, U as User, l as UserListResponse, V as ValidationResult, W as Webhook } from './client-RPDHpVsj.js';
|
|
5
|
+
|
|
6
|
+
interface MiddlewareOptions {
|
|
7
|
+
/** Base URL of Easy Auth service */
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
/** Service API Key */
|
|
10
|
+
apiKey: string;
|
|
11
|
+
/** Paths that require authentication */
|
|
12
|
+
protectedPaths?: string[];
|
|
13
|
+
/** Paths that require specific scopes: path -> scopes[] */
|
|
14
|
+
scopedPaths?: Record<string, string[]>;
|
|
15
|
+
/** Paths that are public (no auth required) */
|
|
16
|
+
publicPaths?: string[];
|
|
17
|
+
/** Login page path */
|
|
18
|
+
loginPath?: string;
|
|
19
|
+
/** Forbidden page path (insufficient scopes) */
|
|
20
|
+
forbiddenPath?: string;
|
|
21
|
+
}
|
|
22
|
+
interface MiddlewareContext {
|
|
23
|
+
valid: boolean;
|
|
24
|
+
userId?: string;
|
|
25
|
+
email?: string;
|
|
26
|
+
scopes?: string[];
|
|
27
|
+
error?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create a Next.js middleware with scope-based protection
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // middleware.ts
|
|
34
|
+
* import { createEasyAuthMiddleware } from "@easyauth/client/next";
|
|
35
|
+
*
|
|
36
|
+
* export const middleware = createEasyAuthMiddleware({
|
|
37
|
+
* baseUrl: process.env.EASY_AUTH_URL!,
|
|
38
|
+
* apiKey: process.env.EASY_AUTH_API_KEY!,
|
|
39
|
+
* protectedPaths: ["/dashboard", "/api/protected"],
|
|
40
|
+
* scopedPaths: {
|
|
41
|
+
* "/api/users": ["users:read"],
|
|
42
|
+
* "/api/users/create": ["users:write"],
|
|
43
|
+
* "/api/billing": ["billing:read"],
|
|
44
|
+
* "/api/admin": ["admin"],
|
|
45
|
+
* },
|
|
46
|
+
* publicPaths: ["/", "/login", "/register"],
|
|
47
|
+
* });
|
|
48
|
+
*/
|
|
49
|
+
declare function createEasyAuthMiddleware(options: MiddlewareOptions): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
50
|
+
/**
|
|
51
|
+
* Higher-order component to protect a page with required scopes
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // pages/admin.tsx
|
|
55
|
+
* import { withScope } from "@easyauth/client/next";
|
|
56
|
+
*
|
|
57
|
+
* function AdminPage() {
|
|
58
|
+
* return <div>Admin Only</div>;
|
|
59
|
+
* }
|
|
60
|
+
*
|
|
61
|
+
* export default withScope(AdminPage, ["admin"], {
|
|
62
|
+
* baseUrl: process.env.NEXT_PUBLIC_EASY_AUTH_URL!,
|
|
63
|
+
* apiKey: process.env.NEXT_PUBLIC_EASY_AUTH_API_KEY!,
|
|
64
|
+
* });
|
|
65
|
+
*/
|
|
66
|
+
declare function withScope<P extends object>(Component: React.ComponentType<P>, _requiredScopes: string[], _config: EasyAuthConfig): (props: P) => react_jsx_runtime.JSX.Element;
|
|
67
|
+
/**
|
|
68
|
+
* Server-side helper to validate scopes in API routes
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* // app/api/users/route.ts
|
|
72
|
+
* import { validateScopes } from "@easyauth/client/next";
|
|
73
|
+
*
|
|
74
|
+
* export async function GET(request: Request) {
|
|
75
|
+
* const result = await validateScopes(request, {
|
|
76
|
+
* baseUrl: process.env.EASY_AUTH_URL!,
|
|
77
|
+
* apiKey: process.env.EASY_AUTH_API_KEY!,
|
|
78
|
+
* requiredScopes: ["users:read"],
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* if (!result.valid) {
|
|
82
|
+
* return new Response(JSON.stringify({ error: result.error }), {
|
|
83
|
+
* status: result.status || 401,
|
|
84
|
+
* });
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* // Proceed with the request
|
|
88
|
+
* return Response.json({ users: [] });
|
|
89
|
+
* }
|
|
90
|
+
*/
|
|
91
|
+
declare function validateScopes(request: Request, options: {
|
|
92
|
+
baseUrl: string;
|
|
93
|
+
apiKey: string;
|
|
94
|
+
requiredScopes: string[];
|
|
95
|
+
}): Promise<{
|
|
96
|
+
valid: true;
|
|
97
|
+
userId: string;
|
|
98
|
+
email: string;
|
|
99
|
+
scopes: string[];
|
|
100
|
+
} | {
|
|
101
|
+
valid: false;
|
|
102
|
+
error: string;
|
|
103
|
+
status: number;
|
|
104
|
+
}>;
|
|
105
|
+
|
|
106
|
+
export { EasyAuthConfig, type MiddlewareContext, type MiddlewareOptions, createEasyAuthMiddleware, validateScopes, withScope };
|