@drmhse/authos-react 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.
- package/dist/index.d.mts +372 -0
- package/dist/index.d.ts +372 -0
- package/dist/index.js +555 -0
- package/dist/index.mjs +530 -0
- package/dist/nextjs.d.mts +146 -0
- package/dist/nextjs.d.ts +146 -0
- package/dist/nextjs.js +5878 -0
- package/dist/nextjs.mjs +5873 -0
- package/package.json +76 -0
package/dist/nextjs.d.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
|
|
3
|
+
interface AuthMiddlewareConfig {
|
|
4
|
+
/**
|
|
5
|
+
* The JWKS URL for token verification
|
|
6
|
+
*/
|
|
7
|
+
jwksUrl: string;
|
|
8
|
+
/**
|
|
9
|
+
* Protected routes that require authentication
|
|
10
|
+
*/
|
|
11
|
+
protectedRoutes?: string[];
|
|
12
|
+
/**
|
|
13
|
+
* Public routes that don't require authentication
|
|
14
|
+
*/
|
|
15
|
+
publicRoutes?: string[];
|
|
16
|
+
/**
|
|
17
|
+
* The URL to redirect to when not authenticated
|
|
18
|
+
*/
|
|
19
|
+
signInUrl?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Cookie name for the access token
|
|
22
|
+
*/
|
|
23
|
+
tokenCookie?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates an authentication middleware for Next.js Edge Runtime.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* // middleware.ts
|
|
31
|
+
* import { authMiddleware } from '@drmhse/authos-react/nextjs';
|
|
32
|
+
*
|
|
33
|
+
* export default authMiddleware({
|
|
34
|
+
* jwksUrl: 'https://auth.example.com/.well-known/jwks.json',
|
|
35
|
+
* protectedRoutes: ['/dashboard/*', '/settings/*'],
|
|
36
|
+
* publicRoutes: ['/', '/about', '/pricing'],
|
|
37
|
+
* signInUrl: '/signin',
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* export const config = {
|
|
41
|
+
* matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
|
|
42
|
+
* };
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare function authMiddleware(config: AuthMiddlewareConfig): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* User information extracted from the request in server components.
|
|
49
|
+
*/
|
|
50
|
+
interface AuthUser {
|
|
51
|
+
id: string;
|
|
52
|
+
email: string;
|
|
53
|
+
org?: string;
|
|
54
|
+
permissions: string[];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Authentication state for server components.
|
|
58
|
+
*/
|
|
59
|
+
interface AuthState {
|
|
60
|
+
userId: string | null;
|
|
61
|
+
orgSlug: string | null;
|
|
62
|
+
isAuthenticated: boolean;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get the current authenticated user in a React Server Component.
|
|
66
|
+
* This reads from the headers set by the authMiddleware.
|
|
67
|
+
*
|
|
68
|
+
* @returns The current user or null if not authenticated
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```tsx
|
|
72
|
+
* // app/dashboard/page.tsx
|
|
73
|
+
* import { currentUser } from '@drmhse/authos-react/nextjs';
|
|
74
|
+
*
|
|
75
|
+
* export default async function DashboardPage() {
|
|
76
|
+
* const user = await currentUser();
|
|
77
|
+
*
|
|
78
|
+
* if (!user) {
|
|
79
|
+
* redirect('/signin');
|
|
80
|
+
* }
|
|
81
|
+
*
|
|
82
|
+
* return <div>Welcome, {user.email}!</div>;
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare function currentUser(): Promise<AuthUser | null>;
|
|
87
|
+
/**
|
|
88
|
+
* Get the current authentication state in a React Server Component.
|
|
89
|
+
* Provides basic auth info without fetching full user details.
|
|
90
|
+
*
|
|
91
|
+
* @returns The current auth state
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```tsx
|
|
95
|
+
* // app/layout.tsx
|
|
96
|
+
* import { auth } from '@drmhse/authos-react/nextjs';
|
|
97
|
+
*
|
|
98
|
+
* export default async function RootLayout({ children }) {
|
|
99
|
+
* const { isAuthenticated, userId } = await auth();
|
|
100
|
+
*
|
|
101
|
+
* return (
|
|
102
|
+
* <html>
|
|
103
|
+
* <body>
|
|
104
|
+
* <nav>
|
|
105
|
+
* {isAuthenticated ? (
|
|
106
|
+
* <UserMenu userId={userId} />
|
|
107
|
+
* ) : (
|
|
108
|
+
* <a href="/signin">Sign In</a>
|
|
109
|
+
* )}
|
|
110
|
+
* </nav>
|
|
111
|
+
* {children}
|
|
112
|
+
* </body>
|
|
113
|
+
* </html>
|
|
114
|
+
* );
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
declare function auth(): Promise<AuthState>;
|
|
119
|
+
/**
|
|
120
|
+
* Get the access token from cookies in a server component.
|
|
121
|
+
* Useful when you need to make authenticated API calls from the server.
|
|
122
|
+
*
|
|
123
|
+
* @param cookieName - The name of the token cookie (default: 'authos_token')
|
|
124
|
+
* @returns The access token or null
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```tsx
|
|
128
|
+
* // app/api/data/route.ts
|
|
129
|
+
* import { getToken } from '@drmhse/authos-react/nextjs';
|
|
130
|
+
*
|
|
131
|
+
* export async function GET() {
|
|
132
|
+
* const token = await getToken();
|
|
133
|
+
*
|
|
134
|
+
* if (!token) {
|
|
135
|
+
* return Response.json({ error: 'Unauthorized' }, { status: 401 });
|
|
136
|
+
* }
|
|
137
|
+
*
|
|
138
|
+
* // Use token for backend API calls
|
|
139
|
+
* const data = await fetchFromAPI(token);
|
|
140
|
+
* return Response.json(data);
|
|
141
|
+
* }
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
declare function getToken(cookieName?: string): Promise<string | null>;
|
|
145
|
+
|
|
146
|
+
export { type AuthMiddlewareConfig, type AuthState, type AuthUser, auth, authMiddleware, currentUser, getToken };
|