@basictech/nextjs 0.6.0-beta.6 → 0.6.0-beta.7
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/.turbo/turbo-build.log +17 -11
- package/CHANGELOG.md +11 -0
- package/dist/client.d.mts +1 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +46 -0
- package/dist/client.js.map +1 -0
- package/dist/client.mjs +24 -0
- package/dist/client.mjs.map +1 -0
- package/dist/index.d.mts +81 -8
- package/dist/index.d.ts +81 -8
- package/dist/index.js +69 -92
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -81
- package/dist/index.mjs.map +1 -1
- package/package.json +24 -3
- package/readme.md +623 -53
- package/src/client.ts +33 -0
- package/src/{componets.tsx → components.tsx} +8 -8
- package/src/index.ts +35 -11
- package/src/middleware.ts +186 -0
- package/tsup.config.ts +1 -1
- package/src/sync.ts +0 -75
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @basictech/nextjs@0.6.0-beta.
|
|
3
|
+
> @basictech/nextjs@0.6.0-beta.6 build
|
|
4
4
|
> tsup
|
|
5
5
|
|
|
6
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Building entry: src/index.ts, src/client.ts
|
|
7
7
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
8
8
|
[34mCLI[39m tsup v7.2.0
|
|
9
9
|
[34mCLI[39m Using tsup config: /Users/raz/codebook/basic/libs/client-ts/packages/nextjs/tsup.config.ts
|
|
@@ -11,13 +11,19 @@
|
|
|
11
11
|
[34mCLI[39m Cleaning output folder
|
|
12
12
|
[34mCJS[39m Build start
|
|
13
13
|
[34mESM[39m Build start
|
|
14
|
-
[32mESM[39m [1mdist/index.mjs
|
|
15
|
-
[32mESM[39m [1mdist/
|
|
16
|
-
[32mESM[39m
|
|
17
|
-
[
|
|
18
|
-
[
|
|
19
|
-
[32mCJS[39m
|
|
14
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m2.22 KB[39m
|
|
15
|
+
[32mESM[39m [1mdist/client.mjs [22m[32m375.00 B[39m
|
|
16
|
+
[32mESM[39m [1mdist/index.mjs.map [22m[32m6.68 KB[39m
|
|
17
|
+
[32mESM[39m [1mdist/client.mjs.map [22m[32m933.00 B[39m
|
|
18
|
+
[32mESM[39m ⚡️ Build success in 19ms
|
|
19
|
+
[32mCJS[39m [1mdist/client.js [22m[32m1.60 KB[39m
|
|
20
|
+
[32mCJS[39m [1mdist/index.js [22m[32m3.43 KB[39m
|
|
21
|
+
[32mCJS[39m [1mdist/client.js.map [22m[32m927.00 B[39m
|
|
22
|
+
[32mCJS[39m [1mdist/index.js.map [22m[32m7.77 KB[39m
|
|
23
|
+
[32mCJS[39m ⚡️ Build success in 20ms
|
|
20
24
|
[34mDTS[39m Build start
|
|
21
|
-
[32mDTS[39m ⚡️ Build success in
|
|
22
|
-
[32mDTS[39m [1mdist/
|
|
23
|
-
[32mDTS[39m [1mdist/index.d.
|
|
25
|
+
[32mDTS[39m ⚡️ Build success in 1866ms
|
|
26
|
+
[32mDTS[39m [1mdist/client.d.ts [22m[32m303.00 B[39m
|
|
27
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m2.47 KB[39m
|
|
28
|
+
[32mDTS[39m [1mdist/client.d.mts [22m[32m303.00 B[39m
|
|
29
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m2.47 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { AuthConfig, AuthError, AuthResult, BasicContextType, BasicDB, BasicProvider, BasicProviderProps, BasicStorage, Collection, DBMode, LocalStorageAdapter, NotAuthenticatedError, RemoteCollection, RemoteDB, RemoteDBConfig, RemoteDBError, STORAGE_KEYS, useBasic, useQuery } from '@basictech/react';
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { AuthConfig, AuthError, AuthResult, BasicContextType, BasicDB, BasicProvider, BasicProviderProps, BasicStorage, Collection, DBMode, LocalStorageAdapter, NotAuthenticatedError, RemoteCollection, RemoteDB, RemoteDBConfig, RemoteDBError, STORAGE_KEYS, useBasic, useQuery } from '@basictech/react';
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/client.ts
|
|
22
|
+
var client_exports = {};
|
|
23
|
+
__export(client_exports, {
|
|
24
|
+
BasicProvider: () => import_react.BasicProvider,
|
|
25
|
+
NotAuthenticatedError: () => import_react.NotAuthenticatedError,
|
|
26
|
+
RemoteCollection: () => import_react.RemoteCollection,
|
|
27
|
+
RemoteDB: () => import_react.RemoteDB,
|
|
28
|
+
RemoteDBError: () => import_react.RemoteDBError,
|
|
29
|
+
STORAGE_KEYS: () => import_react.STORAGE_KEYS,
|
|
30
|
+
useBasic: () => import_react.useBasic,
|
|
31
|
+
useQuery: () => import_react.useQuery
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(client_exports);
|
|
34
|
+
var import_react = require("@basictech/react");
|
|
35
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
+
0 && (module.exports = {
|
|
37
|
+
BasicProvider,
|
|
38
|
+
NotAuthenticatedError,
|
|
39
|
+
RemoteCollection,
|
|
40
|
+
RemoteDB,
|
|
41
|
+
RemoteDBError,
|
|
42
|
+
STORAGE_KEYS,
|
|
43
|
+
useBasic,
|
|
44
|
+
useQuery
|
|
45
|
+
});
|
|
46
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["'use client'\n\n// @basictech/nextjs/client - Client-side exports for Next.js\n// Use this in client components (files with 'use client' directive)\n\n// Re-export everything from @basictech/react for client-side use\nexport {\n useBasic,\n BasicProvider,\n useQuery,\n RemoteDB,\n RemoteCollection,\n RemoteDBError,\n NotAuthenticatedError,\n STORAGE_KEYS\n} from \"@basictech/react\"\n\n// Re-export types (DBMode is also a type)\nexport type {\n AuthConfig,\n BasicStorage,\n LocalStorageAdapter,\n BasicProviderProps,\n BasicContextType,\n AuthResult,\n BasicDB,\n Collection,\n RemoteDBConfig,\n AuthError\n} from \"@basictech/react\"\n\n// DBMode is a type-only export\nexport type { DBMode } from \"@basictech/react\"\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,mBASO;","names":[]}
|
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/client.ts
|
|
4
|
+
import {
|
|
5
|
+
useBasic,
|
|
6
|
+
BasicProvider,
|
|
7
|
+
useQuery,
|
|
8
|
+
RemoteDB,
|
|
9
|
+
RemoteCollection,
|
|
10
|
+
RemoteDBError,
|
|
11
|
+
NotAuthenticatedError,
|
|
12
|
+
STORAGE_KEYS
|
|
13
|
+
} from "@basictech/react";
|
|
14
|
+
export {
|
|
15
|
+
BasicProvider,
|
|
16
|
+
NotAuthenticatedError,
|
|
17
|
+
RemoteCollection,
|
|
18
|
+
RemoteDB,
|
|
19
|
+
RemoteDBError,
|
|
20
|
+
STORAGE_KEYS,
|
|
21
|
+
useBasic,
|
|
22
|
+
useQuery
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["'use client'\n\n// @basictech/nextjs/client - Client-side exports for Next.js\n// Use this in client components (files with 'use client' directive)\n\n// Re-export everything from @basictech/react for client-side use\nexport {\n useBasic,\n BasicProvider,\n useQuery,\n RemoteDB,\n RemoteCollection,\n RemoteDBError,\n NotAuthenticatedError,\n STORAGE_KEYS\n} from \"@basictech/react\"\n\n// Re-export types (DBMode is also a type)\nexport type {\n AuthConfig,\n BasicStorage,\n LocalStorageAdapter,\n BasicProviderProps,\n BasicContextType,\n AuthResult,\n BasicDB,\n Collection,\n RemoteDBConfig,\n AuthError\n} from \"@basictech/react\"\n\n// DBMode is a type-only export\nexport type { DBMode } from \"@basictech/react\"\n"],"mappings":";;;AAMA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":[]}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
export { useBasic, useQuery } from '@basictech/react';
|
|
4
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
1
|
+
export { AuthConfig, AuthError, AuthResult, BasicContextType, BasicDB, BasicProviderProps, BasicStorage, Collection, DBMode, LocalStorageAdapter, RemoteDBConfig } from '@basictech/react';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
5
3
|
|
|
6
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for the Basic auth middleware
|
|
6
|
+
*/
|
|
7
|
+
interface BasicMiddlewareConfig {
|
|
8
|
+
/**
|
|
9
|
+
* Routes that require authentication
|
|
10
|
+
* Supports glob patterns like '/dashboard/*', '/api/protected/*'
|
|
11
|
+
*/
|
|
12
|
+
protectedRoutes?: string[];
|
|
13
|
+
/**
|
|
14
|
+
* Routes that are always public (bypass auth check)
|
|
15
|
+
* Useful for login pages, public APIs, etc.
|
|
16
|
+
*/
|
|
17
|
+
publicRoutes?: string[];
|
|
18
|
+
/**
|
|
19
|
+
* Where to redirect unauthenticated users
|
|
20
|
+
* @default '/login'
|
|
21
|
+
*/
|
|
22
|
+
signInUrl?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Where to redirect after successful sign-in
|
|
25
|
+
* @default '/'
|
|
26
|
+
*/
|
|
27
|
+
afterSignInUrl?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Cookie name for the access token
|
|
30
|
+
* @default 'basic_access_token'
|
|
31
|
+
*/
|
|
32
|
+
tokenCookieName?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Cookie name for the full token object
|
|
35
|
+
* @default 'basic_token'
|
|
36
|
+
*/
|
|
37
|
+
fullTokenCookieName?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Get auth info from cookies
|
|
41
|
+
*/
|
|
42
|
+
declare function getAuthFromRequest(request: NextRequest, config?: Partial<BasicMiddlewareConfig>): {
|
|
43
|
+
isAuthenticated: boolean;
|
|
44
|
+
token: string | null;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Create a Basic auth middleware for NextJS
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* // In middleware.ts at the root of your NextJS app:
|
|
51
|
+
* import { createBasicMiddleware } from '@basictech/nextjs'
|
|
52
|
+
*
|
|
53
|
+
* export const middleware = createBasicMiddleware({
|
|
54
|
+
* protectedRoutes: ['/dashboard/*', '/settings/*'],
|
|
55
|
+
* publicRoutes: ['/login', '/signup', '/'],
|
|
56
|
+
* signInUrl: '/login'
|
|
57
|
+
* })
|
|
58
|
+
*
|
|
59
|
+
* export const config = {
|
|
60
|
+
* matcher: ['/((?!_next/static|_next/image|favicon.ico).*)']
|
|
61
|
+
* }
|
|
62
|
+
*/
|
|
63
|
+
declare function createBasicMiddleware(config?: BasicMiddlewareConfig): (request: NextRequest) => NextResponse;
|
|
64
|
+
/**
|
|
65
|
+
* Simple auth check middleware - redirects unauthenticated users
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // In middleware.ts
|
|
69
|
+
* import { withBasicAuth } from '@basictech/nextjs'
|
|
70
|
+
*
|
|
71
|
+
* export const middleware = withBasicAuth
|
|
72
|
+
*
|
|
73
|
+
* export const config = {
|
|
74
|
+
* matcher: ['/dashboard/:path*', '/settings/:path*']
|
|
75
|
+
* }
|
|
76
|
+
*/
|
|
77
|
+
declare function withBasicAuth(request: NextRequest): NextResponse;
|
|
78
|
+
/**
|
|
79
|
+
* Helper to get the return URL from search params
|
|
80
|
+
*/
|
|
81
|
+
declare function getReturnUrl(request: NextRequest, defaultUrl?: string): string;
|
|
7
82
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export { BasicProvider, LoginButton };
|
|
83
|
+
export { BasicMiddlewareConfig, createBasicMiddleware, getAuthFromRequest, getReturnUrl, withBasicAuth };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
export { useBasic, useQuery } from '@basictech/react';
|
|
4
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
1
|
+
export { AuthConfig, AuthError, AuthResult, BasicContextType, BasicDB, BasicProviderProps, BasicStorage, Collection, DBMode, LocalStorageAdapter, RemoteDBConfig } from '@basictech/react';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
5
3
|
|
|
6
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for the Basic auth middleware
|
|
6
|
+
*/
|
|
7
|
+
interface BasicMiddlewareConfig {
|
|
8
|
+
/**
|
|
9
|
+
* Routes that require authentication
|
|
10
|
+
* Supports glob patterns like '/dashboard/*', '/api/protected/*'
|
|
11
|
+
*/
|
|
12
|
+
protectedRoutes?: string[];
|
|
13
|
+
/**
|
|
14
|
+
* Routes that are always public (bypass auth check)
|
|
15
|
+
* Useful for login pages, public APIs, etc.
|
|
16
|
+
*/
|
|
17
|
+
publicRoutes?: string[];
|
|
18
|
+
/**
|
|
19
|
+
* Where to redirect unauthenticated users
|
|
20
|
+
* @default '/login'
|
|
21
|
+
*/
|
|
22
|
+
signInUrl?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Where to redirect after successful sign-in
|
|
25
|
+
* @default '/'
|
|
26
|
+
*/
|
|
27
|
+
afterSignInUrl?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Cookie name for the access token
|
|
30
|
+
* @default 'basic_access_token'
|
|
31
|
+
*/
|
|
32
|
+
tokenCookieName?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Cookie name for the full token object
|
|
35
|
+
* @default 'basic_token'
|
|
36
|
+
*/
|
|
37
|
+
fullTokenCookieName?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Get auth info from cookies
|
|
41
|
+
*/
|
|
42
|
+
declare function getAuthFromRequest(request: NextRequest, config?: Partial<BasicMiddlewareConfig>): {
|
|
43
|
+
isAuthenticated: boolean;
|
|
44
|
+
token: string | null;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Create a Basic auth middleware for NextJS
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* // In middleware.ts at the root of your NextJS app:
|
|
51
|
+
* import { createBasicMiddleware } from '@basictech/nextjs'
|
|
52
|
+
*
|
|
53
|
+
* export const middleware = createBasicMiddleware({
|
|
54
|
+
* protectedRoutes: ['/dashboard/*', '/settings/*'],
|
|
55
|
+
* publicRoutes: ['/login', '/signup', '/'],
|
|
56
|
+
* signInUrl: '/login'
|
|
57
|
+
* })
|
|
58
|
+
*
|
|
59
|
+
* export const config = {
|
|
60
|
+
* matcher: ['/((?!_next/static|_next/image|favicon.ico).*)']
|
|
61
|
+
* }
|
|
62
|
+
*/
|
|
63
|
+
declare function createBasicMiddleware(config?: BasicMiddlewareConfig): (request: NextRequest) => NextResponse;
|
|
64
|
+
/**
|
|
65
|
+
* Simple auth check middleware - redirects unauthenticated users
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // In middleware.ts
|
|
69
|
+
* import { withBasicAuth } from '@basictech/nextjs'
|
|
70
|
+
*
|
|
71
|
+
* export const middleware = withBasicAuth
|
|
72
|
+
*
|
|
73
|
+
* export const config = {
|
|
74
|
+
* matcher: ['/dashboard/:path*', '/settings/:path*']
|
|
75
|
+
* }
|
|
76
|
+
*/
|
|
77
|
+
declare function withBasicAuth(request: NextRequest): NextResponse;
|
|
78
|
+
/**
|
|
79
|
+
* Helper to get the return URL from search params
|
|
80
|
+
*/
|
|
81
|
+
declare function getReturnUrl(request: NextRequest, defaultUrl?: string): string;
|
|
7
82
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export { BasicProvider, LoginButton };
|
|
83
|
+
export { BasicMiddlewareConfig, createBasicMiddleware, getAuthFromRequest, getReturnUrl, withBasicAuth };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
"use client";
|
|
3
|
-
var __create = Object.create;
|
|
4
2
|
var __defProp = Object.defineProperty;
|
|
5
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
6
|
var __export = (target, all) => {
|
|
10
7
|
for (var name in all)
|
|
@@ -18,105 +15,85 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
15
|
}
|
|
19
16
|
return to;
|
|
20
17
|
};
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
-
mod
|
|
28
|
-
));
|
|
29
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
19
|
|
|
31
20
|
// src/index.ts
|
|
32
21
|
var src_exports = {};
|
|
33
22
|
__export(src_exports, {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
23
|
+
createBasicMiddleware: () => createBasicMiddleware,
|
|
24
|
+
getAuthFromRequest: () => getAuthFromRequest,
|
|
25
|
+
getReturnUrl: () => getReturnUrl,
|
|
26
|
+
withBasicAuth: () => withBasicAuth
|
|
38
27
|
});
|
|
39
28
|
module.exports = __toCommonJS(src_exports);
|
|
40
|
-
var import_dynamic = __toESM(require("next/dynamic"));
|
|
41
|
-
var import_react = require("@basictech/react");
|
|
42
29
|
|
|
43
|
-
// src/
|
|
44
|
-
var
|
|
45
|
-
var
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
{
|
|
53
|
-
src: "https://via.placeholder.com/150",
|
|
54
|
-
alt: "User Avatar",
|
|
55
|
-
style: avatarImageStyle
|
|
56
|
-
}
|
|
57
|
-
),
|
|
58
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Avatar.Fallback, { delayMs: 600, style: avatarFallbackStyle, children: "U" })
|
|
59
|
-
] }) }),
|
|
60
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Popover.Portal, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Popover.Content, { style: popoverContentStyle, sideOffset: 10, children: [
|
|
61
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { style: { marginBottom: "10px" }, children: "Hello, User!" }),
|
|
62
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { style: buttonStyle, children: "Logout" }),
|
|
63
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Popover.Arrow, { style: popoverArrowStyle })
|
|
64
|
-
] }) })
|
|
65
|
-
] });
|
|
30
|
+
// src/middleware.ts
|
|
31
|
+
var import_server = require("next/server");
|
|
32
|
+
var DEFAULT_CONFIG = {
|
|
33
|
+
protectedRoutes: [],
|
|
34
|
+
publicRoutes: ["/login", "/signup", "/auth/*"],
|
|
35
|
+
signInUrl: "/login",
|
|
36
|
+
afterSignInUrl: "/",
|
|
37
|
+
tokenCookieName: "basic_access_token",
|
|
38
|
+
fullTokenCookieName: "basic_token"
|
|
66
39
|
};
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
40
|
+
function matchesPattern(path, patterns) {
|
|
41
|
+
return patterns.some((pattern) => {
|
|
42
|
+
const regexPattern = pattern.replace(/\*/g, ".*").replace(/\//g, "\\/");
|
|
43
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
44
|
+
return regex.test(path);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function hasValidToken(request, config) {
|
|
48
|
+
const token = request.cookies.get(config.tokenCookieName)?.value;
|
|
49
|
+
if (!token) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return token.length > 0;
|
|
53
|
+
}
|
|
54
|
+
function getAuthFromRequest(request, config) {
|
|
55
|
+
const mergedConfig = { ...DEFAULT_CONFIG, ...config };
|
|
56
|
+
const token = request.cookies.get(mergedConfig.tokenCookieName)?.value || null;
|
|
57
|
+
return {
|
|
58
|
+
isAuthenticated: !!token && token.length > 0,
|
|
59
|
+
token
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function createBasicMiddleware(config) {
|
|
63
|
+
const mergedConfig = { ...DEFAULT_CONFIG, ...config };
|
|
64
|
+
return function middleware(request) {
|
|
65
|
+
const { pathname } = request.nextUrl;
|
|
66
|
+
if (pathname.startsWith("/_next") || pathname.startsWith("/api/_next") || pathname.includes(".")) {
|
|
67
|
+
return import_server.NextResponse.next();
|
|
68
|
+
}
|
|
69
|
+
if (matchesPattern(pathname, mergedConfig.publicRoutes)) {
|
|
70
|
+
return import_server.NextResponse.next();
|
|
71
|
+
}
|
|
72
|
+
const isProtectedRoute = mergedConfig.protectedRoutes.length === 0 ? true : matchesPattern(pathname, mergedConfig.protectedRoutes);
|
|
73
|
+
if (!isProtectedRoute) {
|
|
74
|
+
return import_server.NextResponse.next();
|
|
75
|
+
}
|
|
76
|
+
const isAuthenticated = hasValidToken(request, mergedConfig);
|
|
77
|
+
if (!isAuthenticated) {
|
|
78
|
+
const signInUrl = new URL(mergedConfig.signInUrl, request.url);
|
|
79
|
+
signInUrl.searchParams.set("returnUrl", pathname);
|
|
80
|
+
return import_server.NextResponse.redirect(signInUrl);
|
|
81
|
+
}
|
|
82
|
+
return import_server.NextResponse.next();
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function withBasicAuth(request) {
|
|
86
|
+
return createBasicMiddleware()(request);
|
|
87
|
+
}
|
|
88
|
+
function getReturnUrl(request, defaultUrl = "/") {
|
|
89
|
+
const returnUrl = request.nextUrl.searchParams.get("returnUrl");
|
|
90
|
+
return returnUrl || defaultUrl;
|
|
91
|
+
}
|
|
115
92
|
// Annotate the CommonJS export names for ESM import in node:
|
|
116
93
|
0 && (module.exports = {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
94
|
+
createBasicMiddleware,
|
|
95
|
+
getAuthFromRequest,
|
|
96
|
+
getReturnUrl,
|
|
97
|
+
withBasicAuth
|
|
121
98
|
});
|
|
122
99
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/middleware.ts"],"sourcesContent":["// @basictech/nextjs - Next.js integration for Basic SDK\n//\n// Usage:\n// - Server components/middleware: import from \"@basictech/nextjs\"\n// - Client components: import from \"@basictech/nextjs/client\"\n//\n// Example:\n// // In a client component (providers.tsx)\n// 'use client'\n// import { BasicProvider, useBasic } from \"@basictech/nextjs/client\"\n//\n// // In middleware.ts\n// import { createBasicMiddleware } from \"@basictech/nextjs\"\n\n// Re-export types from react package (types are safe for SSR)\nexport type {\n AuthConfig,\n BasicStorage,\n LocalStorageAdapter,\n BasicProviderProps,\n BasicContextType,\n AuthResult,\n DBMode,\n BasicDB,\n Collection,\n RemoteDBConfig,\n AuthError\n} from \"@basictech/react\"\n\n// Middleware exports (server-side safe - no dexie dependency)\nexport { \n createBasicMiddleware, \n withBasicAuth,\n getAuthFromRequest,\n getReturnUrl\n} from \"./middleware\"\n\nexport type { BasicMiddlewareConfig } from \"./middleware\"\n","import { NextRequest, NextResponse } from 'next/server'\n\n/**\n * Configuration options for the Basic auth middleware\n */\nexport interface BasicMiddlewareConfig {\n /**\n * Routes that require authentication\n * Supports glob patterns like '/dashboard/*', '/api/protected/*'\n */\n protectedRoutes?: string[]\n \n /**\n * Routes that are always public (bypass auth check)\n * Useful for login pages, public APIs, etc.\n */\n publicRoutes?: string[]\n \n /**\n * Where to redirect unauthenticated users\n * @default '/login'\n */\n signInUrl?: string\n \n /**\n * Where to redirect after successful sign-in\n * @default '/'\n */\n afterSignInUrl?: string\n \n /**\n * Cookie name for the access token\n * @default 'basic_access_token'\n */\n tokenCookieName?: string\n \n /**\n * Cookie name for the full token object\n * @default 'basic_token'\n */\n fullTokenCookieName?: string\n}\n\nconst DEFAULT_CONFIG: Required<BasicMiddlewareConfig> = {\n protectedRoutes: [],\n publicRoutes: ['/login', '/signup', '/auth/*'],\n signInUrl: '/login',\n afterSignInUrl: '/',\n tokenCookieName: 'basic_access_token',\n fullTokenCookieName: 'basic_token'\n}\n\n/**\n * Check if a path matches any of the patterns\n * Supports simple glob patterns with * wildcard\n */\nfunction matchesPattern(path: string, patterns: string[]): boolean {\n return patterns.some(pattern => {\n // Convert glob pattern to regex\n const regexPattern = pattern\n .replace(/\\*/g, '.*') // Replace * with .*\n .replace(/\\//g, '\\\\/') // Escape slashes\n const regex = new RegExp(`^${regexPattern}$`)\n return regex.test(path)\n })\n}\n\n/**\n * Check if the user has a valid token\n */\nfunction hasValidToken(request: NextRequest, config: Required<BasicMiddlewareConfig>): boolean {\n const token = request.cookies.get(config.tokenCookieName)?.value\n \n if (!token) {\n return false\n }\n \n // Basic validation - token exists and is not empty\n // For more thorough validation, you'd decode the JWT and check expiry\n // But that adds latency to every request\n return token.length > 0\n}\n\n/**\n * Get auth info from cookies\n */\nexport function getAuthFromRequest(request: NextRequest, config?: Partial<BasicMiddlewareConfig>): {\n isAuthenticated: boolean\n token: string | null\n} {\n const mergedConfig = { ...DEFAULT_CONFIG, ...config }\n const token = request.cookies.get(mergedConfig.tokenCookieName)?.value || null\n \n return {\n isAuthenticated: !!token && token.length > 0,\n token\n }\n}\n\n/**\n * Create a Basic auth middleware for NextJS\n * \n * @example\n * // In middleware.ts at the root of your NextJS app:\n * import { createBasicMiddleware } from '@basictech/nextjs'\n * \n * export const middleware = createBasicMiddleware({\n * protectedRoutes: ['/dashboard/*', '/settings/*'],\n * publicRoutes: ['/login', '/signup', '/'],\n * signInUrl: '/login'\n * })\n * \n * export const config = {\n * matcher: ['/((?!_next/static|_next/image|favicon.ico).*)']\n * }\n */\nexport function createBasicMiddleware(config?: BasicMiddlewareConfig) {\n const mergedConfig = { ...DEFAULT_CONFIG, ...config }\n \n return function middleware(request: NextRequest): NextResponse {\n const { pathname } = request.nextUrl\n \n // Skip middleware for static files and Next.js internals\n if (\n pathname.startsWith('/_next') ||\n pathname.startsWith('/api/_next') ||\n pathname.includes('.') // Static files like .css, .js, .ico\n ) {\n return NextResponse.next()\n }\n \n // Check if route is explicitly public\n if (matchesPattern(pathname, mergedConfig.publicRoutes)) {\n return NextResponse.next()\n }\n \n // Check if route is protected\n const isProtectedRoute = mergedConfig.protectedRoutes.length === 0 \n ? true // If no protected routes specified, protect everything except public\n : matchesPattern(pathname, mergedConfig.protectedRoutes)\n \n if (!isProtectedRoute) {\n return NextResponse.next()\n }\n \n // Check authentication\n const isAuthenticated = hasValidToken(request, mergedConfig)\n \n if (!isAuthenticated) {\n // Redirect to sign-in page with return URL\n const signInUrl = new URL(mergedConfig.signInUrl, request.url)\n signInUrl.searchParams.set('returnUrl', pathname)\n \n return NextResponse.redirect(signInUrl)\n }\n \n // User is authenticated, allow the request\n return NextResponse.next()\n }\n}\n\n/**\n * Simple auth check middleware - redirects unauthenticated users\n * \n * @example\n * // In middleware.ts\n * import { withBasicAuth } from '@basictech/nextjs'\n * \n * export const middleware = withBasicAuth\n * \n * export const config = {\n * matcher: ['/dashboard/:path*', '/settings/:path*']\n * }\n */\nexport function withBasicAuth(request: NextRequest): NextResponse {\n return createBasicMiddleware()(request)\n}\n\n/**\n * Helper to get the return URL from search params\n */\nexport function getReturnUrl(request: NextRequest, defaultUrl: string = '/'): string {\n const returnUrl = request.nextUrl.searchParams.get('returnUrl')\n return returnUrl || defaultUrl\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAA0C;AA2C1C,IAAM,iBAAkD;AAAA,EACtD,iBAAiB,CAAC;AAAA,EAClB,cAAc,CAAC,UAAU,WAAW,SAAS;AAAA,EAC7C,WAAW;AAAA,EACX,gBAAgB;AAAA,EAChB,iBAAiB;AAAA,EACjB,qBAAqB;AACvB;AAMA,SAAS,eAAe,MAAc,UAA6B;AACjE,SAAO,SAAS,KAAK,aAAW;AAE9B,UAAM,eAAe,QAClB,QAAQ,OAAO,IAAI,EACnB,QAAQ,OAAO,KAAK;AACvB,UAAM,QAAQ,IAAI,OAAO,IAAI,YAAY,GAAG;AAC5C,WAAO,MAAM,KAAK,IAAI;AAAA,EACxB,CAAC;AACH;AAKA,SAAS,cAAc,SAAsB,QAAkD;AAC7F,QAAM,QAAQ,QAAQ,QAAQ,IAAI,OAAO,eAAe,GAAG;AAE3D,MAAI,CAAC,OAAO;AACV,WAAO;AAAA,EACT;AAKA,SAAO,MAAM,SAAS;AACxB;AAKO,SAAS,mBAAmB,SAAsB,QAGvD;AACA,QAAM,eAAe,EAAE,GAAG,gBAAgB,GAAG,OAAO;AACpD,QAAM,QAAQ,QAAQ,QAAQ,IAAI,aAAa,eAAe,GAAG,SAAS;AAE1E,SAAO;AAAA,IACL,iBAAiB,CAAC,CAAC,SAAS,MAAM,SAAS;AAAA,IAC3C;AAAA,EACF;AACF;AAmBO,SAAS,sBAAsB,QAAgC;AACpE,QAAM,eAAe,EAAE,GAAG,gBAAgB,GAAG,OAAO;AAEpD,SAAO,SAAS,WAAW,SAAoC;AAC7D,UAAM,EAAE,SAAS,IAAI,QAAQ;AAG7B,QACE,SAAS,WAAW,QAAQ,KAC5B,SAAS,WAAW,YAAY,KAChC,SAAS,SAAS,GAAG,GACrB;AACA,aAAO,2BAAa,KAAK;AAAA,IAC3B;AAGA,QAAI,eAAe,UAAU,aAAa,YAAY,GAAG;AACvD,aAAO,2BAAa,KAAK;AAAA,IAC3B;AAGA,UAAM,mBAAmB,aAAa,gBAAgB,WAAW,IAC7D,OACA,eAAe,UAAU,aAAa,eAAe;AAEzD,QAAI,CAAC,kBAAkB;AACrB,aAAO,2BAAa,KAAK;AAAA,IAC3B;AAGA,UAAM,kBAAkB,cAAc,SAAS,YAAY;AAE3D,QAAI,CAAC,iBAAiB;AAEpB,YAAM,YAAY,IAAI,IAAI,aAAa,WAAW,QAAQ,GAAG;AAC7D,gBAAU,aAAa,IAAI,aAAa,QAAQ;AAEhD,aAAO,2BAAa,SAAS,SAAS;AAAA,IACxC;AAGA,WAAO,2BAAa,KAAK;AAAA,EAC3B;AACF;AAeO,SAAS,cAAc,SAAoC;AAChE,SAAO,sBAAsB,EAAE,OAAO;AACxC;AAKO,SAAS,aAAa,SAAsB,aAAqB,KAAa;AACnF,QAAM,YAAY,QAAQ,QAAQ,aAAa,IAAI,WAAW;AAC9D,SAAO,aAAa;AACtB;","names":[]}
|