@asgardeo/nextjs 0.1.8 → 0.1.10

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/README.md CHANGED
@@ -28,7 +28,7 @@ yarn add @asgardeo/nextjs
28
28
  1. Create a `.env.local` file with your Asgardeo configuration:
29
29
 
30
30
  ```bash
31
- NEXT_PUBLIC_ASGARDEO_BASE_URL=https://api.asgardeo.io/t/<your-org-name>
31
+ NEXT_PUBLIC_ASGARDEO_BASE_URL=https://api.asgardeo.io/t/<your-organization-name>
32
32
  NEXT_PUBLIC_ASGARDEO_CLIENT_ID=<your-client-id>
33
33
  NEXT_PUBLIC_ASGARDEO_CLIENT_SECRET=<your-client-secret>
34
34
  ```
package/dist/cjs/index.js CHANGED
@@ -50,6 +50,7 @@ __export(index_exports, {
50
50
  UserDropdown: () => import_UserDropdown.default,
51
51
  UserProfile: () => import_UserProfile.default,
52
52
  asgardeoMiddleware: () => import_asgardeoMiddleware.default,
53
+ createRouteMatcher: () => import_createRouteMatcher.default,
53
54
  handleOAuthCallback: () => import_handleOAuthCallbackAction.default,
54
55
  isSignedIn: () => import_isSignedIn.default,
55
56
  useAsgardeo: () => import_useAsgardeo.default
@@ -81,6 +82,9 @@ var import_UserDropdown = __toESM(require("./client/components/presentation/User
81
82
  var import_UserProfile = __toESM(require("./client/components/presentation/UserProfile/UserProfile"), 1);
82
83
  var import_AsgardeoNextClient = __toESM(require("./AsgardeoNextClient"), 1);
83
84
  var import_asgardeoMiddleware = __toESM(require("./middleware/asgardeoMiddleware"), 1);
85
+ __reExport(index_exports, require("./middleware/asgardeoMiddleware"), module.exports);
86
+ var import_createRouteMatcher = __toESM(require("./middleware/createRouteMatcher"), 1);
87
+ __reExport(index_exports, require("./middleware/createRouteMatcher"), module.exports);
84
88
  // Annotate the CommonJS export names for ESM import in node:
85
89
  0 && (module.exports = {
86
90
  AsgardeoNext,
@@ -104,10 +108,13 @@ var import_asgardeoMiddleware = __toESM(require("./middleware/asgardeoMiddleware
104
108
  UserDropdown,
105
109
  UserProfile,
106
110
  asgardeoMiddleware,
111
+ createRouteMatcher,
107
112
  handleOAuthCallback,
108
113
  isSignedIn,
109
114
  useAsgardeo,
110
115
  ...require("./server/AsgardeoProvider"),
111
- ...require("./client/contexts/Asgardeo/useAsgardeo")
116
+ ...require("./client/contexts/Asgardeo/useAsgardeo"),
117
+ ...require("./middleware/asgardeoMiddleware"),
118
+ ...require("./middleware/createRouteMatcher")
112
119
  });
113
120
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
- "sourcesContent": ["/**\n * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).\n *\n * WSO2 LLC. licenses this file to you under the Apache License,\n * Version 2.0 (the \"License\"); you may not use this file except\n * in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing,\n * software distributed under the License is distributed on an\n * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n * KIND, either express or implied. See the License for the\n * specific language governing permissions and limitations\n * under the License.\n */\n\nexport {default as AsgardeoProvider} from './server/AsgardeoProvider';\nexport * from './server/AsgardeoProvider';\n\nexport {default as useAsgardeo} from './client/contexts/Asgardeo/useAsgardeo';\nexport * from './client/contexts/Asgardeo/useAsgardeo';\n\nexport {default as isSignedIn} from './server/actions/isSignedIn';\n\nexport {default as handleOAuthCallback} from './server/actions/handleOAuthCallbackAction';\n\nexport {default as CreateOrganization} from './client/components/presentation/CreateOrganization/CreateOrganization';\nexport {CreateOrganizationProps} from './client/components/presentation/CreateOrganization/CreateOrganization';\n\nexport {default as OrganizationProfile} from './client/components/presentation/OrganizationProfile/OrganizationProfile';\nexport {OrganizationProfileProps} from './client/components/presentation/OrganizationProfile/OrganizationProfile';\n\nexport {default as OrganizationSwitcher} from './client/components/presentation/OrganizationSwitcher/OrganizationSwitcher';\nexport {OrganizationSwitcherProps} from './client/components/presentation/OrganizationSwitcher/OrganizationSwitcher';\n\nexport {default as SignedIn} from './client/components/control/SignedIn/SignedIn';\nexport {SignedInProps} from './client/components/control/SignedIn/SignedIn';\n\nexport {default as SignedOut} from './client/components/control/SignedOut/SignedOut';\nexport {SignedOutProps} from './client/components/control/SignedOut/SignedOut';\n\nexport {default as SignInButton} from './client/components/actions/SignInButton/SignInButton';\nexport type {SignInButtonProps} from './client/components/actions/SignInButton/SignInButton';\n\nexport {default as SignUpButton} from './client/components/actions/SignUpButton/SignUpButton';\nexport type {SignUpButtonProps} from './client/components/actions/SignUpButton/SignUpButton';\n\nexport {default as SignIn} from './client/components/presentation/SignIn/SignIn';\nexport type {SignInProps} from './client/components/presentation/SignIn/SignIn';\n\nexport {default as SignOutButton} from './client/components/actions/SignOutButton/SignOutButton';\nexport type {SignOutButtonProps} from './client/components/actions/SignOutButton/SignOutButton';\n\nexport {default as User} from './client/components/presentation/User/User';\nexport type {UserProps} from './client/components/presentation/User/User';\n\nexport {default as SignUp} from './client/components/presentation/SignUp/SignUp';\nexport type {SignUpProps} from './client/components/presentation/SignUp/SignUp';\n\nexport {default as UserDropdown} from './client/components/presentation/UserDropdown/UserDropdown';\nexport type {UserDropdownProps} from './client/components/presentation/UserDropdown/UserDropdown';\n\nexport {default as UserProfile} from './client/components/presentation/UserProfile/UserProfile';\nexport type {UserProfileProps} from './client/components/presentation/UserProfile/UserProfile';\n\nexport {default as AsgardeoNext} from './AsgardeoNextClient';\n\nexport {default as asgardeoMiddleware} from './middleware/asgardeoMiddleware';\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,8BAA0C;AAC1C,0BAAc,sCAnBd;AAqBA,yBAAqC;AACrC,0BAAc,mDAtBd;AAwBA,wBAAoC;AAEpC,uCAA6C;AAE7C,gCAA4C;AAC5C,IAAAA,6BAAsC;AAEtC,iCAA6C;AAC7C,IAAAC,8BAAuC;AAEvC,kCAA8C;AAC9C,IAAAC,+BAAwC;AAExC,sBAAkC;AAClC,IAAAC,mBAA4B;AAE5B,uBAAmC;AACnC,IAAAC,oBAA6B;AAE7B,0BAAsC;AAGtC,0BAAsC;AAGtC,oBAAgC;AAGhC,2BAAuC;AAGvC,kBAA8B;AAG9B,oBAAgC;AAGhC,0BAAsC;AAGtC,yBAAqC;AAGrC,gCAAsC;AAEtC,gCAA4C;",
4
+ "sourcesContent": ["/**\n * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).\n *\n * WSO2 LLC. licenses this file to you under the Apache License,\n * Version 2.0 (the \"License\"); you may not use this file except\n * in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing,\n * software distributed under the License is distributed on an\n * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n * KIND, either express or implied. See the License for the\n * specific language governing permissions and limitations\n * under the License.\n */\n\nexport {default as AsgardeoProvider} from './server/AsgardeoProvider';\nexport * from './server/AsgardeoProvider';\n\nexport {default as useAsgardeo} from './client/contexts/Asgardeo/useAsgardeo';\nexport * from './client/contexts/Asgardeo/useAsgardeo';\n\nexport {default as isSignedIn} from './server/actions/isSignedIn';\n\nexport {default as handleOAuthCallback} from './server/actions/handleOAuthCallbackAction';\n\nexport {default as CreateOrganization} from './client/components/presentation/CreateOrganization/CreateOrganization';\nexport {CreateOrganizationProps} from './client/components/presentation/CreateOrganization/CreateOrganization';\n\nexport {default as OrganizationProfile} from './client/components/presentation/OrganizationProfile/OrganizationProfile';\nexport {OrganizationProfileProps} from './client/components/presentation/OrganizationProfile/OrganizationProfile';\n\nexport {default as OrganizationSwitcher} from './client/components/presentation/OrganizationSwitcher/OrganizationSwitcher';\nexport {OrganizationSwitcherProps} from './client/components/presentation/OrganizationSwitcher/OrganizationSwitcher';\n\nexport {default as SignedIn} from './client/components/control/SignedIn/SignedIn';\nexport {SignedInProps} from './client/components/control/SignedIn/SignedIn';\n\nexport {default as SignedOut} from './client/components/control/SignedOut/SignedOut';\nexport {SignedOutProps} from './client/components/control/SignedOut/SignedOut';\n\nexport {default as SignInButton} from './client/components/actions/SignInButton/SignInButton';\nexport type {SignInButtonProps} from './client/components/actions/SignInButton/SignInButton';\n\nexport {default as SignUpButton} from './client/components/actions/SignUpButton/SignUpButton';\nexport type {SignUpButtonProps} from './client/components/actions/SignUpButton/SignUpButton';\n\nexport {default as SignIn} from './client/components/presentation/SignIn/SignIn';\nexport type {SignInProps} from './client/components/presentation/SignIn/SignIn';\n\nexport {default as SignOutButton} from './client/components/actions/SignOutButton/SignOutButton';\nexport type {SignOutButtonProps} from './client/components/actions/SignOutButton/SignOutButton';\n\nexport {default as User} from './client/components/presentation/User/User';\nexport type {UserProps} from './client/components/presentation/User/User';\n\nexport {default as SignUp} from './client/components/presentation/SignUp/SignUp';\nexport type {SignUpProps} from './client/components/presentation/SignUp/SignUp';\n\nexport {default as UserDropdown} from './client/components/presentation/UserDropdown/UserDropdown';\nexport type {UserDropdownProps} from './client/components/presentation/UserDropdown/UserDropdown';\n\nexport {default as UserProfile} from './client/components/presentation/UserProfile/UserProfile';\nexport type {UserProfileProps} from './client/components/presentation/UserProfile/UserProfile';\n\nexport {default as AsgardeoNext} from './AsgardeoNextClient';\n\nexport {default as asgardeoMiddleware} from './middleware/asgardeoMiddleware';\nexport * from './middleware/asgardeoMiddleware';\n\nexport {default as createRouteMatcher} from './middleware/createRouteMatcher';\nexport * from './middleware/createRouteMatcher';\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,8BAA0C;AAC1C,0BAAc,sCAnBd;AAqBA,yBAAqC;AACrC,0BAAc,mDAtBd;AAwBA,wBAAoC;AAEpC,uCAA6C;AAE7C,gCAA4C;AAC5C,IAAAA,6BAAsC;AAEtC,iCAA6C;AAC7C,IAAAC,8BAAuC;AAEvC,kCAA8C;AAC9C,IAAAC,+BAAwC;AAExC,sBAAkC;AAClC,IAAAC,mBAA4B;AAE5B,uBAAmC;AACnC,IAAAC,oBAA6B;AAE7B,0BAAsC;AAGtC,0BAAsC;AAGtC,oBAAgC;AAGhC,2BAAuC;AAGvC,kBAA8B;AAG9B,oBAAgC;AAGhC,0BAAsC;AAGtC,yBAAqC;AAGrC,gCAAsC;AAEtC,gCAA4C;AAC5C,0BAAc,4CAtEd;AAwEA,gCAA4C;AAC5C,0BAAc,4CAzEd;",
6
6
  "names": ["import_CreateOrganization", "import_OrganizationProfile", "import_OrganizationSwitcher", "import_SignedIn", "import_SignedOut"]
7
7
  }
package/dist/index.d.ts CHANGED
@@ -49,3 +49,6 @@ export { default as UserProfile } from './client/components/presentation/UserPro
49
49
  export type { UserProfileProps } from './client/components/presentation/UserProfile/UserProfile';
50
50
  export { default as AsgardeoNext } from './AsgardeoNextClient';
51
51
  export { default as asgardeoMiddleware } from './middleware/asgardeoMiddleware';
52
+ export * from './middleware/asgardeoMiddleware';
53
+ export { default as createRouteMatcher } from './middleware/createRouteMatcher';
54
+ export * from './middleware/createRouteMatcher';
package/dist/index.js CHANGED
@@ -36,4 +36,7 @@ export { default as UserDropdown } from './client/components/presentation/UserDr
36
36
  export { default as UserProfile } from './client/components/presentation/UserProfile/UserProfile';
37
37
  export { default as AsgardeoNext } from './AsgardeoNextClient';
38
38
  export { default as asgardeoMiddleware } from './middleware/asgardeoMiddleware';
39
+ export * from './middleware/asgardeoMiddleware';
40
+ export { default as createRouteMatcher } from './middleware/createRouteMatcher';
41
+ export * from './middleware/createRouteMatcher';
39
42
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAC,OAAO,IAAI,gBAAgB,EAAC,MAAM,2BAA2B,CAAC;AACtE,cAAc,2BAA2B,CAAC;AAE1C,OAAO,EAAC,OAAO,IAAI,WAAW,EAAC,MAAM,wCAAwC,CAAC;AAC9E,cAAc,wCAAwC,CAAC;AAEvD,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,MAAM,6BAA6B,CAAC;AAElE,OAAO,EAAC,OAAO,IAAI,mBAAmB,EAAC,MAAM,4CAA4C,CAAC;AAE1F,OAAO,EAAC,OAAO,IAAI,kBAAkB,EAAC,MAAM,wEAAwE,CAAC;AAGrH,OAAO,EAAC,OAAO,IAAI,mBAAmB,EAAC,MAAM,0EAA0E,CAAC;AAGxH,OAAO,EAAC,OAAO,IAAI,oBAAoB,EAAC,MAAM,4EAA4E,CAAC;AAG3H,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MAAM,+CAA+C,CAAC;AAGlF,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,MAAM,iDAAiD,CAAC;AAGrF,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,uDAAuD,CAAC;AAG9F,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,uDAAuD,CAAC;AAG9F,OAAO,EAAC,OAAO,IAAI,MAAM,EAAC,MAAM,gDAAgD,CAAC;AAGjF,OAAO,EAAC,OAAO,IAAI,aAAa,EAAC,MAAM,yDAAyD,CAAC;AAGjG,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,4CAA4C,CAAC;AAG3E,OAAO,EAAC,OAAO,IAAI,MAAM,EAAC,MAA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AAa,EAAC,MAAM,yDAAyD,CAAC;AAGjG,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,4CAA4C,CAAC;AAG3E,OAAO,EAAC,OAAO,IAAI,MAAM,EAAC,MAAM,gDAAgD,CAAC;AAGjF,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,4DAA4D,CAAC;AAGnG,OAAO,EAAC,OAAO,IAAI,WAAW,EAAC,MAAM,0DAA0D,CAAC;AAGhG,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,MAAM,sBAAsB,CAAC;AAE7D,OAAO,EAAC,OAAO,IAAI,kBAAkB,EAAC,MAAM,iCAAiC,CAAC;AAC9E,cAAc,iCAAiC,CAAC;AAEhD,OAAO,EAAC,OAAO,IAAI,kBAAkB,EAAC,MAAM,iCAAiC,CAAC;AAC9E,cAAc,iCAAiC,CAAC"}
@@ -17,18 +17,26 @@
17
17
  */
18
18
  import { NextRequest, NextResponse } from 'next/server';
19
19
  import { AsgardeoNextConfig } from '../models/config';
20
- export interface AsgardeoMiddlewareOptions extends Partial<AsgardeoNextConfig> {
21
- debug?: boolean;
22
- }
23
- type AsgardeoAuth = {
24
- protect: (options?: {
20
+ export type AsgardeoMiddlewareOptions = Partial<AsgardeoNextConfig>;
21
+ export type AsgardeoMiddlewareContext = {
22
+ /**
23
+ * Protect a route by redirecting unauthenticated users.
24
+ * Redirect URL fallback order:
25
+ * 1. options.redirect
26
+ * 2. resolvedOptions.signInUrl
27
+ * 3. resolvedOptions.defaultRedirect
28
+ * 4. referer (if from same origin)
29
+ * If none are available, throws an error.
30
+ */
31
+ protectRoute: (options?: {
25
32
  redirect?: string;
26
33
  }) => Promise<NextResponse | void>;
27
- isSignedIn: () => Promise<boolean>;
28
- getUser: () => Promise<any | null>;
29
- redirectToSignIn: (afterSignInUrl?: string) => NextResponse;
34
+ /** Check if the current request has a valid Asgardeo session */
35
+ isSignedIn: () => boolean;
36
+ /** Get the session ID from the current request */
37
+ getSessionId: () => string | undefined;
30
38
  };
31
- type AsgardeoMiddlewareHandler = (auth: AsgardeoAuth, req: NextRequest) => Promise<NextResponse | void> | NextResponse | void;
39
+ type AsgardeoMiddlewareHandler = (asgardeo: AsgardeoMiddlewareContext, req: NextRequest) => Promise<NextResponse | void> | NextResponse | void;
32
40
  /**
33
41
  * Asgardeo middleware that integrates authentication into your Next.js application.
34
42
  * Similar to Clerk's clerkMiddleware pattern.
@@ -39,7 +47,7 @@ type AsgardeoMiddlewareHandler = (auth: AsgardeoAuth, req: NextRequest) => Promi
39
47
  *
40
48
  * @example
41
49
  * ```typescript
42
- * // middleware.ts
50
+ * // middleware.ts - Basic usage
43
51
  * import { asgardeoMiddleware } from '@asgardeo/nextjs';
44
52
  *
45
53
  * export default asgardeoMiddleware();
@@ -47,17 +55,43 @@ type AsgardeoMiddlewareHandler = (auth: AsgardeoAuth, req: NextRequest) => Promi
47
55
  *
48
56
  * @example
49
57
  * ```typescript
50
- * // With protection
58
+ * // With route protection
51
59
  * import { asgardeoMiddleware, createRouteMatcher } from '@asgardeo/nextjs';
52
60
  *
53
61
  * const isProtectedRoute = createRouteMatcher(['/dashboard(.*)']);
54
62
  *
55
- * export default asgardeoMiddleware(async (auth, req) => {
63
+ * export default asgardeoMiddleware(async (asgardeo, req) => {
56
64
  * if (isProtectedRoute(req)) {
57
- * await auth.protect();
65
+ * await asgardeo.protectRoute();
58
66
  * }
59
67
  * });
60
68
  * ```
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * // Advanced usage with custom logic
73
+ * import { asgardeoMiddleware, createRouteMatcher } from '@asgardeo/nextjs';
74
+ *
75
+ * const isProtectedRoute = createRouteMatcher(['/dashboard(.*)']);
76
+ * const isAuthRoute = createRouteMatcher(['/sign-in', '/sign-up']);
77
+ *
78
+ * export default asgardeoMiddleware(async (asgardeo, req) => {
79
+ * // Skip protection for auth routes
80
+ * if (isAuthRoute(req)) return;
81
+ *
82
+ * // Protect specified routes
83
+ * if (isProtectedRoute(req)) {
84
+ * await asgardeo.protectRoute({ redirect: '/sign-in' });
85
+ * }
86
+ *
87
+ * // Check authentication status
88
+ * if (asgardeo.isSignedIn()) {
89
+ * console.log('User is authenticated with session:', asgardeo.getSessionId());
90
+ * }
91
+ * }, {
92
+ * defaultRedirect: '/sign-in'
93
+ * });
94
+ * ```
61
95
  */
62
96
  declare const asgardeoMiddleware: (handler?: AsgardeoMiddlewareHandler, options?: AsgardeoMiddlewareOptions | ((req: NextRequest) => AsgardeoMiddlewareOptions)) => ((request: NextRequest) => Promise<NextResponse>);
63
97
  export default asgardeoMiddleware;
@@ -16,7 +16,27 @@
16
16
  * under the License.
17
17
  */
18
18
  import { NextResponse } from 'next/server';
19
- import AsgardeoNextClient from '../AsgardeoNextClient';
19
+ import { CookieConfig } from '@asgardeo/node';
20
+ /**
21
+ * Checks if a request has a valid session ID in cookies.
22
+ * This is a lightweight check that can be used in middleware.
23
+ *
24
+ * @param request - The Next.js request object
25
+ * @returns True if a session ID exists, false otherwise
26
+ */
27
+ const hasValidSession = (request) => {
28
+ const sessionId = request.cookies.get(CookieConfig.SESSION_COOKIE_NAME)?.value;
29
+ return Boolean(sessionId && sessionId.trim().length > 0);
30
+ };
31
+ /**
32
+ * Gets the session ID from the request cookies.
33
+ *
34
+ * @param request - The Next.js request object
35
+ * @returns The session ID if it exists, undefined otherwise
36
+ */
37
+ const getSessionIdFromRequest = (request) => {
38
+ return request.cookies.get(CookieConfig.SESSION_COOKIE_NAME)?.value;
39
+ };
20
40
  /**
21
41
  * Asgardeo middleware that integrates authentication into your Next.js application.
22
42
  * Similar to Clerk's clerkMiddleware pattern.
@@ -27,7 +47,7 @@ import AsgardeoNextClient from '../AsgardeoNextClient';
27
47
  *
28
48
  * @example
29
49
  * ```typescript
30
- * // middleware.ts
50
+ * // middleware.ts - Basic usage
31
51
  * import { asgardeoMiddleware } from '@asgardeo/nextjs';
32
52
  *
33
53
  * export default asgardeoMiddleware();
@@ -35,79 +55,85 @@ import AsgardeoNextClient from '../AsgardeoNextClient';
35
55
  *
36
56
  * @example
37
57
  * ```typescript
38
- * // With protection
58
+ * // With route protection
59
+ * import { asgardeoMiddleware, createRouteMatcher } from '@asgardeo/nextjs';
60
+ *
61
+ * const isProtectedRoute = createRouteMatcher(['/dashboard(.*)']);
62
+ *
63
+ * export default asgardeoMiddleware(async (asgardeo, req) => {
64
+ * if (isProtectedRoute(req)) {
65
+ * await asgardeo.protectRoute();
66
+ * }
67
+ * });
68
+ * ```
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * // Advanced usage with custom logic
39
73
  * import { asgardeoMiddleware, createRouteMatcher } from '@asgardeo/nextjs';
40
74
  *
41
75
  * const isProtectedRoute = createRouteMatcher(['/dashboard(.*)']);
76
+ * const isAuthRoute = createRouteMatcher(['/sign-in', '/sign-up']);
77
+ *
78
+ * export default asgardeoMiddleware(async (asgardeo, req) => {
79
+ * // Skip protection for auth routes
80
+ * if (isAuthRoute(req)) return;
42
81
  *
43
- * export default asgardeoMiddleware(async (auth, req) => {
82
+ * // Protect specified routes
44
83
  * if (isProtectedRoute(req)) {
45
- * await auth.protect();
84
+ * await asgardeo.protectRoute({ redirect: '/sign-in' });
85
+ * }
86
+ *
87
+ * // Check authentication status
88
+ * if (asgardeo.isSignedIn()) {
89
+ * console.log('User is authenticated with session:', asgardeo.getSessionId());
46
90
  * }
91
+ * }, {
92
+ * defaultRedirect: '/sign-in'
47
93
  * });
48
94
  * ```
49
95
  */
50
96
  const asgardeoMiddleware = (handler, options) => {
51
97
  return async (request) => {
52
- // Resolve options - can be static or dynamic based on request
53
98
  const resolvedOptions = typeof options === 'function' ? options(request) : options || {};
54
- const asgardeoClient = AsgardeoNextClient.getInstance();
55
- // // Initialize client if not already done
56
- // if (!asgardeoClient.isInitialized && resolvedOptions) {
57
- // asgardeoClient.initialize(resolvedOptions);
58
- // }
59
- // // Debug logging
60
- // if (resolvedOptions.debug) {
61
- // console.log(`[Asgardeo Middleware] Processing request: ${request.nextUrl.pathname}`);
62
- // }
63
- // // Handle auth API routes automatically
64
- // if (request.nextUrl.pathname.startsWith('/api/auth/asgardeo')) {
65
- // if (resolvedOptions.debug) {
66
- // console.log(`[Asgardeo Middleware] Handling auth route: ${request.nextUrl.pathname}`);
67
- // }
68
- // return await asgardeoClient.handleAuthRequest(request);
69
- // }
70
- // // Create auth object for the handler
71
- // const auth: AsgardeoAuth = {
72
- // protect: async (options?: {redirect?: string}) => {
73
- // const isSignedIn = await asgardeoClient.isSignedIn(request);
74
- // if (!isSignedIn) {
75
- // const afterSignInUrl = options?.redirect || '/api/auth/asgardeo/signin';
76
- // return NextResponse.redirect(new URL(afterSignInUrl, request.url));
77
- // }
78
- // },
79
- // isSignedIn: async () => {
80
- // return await asgardeoClient.isSignedIn(request);
81
- // },
82
- // getUser: async () => {
83
- // return await asgardeoClient.getUser(request);
84
- // },
85
- // redirectToSignIn: (afterSignInUrl?: string) => {
86
- // const signInUrl = afterSignInUrl || '/api/auth/asgardeo/signin';
87
- // return NextResponse.redirect(new URL(signInUrl, request.url));
88
- // },
89
- // };
90
- // // Execute user-provided handler if present
91
- // let handlerResponse: NextResponse | void;
92
- // if (handler) {
93
- // handlerResponse = await handler(auth, request);
94
- // }
95
- // // If handler returned a response, use it
96
- // if (handlerResponse) {
97
- // return handlerResponse;
98
- // }
99
- // // Otherwise, continue with default behavior
100
- // const response = NextResponse.next();
101
- // // Add authentication context to response headers
102
- // const isSignedIn = await asgardeoClient.isSignedIn(request);
103
- // if (isSignedIn) {
104
- // response.headers.set('x-asgardeo-authenticated', 'true');
105
- // const user = await asgardeoClient.getUser(request);
106
- // if (user?.sub) {
107
- // response.headers.set('x-asgardeo-user-id', user.sub);
108
- // }
109
- // }
110
- // return response;
99
+ const sessionId = request.cookies.get(CookieConfig.SESSION_COOKIE_NAME)?.value;
100
+ const isAuthenticated = hasValidSession(request);
101
+ const asgardeo = {
102
+ protectRoute: async (options) => {
103
+ if (!isAuthenticated) {
104
+ const referer = request.headers.get('referer');
105
+ // TODO: Make this configurable or call the signIn() from here.
106
+ let fallbackRedirect = '/';
107
+ // If referer exists and is from the same origin, use it as fallback
108
+ if (referer) {
109
+ try {
110
+ const refererUrl = new URL(referer);
111
+ const requestUrl = new URL(request.url);
112
+ if (refererUrl.origin === requestUrl.origin) {
113
+ fallbackRedirect = refererUrl.pathname + refererUrl.search;
114
+ }
115
+ }
116
+ catch (error) {
117
+ // Invalid referer URL, ignore it
118
+ }
119
+ }
120
+ // Fallback chain: options.redirect -> resolvedOptions.signInUrl -> resolvedOptions.defaultRedirect -> referer (same origin only)
121
+ const redirectUrl = resolvedOptions?.signInUrl || fallbackRedirect;
122
+ const signInUrl = new URL(redirectUrl, request.url);
123
+ return NextResponse.redirect(signInUrl);
124
+ }
125
+ // Session exists, allow access
126
+ return;
127
+ },
128
+ isSignedIn: () => isAuthenticated,
129
+ getSessionId: () => sessionId,
130
+ };
131
+ if (handler) {
132
+ const result = await handler(asgardeo, request);
133
+ if (result) {
134
+ return result;
135
+ }
136
+ }
111
137
  return NextResponse.next();
112
138
  };
113
139
  };
@@ -1 +1 @@
1
- {"version":3,"file":"asgardeoMiddleware.js","sourceRoot":"","sources":["../../src/middleware/asgardeoMiddleware.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAc,YAAY,EAAC,MAAM,aAAa,CAAC;AACtD,OAAO,kBAAkB,MAAM,uBAAuB,CAAC;AAmBvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,kBAAkB,GAAG,CACzB,OAAmC,EACnC,OAAuF,EACpC,EAAE;IACrD,OAAO,KAAK,EAAE,OAAoB,EAAyB,EAAE;QAC3D,8DAA8D;QAC9D,MAAM,eAAe,GAAG,OAAO,OAAO,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,IAAI,EAAE,CAAC;QAEzF,MAAM,cAAc,GAAG,kBAAkB,CAAC,WAAW,EAAE,CA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C;4BACpC,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;4BAExC,IAAI,UAAU,CAAC,MAAM,KAAK,UAAU,CAAC,MAAM,EAAE,CAAC;gCAC5C,gBAAgB,GAAG,UAAU,CAAC,QAAQ,GAAG,UAAU,CAAC,MAAM,CAAC;4BAC7D,CAAC;wBACH,CAAC;wBAAC,OAAO,KAAK,EAAE,CAAC;4BACf,iCAAiC;wBACnC,CAAC;oBACH,CAAC;oBAED,iIAAiI;oBACjI,MAAM,WAAW,GAAY,eAAe,EAAE,SAAoB,IAAI,gBAAgB,CAAC;oBAEvF,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;oBAEpD,OAAO,YAAY,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;gBAC1C,CAAC;gBAED,+BAA+B;gBAC/B,OAAO;YACT,CAAC;YACD,UAAU,EAAE,GAAG,EAAE,CAAC,eAAe;YACjC,YAAY,EAAE,GAAG,EAAE,CAAC,SAAS;SAC9B,CAAC;QAEF,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;YAChD,IAAI,MAAM,EAAE,CAAC;gBACX,OAAO,MAAM,CAAC;YAChB,CAAC;QACH,CAAC;QAED,OAAO,YAAY,CAAC,IAAI,EAAE,CAAC;IAC7B,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,kBAAkB,CAAC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3
+ *
4
+ * WSO2 LLC. licenses this file to you under the Apache License,
5
+ * Version 2.0 (the "License"); you may not use this file except
6
+ * in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing,
12
+ * software distributed under the License is distributed on an
13
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ * KIND, either express or implied. See the License for the
15
+ * specific language governing permissions and limitations
16
+ * under the License.
17
+ */
18
+ import { NextRequest } from 'next/server';
19
+ /**
20
+ * Creates a route matcher function that tests if a request matches any of the given patterns.
21
+ *
22
+ * @param patterns - Array of route patterns to match. Supports glob-like patterns.
23
+ * @returns Function that tests if a request matches any of the patterns
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const isProtectedRoute = createRouteMatcher([
28
+ * '/dashboard(.*)',
29
+ * '/admin(.*)',
30
+ * '/profile'
31
+ * ]);
32
+ *
33
+ * if (isProtectedRoute(req)) {
34
+ * // Route is protected
35
+ * }
36
+ * ```
37
+ */
38
+ declare const createRouteMatcher: (patterns: string[]) => (req: NextRequest) => boolean;
39
+ export default createRouteMatcher;
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3
+ *
4
+ * WSO2 LLC. licenses this file to you under the Apache License,
5
+ * Version 2.0 (the "License"); you may not use this file except
6
+ * in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing,
12
+ * software distributed under the License is distributed on an
13
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ * KIND, either express or implied. See the License for the
15
+ * specific language governing permissions and limitations
16
+ * under the License.
17
+ */
18
+ /**
19
+ * Creates a route matcher function that tests if a request matches any of the given patterns.
20
+ *
21
+ * @param patterns - Array of route patterns to match. Supports glob-like patterns.
22
+ * @returns Function that tests if a request matches any of the patterns
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * const isProtectedRoute = createRouteMatcher([
27
+ * '/dashboard(.*)',
28
+ * '/admin(.*)',
29
+ * '/profile'
30
+ * ]);
31
+ *
32
+ * if (isProtectedRoute(req)) {
33
+ * // Route is protected
34
+ * }
35
+ * ```
36
+ */
37
+ const createRouteMatcher = (patterns) => {
38
+ const regexPatterns = patterns.map(pattern => {
39
+ // Convert glob-like patterns to regex
40
+ const regexPattern = pattern
41
+ .replace(/\./g, '\\.') // Escape dots
42
+ .replace(/\*/g, '.*') // Convert * to .*
43
+ .replace(/\(\.\*\)/g, '(.*)'); // Handle explicit (.*) patterns
44
+ return new RegExp(`^${regexPattern}$`);
45
+ });
46
+ return (req) => {
47
+ const pathname = req.nextUrl.pathname;
48
+ return regexPatterns.some(regex => regex.test(pathname));
49
+ };
50
+ };
51
+ export default createRouteMatcher;
52
+ //# sourceMappingURL=createRouteMatcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createRouteMatcher.js","sourceRoot":"","sources":["../../src/middleware/createRouteMatcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,kBAAkB,GAAG,CAAC,QAAkB,EAAE,EAAE;IAChD,MAAM,aAAa,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;QAC3C,sCAAsC;QACtC,MAAM,YAAY,GAAG,OAAO;aACzB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAE,cAAc;aACrC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAG,kBAAkB;aACzC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC,CAAC,gCAAgC;QAEjE,OAAO,IAAI,MAAM,CAAC,IAAI,YAAY,GAAG,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,GAAgB,EAAW,EAAE;QACnC,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC;QACtC,OAAO,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC3D,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,kBAAkB,CAAC"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3
+ *
4
+ * WSO2 LLC. licenses this file to you under the Apache License,
5
+ * Version 2.0 (the "License"); you may not use this file except
6
+ * in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing,
12
+ * software distributed under the License is distributed on an
13
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ * KIND, either express or implied. See the License for the
15
+ * specific language governing permissions and limitations
16
+ * under the License.
17
+ */
18
+ import { NextRequest } from 'next/server';
19
+ /**
20
+ * Creates a route matcher function that tests if a request matches any of the given patterns.
21
+ *
22
+ * @param patterns - Array of route patterns to match. Supports glob-like patterns.
23
+ * @returns Function that tests if a request matches any of the patterns
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const isProtectedRoute = createRouteMatcher([
28
+ * '/dashboard(.*)',
29
+ * '/admin(.*)',
30
+ * '/profile'
31
+ * ]);
32
+ *
33
+ * if (isProtectedRoute(req)) {
34
+ * // Route is protected
35
+ * }
36
+ * ```
37
+ */
38
+ export declare const createRouteMatcher: (patterns: string[]) => (req: NextRequest) => boolean;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3
+ *
4
+ * WSO2 LLC. licenses this file to you under the Apache License,
5
+ * Version 2.0 (the "License"); you may not use this file except
6
+ * in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing,
12
+ * software distributed under the License is distributed on an
13
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ * KIND, either express or implied. See the License for the
15
+ * specific language governing permissions and limitations
16
+ * under the License.
17
+ */
18
+ /**
19
+ * Creates a route matcher function that tests if a request matches any of the given patterns.
20
+ *
21
+ * @param patterns - Array of route patterns to match. Supports glob-like patterns.
22
+ * @returns Function that tests if a request matches any of the patterns
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * const isProtectedRoute = createRouteMatcher([
27
+ * '/dashboard(.*)',
28
+ * '/admin(.*)',
29
+ * '/profile'
30
+ * ]);
31
+ *
32
+ * if (isProtectedRoute(req)) {
33
+ * // Route is protected
34
+ * }
35
+ * ```
36
+ */
37
+ export const createRouteMatcher = (patterns) => {
38
+ const regexPatterns = patterns.map(pattern => {
39
+ // Convert glob-like patterns to regex
40
+ const regexPattern = pattern
41
+ .replace(/\./g, '\\.') // Escape dots
42
+ .replace(/\*/g, '.*') // Convert * to .*
43
+ .replace(/\(\.\*\)/g, '(.*)'); // Handle explicit (.*) patterns
44
+ return new RegExp(`^${regexPattern}$`);
45
+ });
46
+ return (req) => {
47
+ const pathname = req.nextUrl.pathname;
48
+ return regexPatterns.some(regex => regex.test(pathname));
49
+ };
50
+ };
51
+ //# sourceMappingURL=createRouteMatcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createRouteMatcher.js","sourceRoot":"","sources":["../../src/utils/createRouteMatcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,QAAkB,EAAE,EAAE;IACvD,MAAM,aAAa,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;QAC3C,sCAAsC;QACtC,MAAM,YAAY,GAAG,OAAO;aACzB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,cAAc;aACpC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,kBAAkB;aACvC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC,CAAC,gCAAgC;QAEjE,OAAO,IAAI,MAAM,CAAC,IAAI,YAAY,GAAG,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,GAAgB,EAAW,EAAE;QACnC,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC;QACtC,OAAO,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC3D,CAAC,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3
+ *
4
+ * WSO2 LLC. licenses this file to you under the Apache License,
5
+ * Version 2.0 (the "License"); you may not use this file except
6
+ * in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing,
12
+ * software distributed under the License is distributed on an
13
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ * KIND, either express or implied. See the License for the
15
+ * specific language governing permissions and limitations
16
+ * under the License.
17
+ */
18
+ import { NextRequest } from 'next/server';
19
+ /**
20
+ * Checks if a request has a valid session ID in cookies.
21
+ * This is a lightweight check that can be used in middleware.
22
+ *
23
+ * @param request - The Next.js request object
24
+ * @returns True if a session ID exists, false otherwise
25
+ */
26
+ export declare const hasValidSession: (request: NextRequest) => boolean;
27
+ /**
28
+ * Gets the session ID from the request cookies.
29
+ *
30
+ * @param request - The Next.js request object
31
+ * @returns The session ID if it exists, undefined otherwise
32
+ */
33
+ export declare const getSessionIdFromRequest: (request: NextRequest) => string | undefined;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3
+ *
4
+ * WSO2 LLC. licenses this file to you under the Apache License,
5
+ * Version 2.0 (the "License"); you may not use this file except
6
+ * in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing,
12
+ * software distributed under the License is distributed on an
13
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ * KIND, either express or implied. See the License for the
15
+ * specific language governing permissions and limitations
16
+ * under the License.
17
+ */
18
+ import { CookieConfig } from '@asgardeo/node';
19
+ /**
20
+ * Checks if a request has a valid session ID in cookies.
21
+ * This is a lightweight check that can be used in middleware.
22
+ *
23
+ * @param request - The Next.js request object
24
+ * @returns True if a session ID exists, false otherwise
25
+ */
26
+ export const hasValidSession = (request) => {
27
+ const sessionId = request.cookies.get(CookieConfig.SESSION_COOKIE_NAME)?.value;
28
+ return Boolean(sessionId && sessionId.trim().length > 0);
29
+ };
30
+ /**
31
+ * Gets the session ID from the request cookies.
32
+ *
33
+ * @param request - The Next.js request object
34
+ * @returns The session ID if it exists, undefined otherwise
35
+ */
36
+ export const getSessionIdFromRequest = (request) => {
37
+ return request.cookies.get(CookieConfig.SESSION_COOKIE_NAME)?.value;
38
+ };
39
+ //# sourceMappingURL=sessionUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessionUtils.js","sourceRoot":"","sources":["../../src/utils/sessionUtils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,OAAoB,EAAW,EAAE;IAC/D,MAAM,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,mBAAmB,CAAC,EAAE,KAAK,CAAC;IAC/E,OAAO,OAAO,CAAC,SAAS,IAAI,SAAS,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAC3D,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,OAAoB,EAAsB,EAAE;IAClF,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,mBAAmB,CAAC,EAAE,KAAK,CAAC;AACtE,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asgardeo/nextjs",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Next.js implementation of Asgardeo JavaScript SDK.",
5
5
  "keywords": [
6
6
  "asgardeo",
@@ -38,8 +38,8 @@
38
38
  "dependencies": {
39
39
  "@types/react": "^19.1.4",
40
40
  "tslib": "^2.8.1",
41
- "@asgardeo/node": "^0.0.7",
42
- "@asgardeo/react": "^0.5.9"
41
+ "@asgardeo/node": "^0.0.8",
42
+ "@asgardeo/react": "^0.5.10"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/node": "^22.15.3",