@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.
@@ -1,9 +1,9 @@
1
1
 
2
2
  
3
- > @basictech/nextjs@0.6.0-beta.3 build
3
+ > @basictech/nextjs@0.6.0-beta.6 build
4
4
  > tsup
5
5
 
6
- CLI Building entry: src/index.ts
6
+ CLI Building entry: src/index.ts, src/client.ts
7
7
  CLI Using tsconfig: tsconfig.json
8
8
  CLI tsup v7.2.0
9
9
  CLI Using tsup config: /Users/raz/codebook/basic/libs/client-ts/packages/nextjs/tsup.config.ts
@@ -11,13 +11,19 @@
11
11
  CLI Cleaning output folder
12
12
  CJS Build start
13
13
  ESM Build start
14
- ESM dist/index.mjs 2.39 KB
15
- ESM dist/index.mjs.map 3.73 KB
16
- ESM ⚡️ Build success in 12ms
17
- CJS dist/index.js 4.34 KB
18
- CJS dist/index.js.map 3.79 KB
19
- CJS ⚡️ Build success in 12ms
14
+ ESM dist/index.mjs 2.22 KB
15
+ ESM dist/client.mjs 375.00 B
16
+ ESM dist/index.mjs.map 6.68 KB
17
+ ESM dist/client.mjs.map 933.00 B
18
+ ESM ⚡️ Build success in 19ms
19
+ CJS dist/client.js 1.60 KB
20
+ CJS dist/index.js 3.43 KB
21
+ CJS dist/client.js.map 927.00 B
22
+ CJS dist/index.js.map 7.77 KB
23
+ CJS ⚡️ Build success in 20ms
20
24
  DTS Build start
21
- DTS ⚡️ Build success in 1168ms
22
- DTS dist/index.d.ts 390.00 B
23
- DTS dist/index.d.mts 390.00 B
25
+ DTS ⚡️ Build success in 1866ms
26
+ DTS dist/client.d.ts 303.00 B
27
+ DTS dist/index.d.ts 2.47 KB
28
+ DTS dist/client.d.mts 303.00 B
29
+ DTS dist/index.d.mts 2.47 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @basictech/nextjs
2
2
 
3
+ ## 0.6.0-beta.7
4
+
5
+ ### Minor Changes
6
+
7
+ - api improvements, react refactor, nextjs update
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @basictech/react@0.7.0-beta.7
13
+
3
14
  ## 0.6.0-beta.6
4
15
 
5
16
  ### Patch Changes
@@ -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';
@@ -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":[]}
@@ -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
- import * as react from 'react';
2
- import * as _basictech_react from '@basictech/react';
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
- declare const LoginButton: () => react_jsx_runtime.JSX.Element;
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
- declare const BasicProvider: react.ComponentType<_basictech_react.BasicProviderProps>;
9
-
10
- export { BasicProvider, LoginButton };
83
+ export { BasicMiddlewareConfig, createBasicMiddleware, getAuthFromRequest, getReturnUrl, withBasicAuth };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,83 @@
1
- import * as react from 'react';
2
- import * as _basictech_react from '@basictech/react';
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
- declare const LoginButton: () => react_jsx_runtime.JSX.Element;
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
- declare const BasicProvider: react.ComponentType<_basictech_react.BasicProviderProps>;
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
- BasicProvider: () => BasicProvider,
35
- LoginButton: () => componets_default,
36
- useBasic: () => import_react.useBasic,
37
- useQuery: () => import_react.useQuery
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/componets.tsx
44
- var Avatar = __toESM(require("@radix-ui/react-avatar"));
45
- var Popover = __toESM(require("@radix-ui/react-popover"));
46
- var import_jsx_runtime = require("react/jsx-runtime");
47
- var LoginButton = () => {
48
- return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Popover.Root, { children: [
49
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Popover.Trigger, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Avatar.Root, { style: avatarContainerStyle, children: [
50
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
51
- Avatar.Image,
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
- var avatarContainerStyle = {
68
- display: "inline-flex",
69
- alignItems: "center",
70
- justifyContent: "center",
71
- width: "40px",
72
- height: "40px",
73
- borderRadius: "100%",
74
- backgroundColor: "#ccc",
75
- cursor: "pointer"
76
- };
77
- var avatarImageStyle = {
78
- width: "100%",
79
- height: "100%",
80
- borderRadius: "100%"
81
- // objectFit: 'cover',
82
- };
83
- var avatarFallbackStyle = {
84
- width: "100%",
85
- height: "100%",
86
- borderRadius: "100%",
87
- backgroundColor: "#007bff",
88
- color: "white",
89
- display: "flex",
90
- alignItems: "center",
91
- justifyContent: "center",
92
- fontSize: "20px"
93
- };
94
- var popoverContentStyle = {
95
- padding: "20px",
96
- borderRadius: "8px",
97
- // backgroundColor: 'white',
98
- boxShadow: "0 2px 10px rgba(0, 0, 0, 0.2)"
99
- };
100
- var buttonStyle = {
101
- padding: "8px 16px",
102
- backgroundColor: "#007bff",
103
- color: "#fff",
104
- border: "none",
105
- borderRadius: "4px",
106
- cursor: "pointer"
107
- };
108
- var popoverArrowStyle = {
109
- fill: "white"
110
- };
111
- var componets_default = LoginButton;
112
-
113
- // src/index.ts
114
- var BasicProvider = (0, import_dynamic.default)(() => import("@basictech/react").then((mod) => mod.BasicProvider), { ssr: false });
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
- BasicProvider,
118
- LoginButton,
119
- useBasic,
120
- useQuery
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/componets.tsx"],"sourcesContent":["'use client'\n\nimport dynamic from 'next/dynamic'\n\nimport { useBasic, useQuery } from \"@basictech/react\"\nimport LoginButton from \"./componets\";\n\n\nconst BasicProvider = dynamic(() => import('@basictech/react').then(mod => mod.BasicProvider), { ssr: false });\n\n\nexport {\n useBasic, BasicProvider, useQuery, LoginButton\n}","import React from 'react';\nimport * as Avatar from '@radix-ui/react-avatar';\nimport * as Popover from '@radix-ui/react-popover';\n\nconst LoginButton = () => {\n return (\n <Popover.Root>\n {/* Trigger: Avatar that will be clicked */}\n <Popover.Trigger asChild>\n <Avatar.Root style={avatarContainerStyle}>\n <Avatar.Image\n src=\"https://via.placeholder.com/150\"\n alt=\"User Avatar\"\n style={avatarImageStyle}\n />\n <Avatar.Fallback delayMs={600} style={avatarFallbackStyle}>\n U\n </Avatar.Fallback>\n </Avatar.Root>\n </Popover.Trigger>\n\n {/* Popover content */}\n <Popover.Portal>\n <Popover.Content style={popoverContentStyle} sideOffset={10}>\n <p style={{ marginBottom: '10px' }}>Hello, User!</p>\n <button style={buttonStyle}>Logout</button>\n <Popover.Arrow style={popoverArrowStyle} />\n </Popover.Content>\n </Popover.Portal>\n </Popover.Root>\n );\n};\n\n// Basic styles\nconst avatarContainerStyle = {\n display: 'inline-flex',\n alignItems: 'center',\n justifyContent: 'center',\n width: '40px',\n height: '40px',\n borderRadius: '100%',\n backgroundColor: '#ccc',\n cursor: 'pointer',\n};\n\nconst avatarImageStyle = {\n width: '100%',\n height: '100%',\n borderRadius: '100%',\n// objectFit: 'cover',\n};\n\nconst avatarFallbackStyle = {\n width: '100%',\n height: '100%',\n borderRadius: '100%',\n backgroundColor: '#007bff',\n color: 'white',\n display: 'flex',\n alignItems: 'center',\n justifyContent: 'center',\n fontSize: '20px',\n};\n\nconst popoverContentStyle = {\n padding: '20px',\n borderRadius: '8px',\n// backgroundColor: 'white',\n boxShadow: '0 2px 10px rgba(0, 0, 0, 0.2)',\n};\n\nconst buttonStyle = {\n padding: '8px 16px',\n backgroundColor: '#007bff',\n color: '#fff',\n border: 'none',\n borderRadius: '4px',\n cursor: 'pointer',\n};\n\nconst popoverArrowStyle = {\n fill: 'white',\n};\n\nexport default LoginButton;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,qBAAoB;AAEpB,mBAAmC;;;ACHnC,aAAwB;AACxB,cAAyB;AAOjB;AALR,IAAM,cAAc,MAAM;AACxB,SACE,6CAAS,cAAR,EAEC;AAAA,gDAAS,iBAAR,EAAgB,SAAO,MACtB,uDAAQ,aAAP,EAAY,OAAO,sBAClB;AAAA;AAAA,QAAQ;AAAA,QAAP;AAAA,UACC,KAAI;AAAA,UACJ,KAAI;AAAA,UACJ,OAAO;AAAA;AAAA,MACT;AAAA,MACA,4CAAQ,iBAAP,EAAgB,SAAS,KAAK,OAAO,qBAAqB,eAE3D;AAAA,OACF,GACF;AAAA,IAGA,4CAAS,gBAAR,EACC,uDAAS,iBAAR,EAAgB,OAAO,qBAAqB,YAAY,IACvD;AAAA,kDAAC,OAAE,OAAO,EAAE,cAAc,OAAO,GAAG,0BAAY;AAAA,MAChD,4CAAC,YAAO,OAAO,aAAa,oBAAM;AAAA,MAClC,4CAAS,eAAR,EAAc,OAAO,mBAAmB;AAAA,OAC3C,GACF;AAAA,KACF;AAEJ;AAGA,IAAM,uBAAuB;AAAA,EAC3B,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,gBAAgB;AAAA,EAChB,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,cAAc;AAAA,EACd,iBAAiB;AAAA,EACjB,QAAQ;AACV;AAEA,IAAM,mBAAmB;AAAA,EACvB,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,cAAc;AAAA;AAEhB;AAEA,IAAM,sBAAsB;AAAA,EAC1B,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,cAAc;AAAA,EACd,iBAAiB;AAAA,EACjB,OAAO;AAAA,EACP,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,gBAAgB;AAAA,EAChB,UAAU;AACZ;AAEA,IAAM,sBAAsB;AAAA,EAC1B,SAAS;AAAA,EACT,cAAc;AAAA;AAAA,EAEd,WAAW;AACb;AAEA,IAAM,cAAc;AAAA,EAClB,SAAS;AAAA,EACT,iBAAiB;AAAA,EACjB,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,cAAc;AAAA,EACd,QAAQ;AACV;AAEA,IAAM,oBAAoB;AAAA,EACxB,MAAM;AACR;AAEA,IAAO,oBAAQ;;;AD5Ef,IAAM,oBAAgB,eAAAA,SAAQ,MAAM,OAAO,kBAAkB,EAAE,KAAK,SAAO,IAAI,aAAa,GAAG,EAAE,KAAK,MAAM,CAAC;","names":["dynamic"]}
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":[]}