@workos-inc/authkit-nextjs 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/esm/authkit-callback-route.js +9 -6
- package/dist/esm/authkit-callback-route.js.map +1 -1
- package/dist/esm/types/interfaces.d.ts +5 -0
- package/dist/esm/types/workos.d.ts +1 -1
- package/dist/esm/workos.js +1 -1
- package/package.json +1 -1
- package/src/authkit-callback-route.ts +12 -8
- package/src/interfaces.ts +2 -0
- package/src/workos.ts +1 -1
package/README.md
CHANGED
|
@@ -97,6 +97,7 @@ export const GET = handleAUth({
|
|
|
97
97
|
| `returnPathname` | `/` | The pathname to redirect the user to after signing in |
|
|
98
98
|
| `baseURL` | `undefined` | The base URL to use for the redirect URI instead of the one in the request. Useful if the app is being run in a container like docker where the hostname can be different from the one in the request |
|
|
99
99
|
| `onSuccess` | `undefined` | A function that receives successful authentication data and can be used for side-effects like persisting tokens |
|
|
100
|
+
| `onError` | `undefined` | A function that can receive the error and the request and handle the error in its own way. |
|
|
100
101
|
|
|
101
102
|
### Middleware
|
|
102
103
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { cookies } from 'next/headers';
|
|
2
|
-
import {
|
|
2
|
+
import { getCookieOptions } from './cookie.js';
|
|
3
3
|
import { WORKOS_CLIENT_ID, WORKOS_COOKIE_NAME } from './env-variables.js';
|
|
4
4
|
import { encryptSession } from './session.js';
|
|
5
5
|
import { errorResponseWithFallback, redirectWithFallback } from './utils.js';
|
|
6
|
-
import {
|
|
6
|
+
import { workos } from './workos.js';
|
|
7
7
|
export function handleAuth(options = {}) {
|
|
8
|
-
const { returnPathname: returnPathnameOption = '/', baseURL, onSuccess } = options;
|
|
8
|
+
const { returnPathname: returnPathnameOption = '/', baseURL, onSuccess, onError } = options;
|
|
9
9
|
// Throw early if baseURL is provided but invalid
|
|
10
10
|
if (baseURL) {
|
|
11
11
|
try {
|
|
@@ -67,12 +67,15 @@ export function handleAuth(options = {}) {
|
|
|
67
67
|
error: error instanceof Error ? error.message : String(error),
|
|
68
68
|
};
|
|
69
69
|
console.error(errorRes);
|
|
70
|
-
return errorResponse();
|
|
70
|
+
return errorResponse(request, error);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
return errorResponse();
|
|
73
|
+
return errorResponse(request);
|
|
74
74
|
};
|
|
75
|
-
function errorResponse() {
|
|
75
|
+
function errorResponse(request, error) {
|
|
76
|
+
if (onError) {
|
|
77
|
+
return onError({ error, request });
|
|
78
|
+
}
|
|
76
79
|
return errorResponseWithFallback({
|
|
77
80
|
error: {
|
|
78
81
|
message: 'Something went wrong',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authkit-callback-route.js","sourceRoot":"","sources":["../../src/authkit-callback-route.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"authkit-callback-route.js","sourceRoot":"","sources":["../../src/authkit-callback-route.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAE1E,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAC7E,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,UAAU,UAAU,CAAC,UAA6B,EAAE;IACxD,MAAM,EAAE,cAAc,EAAE,oBAAoB,GAAG,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAE5F,iDAAiD;IACjD,IAAI,OAAO,EAAE,CAAC;QACZ,IAAI,CAAC;YACH,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC;QACnB,CAAC;QAA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QAAQ,GAAG;oBACf,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;iBAC9D,CAAC;gBAEF,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;gBAExB,OAAO,aAAa,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACvC,CAAC;QACH,CAAC;QAED,OAAO,aAAa,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC,CAAC;IAEF,SAAS,aAAa,CAAC,OAAoB,EAAE,KAAe;QAC1D,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,OAAO,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;QACrC,CAAC;QAED,OAAO,yBAAyB,CAAC;YAC/B,KAAK,EAAE;gBACL,OAAO,EAAE,sBAAsB;gBAC/B,WAAW,EAAE,8FAA8F;aAC5G;SACF,CAAC,CAAC;IACL,CAAC;AACH,CAAC"}
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { OauthTokens, User } from '@workos-inc/node';
|
|
2
|
+
import { type NextRequest } from 'next/server';
|
|
2
3
|
export interface HandleAuthOptions {
|
|
3
4
|
returnPathname?: string;
|
|
4
5
|
baseURL?: string;
|
|
5
6
|
onSuccess?: (data: HandleAuthSuccessData) => void | Promise<void>;
|
|
7
|
+
onError?: (params: {
|
|
8
|
+
error?: unknown;
|
|
9
|
+
request: NextRequest;
|
|
10
|
+
}) => Response | Promise<Response>;
|
|
6
11
|
}
|
|
7
12
|
export interface HandleAuthSuccessData extends Session {
|
|
8
13
|
oauthTokens?: OauthTokens;
|
package/dist/esm/workos.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { WorkOS } from '@workos-inc/node';
|
|
2
2
|
import { WORKOS_API_HOSTNAME, WORKOS_API_KEY, WORKOS_API_HTTPS, WORKOS_API_PORT } from './env-variables.js';
|
|
3
|
-
export const VERSION = '1.
|
|
3
|
+
export const VERSION = '1.3.0';
|
|
4
4
|
const options = {
|
|
5
5
|
apiHostname: WORKOS_API_HOSTNAME,
|
|
6
6
|
https: WORKOS_API_HTTPS ? WORKOS_API_HTTPS === 'true' : true,
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { NextRequest } from 'next/server';
|
|
2
1
|
import { cookies } from 'next/headers';
|
|
3
|
-
import {
|
|
2
|
+
import { NextRequest } from 'next/server';
|
|
3
|
+
import { getCookieOptions } from './cookie.js';
|
|
4
4
|
import { WORKOS_CLIENT_ID, WORKOS_COOKIE_NAME } from './env-variables.js';
|
|
5
|
+
import { HandleAuthOptions } from './interfaces.js';
|
|
5
6
|
import { encryptSession } from './session.js';
|
|
6
7
|
import { errorResponseWithFallback, redirectWithFallback } from './utils.js';
|
|
7
|
-
import {
|
|
8
|
-
import { HandleAuthOptions } from './interfaces.js';
|
|
8
|
+
import { workos } from './workos.js';
|
|
9
9
|
|
|
10
10
|
export function handleAuth(options: HandleAuthOptions = {}) {
|
|
11
|
-
const { returnPathname: returnPathnameOption = '/', baseURL, onSuccess } = options;
|
|
11
|
+
const { returnPathname: returnPathnameOption = '/', baseURL, onSuccess, onError } = options;
|
|
12
12
|
|
|
13
13
|
// Throw early if baseURL is provided but invalid
|
|
14
14
|
if (baseURL) {
|
|
@@ -83,14 +83,18 @@ export function handleAuth(options: HandleAuthOptions = {}) {
|
|
|
83
83
|
|
|
84
84
|
console.error(errorRes);
|
|
85
85
|
|
|
86
|
-
return errorResponse();
|
|
86
|
+
return errorResponse(request, error);
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
return errorResponse();
|
|
90
|
+
return errorResponse(request);
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
-
function errorResponse() {
|
|
93
|
+
function errorResponse(request: NextRequest, error?: unknown) {
|
|
94
|
+
if (onError) {
|
|
95
|
+
return onError({ error, request });
|
|
96
|
+
}
|
|
97
|
+
|
|
94
98
|
return errorResponseWithFallback({
|
|
95
99
|
error: {
|
|
96
100
|
message: 'Something went wrong',
|
package/src/interfaces.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { OauthTokens, User } from '@workos-inc/node';
|
|
2
|
+
import { type NextRequest } from 'next/server';
|
|
2
3
|
|
|
3
4
|
export interface HandleAuthOptions {
|
|
4
5
|
returnPathname?: string;
|
|
5
6
|
baseURL?: string;
|
|
6
7
|
onSuccess?: (data: HandleAuthSuccessData) => void | Promise<void>;
|
|
8
|
+
onError?: (params: { error?: unknown; request: NextRequest }) => Response | Promise<Response>;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
export interface HandleAuthSuccessData extends Session {
|
package/src/workos.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { WorkOS } from '@workos-inc/node';
|
|
2
2
|
import { WORKOS_API_HOSTNAME, WORKOS_API_KEY, WORKOS_API_HTTPS, WORKOS_API_PORT } from './env-variables.js';
|
|
3
3
|
|
|
4
|
-
export const VERSION = '1.
|
|
4
|
+
export const VERSION = '1.3.0';
|
|
5
5
|
|
|
6
6
|
const options = {
|
|
7
7
|
apiHostname: WORKOS_API_HOSTNAME,
|