@workos-inc/authkit-nextjs 2.12.1 → 2.13.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 +128 -70
- package/dist/esm/errors.js +33 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/get-authorization-url.js +7 -2
- package/dist/esm/get-authorization-url.js.map +1 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/middleware-helpers.js +99 -0
- package/dist/esm/middleware-helpers.js.map +1 -0
- package/dist/esm/session.js +11 -35
- package/dist/esm/session.js.map +1 -1
- package/dist/esm/types/errors.d.ts +15 -0
- package/dist/esm/types/index.d.ts +3 -1
- package/dist/esm/types/middleware-helpers.d.ts +25 -0
- package/dist/esm/types/session.d.ts +1 -1
- package/dist/esm/types/workos.d.ts +1 -1
- package/dist/esm/utils.js +0 -2
- package/dist/esm/utils.js.map +1 -1
- package/dist/esm/workos.js +1 -1
- package/package.json +2 -2
- package/src/components/authkit-provider.spec.tsx +41 -45
- package/src/components/min-max-button.spec.tsx +1 -0
- package/src/cookie.spec.ts +7 -1
- package/src/errors.spec.ts +108 -0
- package/src/errors.ts +46 -0
- package/src/get-authorization-url.ts +6 -10
- package/src/index.ts +16 -2
- package/src/middleware-helpers.spec.ts +231 -0
- package/src/middleware-helpers.ts +130 -0
- package/src/session.spec.ts +7 -10
- package/src/session.ts +16 -38
- package/src/utils.ts +0 -2
- package/src/workos.ts +1 -1
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { getSignInUrl, getSignUpUrl, signOut, switchToOrganization } from './auth.js';
|
|
2
2
|
import { handleAuth } from './authkit-callback-route.js';
|
|
3
|
+
import { AuthKitError, TokenRefreshError } from './errors.js';
|
|
3
4
|
import { authkit, authkitMiddleware } from './middleware.js';
|
|
5
|
+
export { applyResponseHeaders, handleAuthkitHeaders, partitionAuthkitHeaders, isAuthkitRequestHeader, AUTHKIT_REQUEST_HEADERS, type AuthkitHeadersResult, type AuthkitRedirectStatus, type AuthkitRequestHeader, type HandleAuthkitHeadersOptions, } from './middleware-helpers.js';
|
|
4
6
|
import { getTokenClaims, refreshSession, saveSession, withAuth } from './session.js';
|
|
5
7
|
import { validateApiKey } from './validate-api-key.js';
|
|
6
8
|
import { getWorkOS } from './workos.js';
|
|
7
9
|
export * from './interfaces.js';
|
|
8
|
-
export { authkit, authkitMiddleware, getSignInUrl, getSignUpUrl, getWorkOS, handleAuth, refreshSession, saveSession, signOut, switchToOrganization,
|
|
10
|
+
export { AuthKitError, TokenRefreshError, authkit, authkitMiddleware, getSignInUrl, getSignUpUrl, getTokenClaims, getWorkOS, handleAuth, refreshSession, saveSession, signOut, switchToOrganization, validateApiKey, withAuth, };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
/** Internal AuthKit headers - forwarded to downstream requests but never sent to browser. */
|
|
3
|
+
export declare const AUTHKIT_REQUEST_HEADERS: readonly ["x-workos-middleware", "x-url", "x-redirect-uri", "x-sign-up-paths", "x-workos-session"];
|
|
4
|
+
export type AuthkitRequestHeader = (typeof AUTHKIT_REQUEST_HEADERS)[number];
|
|
5
|
+
export declare function isAuthkitRequestHeader(name: string): boolean;
|
|
6
|
+
export interface AuthkitHeadersResult {
|
|
7
|
+
requestHeaders: Headers;
|
|
8
|
+
responseHeaders: Headers;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Partitions AuthKit headers into request headers (for withAuth) and response headers (for browser).
|
|
12
|
+
*/
|
|
13
|
+
export declare function partitionAuthkitHeaders(request: NextRequest, authkitHeaders: Headers): AuthkitHeadersResult;
|
|
14
|
+
export declare function applyResponseHeaders(response: NextResponse, responseHeaders: Headers): NextResponse;
|
|
15
|
+
export type AuthkitRedirectStatus = 302 | 303 | 307 | 308;
|
|
16
|
+
export interface HandleAuthkitHeadersOptions {
|
|
17
|
+
/** URL to redirect to (relative or absolute). */
|
|
18
|
+
redirect?: string | URL;
|
|
19
|
+
/** Redirect status code. @default 307 for GET/HEAD, 303 for POST/PUT/DELETE */
|
|
20
|
+
redirectStatus?: AuthkitRedirectStatus;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a NextResponse with properly merged AuthKit headers.
|
|
24
|
+
*/
|
|
25
|
+
export declare function handleAuthkitHeaders(request: NextRequest, authkitHeaders: Headers, options?: HandleAuthkitHeadersOptions): NextResponse;
|
|
@@ -3,7 +3,7 @@ import { NextRequest } from 'next/server';
|
|
|
3
3
|
import { AuthkitMiddlewareAuth, AuthkitOptions, AuthkitResponse, NoUserInfo, Session, UserInfo } from './interfaces.js';
|
|
4
4
|
import type { AuthenticationResponse } from '@workos-inc/node';
|
|
5
5
|
declare function encryptSession(session: Session): Promise<string>;
|
|
6
|
-
declare function updateSessionMiddleware(request: NextRequest, debug: boolean, middlewareAuth: AuthkitMiddlewareAuth, redirectUri: string, signUpPaths: string[], eagerAuth?: boolean): Promise<
|
|
6
|
+
declare function updateSessionMiddleware(request: NextRequest, debug: boolean, middlewareAuth: AuthkitMiddlewareAuth, redirectUri: string, signUpPaths: string[], eagerAuth?: boolean): Promise<import("next/server").NextResponse<unknown>>;
|
|
7
7
|
declare function updateSession(request: NextRequest, options?: AuthkitOptions): Promise<AuthkitResponse>;
|
|
8
8
|
declare function refreshSession(options: {
|
|
9
9
|
organizationId?: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { WorkOS } from '@workos-inc/node';
|
|
2
|
-
export declare const VERSION = "2.
|
|
2
|
+
export declare const VERSION = "2.13.0";
|
|
3
3
|
/**
|
|
4
4
|
* Create a WorkOS instance with the provided API key and options.
|
|
5
5
|
* If an instance already exists, it returns the existing instance.
|
package/dist/esm/utils.js
CHANGED
|
@@ -5,8 +5,6 @@ import { NextResponse } from 'next/server';
|
|
|
5
5
|
*/
|
|
6
6
|
export function setCachePreventionHeaders(headers) {
|
|
7
7
|
headers.set('Cache-Control', 'private, no-cache, no-store, must-revalidate, max-age=0');
|
|
8
|
-
headers.set('Pragma', 'no-cache');
|
|
9
|
-
headers.set('Expires', '0');
|
|
10
8
|
headers.set('x-middleware-cache', 'no-cache');
|
|
11
9
|
}
|
|
12
10
|
export function redirectWithFallback(redirectUri, headers) {
|
package/dist/esm/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAgB;IACxD,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,yDAAyD,CAAC,CAAC;IACxF,OAAO,CAAC,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAgB;IACxD,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,yDAAyD,CAAC,CAAC;IACxF,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,UAAU,CAAC,CAAC;AAChD,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,WAAmB,EAAE,OAAiB;IACzE,MAAM,UAAU,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,OAAO,EAAE,CAAC;IAClE,UAAU,CAAC,GAAG,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;IAExC,mEAAmE;IACnE,iCAAiC;IACjC,OAAO,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,QAAQ;QAC3B,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,WAAW,EAAE,EAAE,OAAO,EAAE,CAAC;QACjD,CAAC,CAAC,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,CAAC;AAC/D,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,SAA8D;IACtG,mEAAmE;IACnE,iCAAiC;IACjC,OAAO,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,IAAI;QACvB,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;QAC/C,CAAC,CAAC,IAAI,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE;YACtC,MAAM,EAAE,GAAG;YACX,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;SAChD,CAAC,CAAC;AACT,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,IAAI,CAAI,EAAW;IACjC,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,IAAI,MAAS,CAAC;IACd,OAAO,GAAG,EAAE;QACV,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,GAAG,EAAE,EAAE,CAAC;YACd,MAAM,GAAG,IAAI,CAAC;QAChB,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/esm/workos.js
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
|
import { lazy } from './utils.js';
|
|
4
|
-
export const VERSION = '2.
|
|
4
|
+
export const VERSION = '2.13.0';
|
|
5
5
|
const options = {
|
|
6
6
|
apiHostname: WORKOS_API_HOSTNAME,
|
|
7
7
|
https: WORKOS_API_HTTPS ? WORKOS_API_HTTPS === 'true' : true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workos-inc/authkit-nextjs",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"description": "Authentication and session helpers for using WorkOS & AuthKit with Next.js",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"eslint-plugin-require-extensions": "^0.1.3",
|
|
58
58
|
"jest": "^29.7.0",
|
|
59
59
|
"jest-environment-jsdom": "^29.7.0",
|
|
60
|
-
"next": "^16.0.
|
|
60
|
+
"next": "^16.0.10",
|
|
61
61
|
"prettier": "^3.3.3",
|
|
62
62
|
"ts-jest": "^29.2.5",
|
|
63
63
|
"ts-node": "^10.9.2",
|
|
@@ -227,63 +227,59 @@ describe('AuthKitProvider', () => {
|
|
|
227
227
|
});
|
|
228
228
|
});
|
|
229
229
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
window.location = { ...window.location, reload: jest.fn() };
|
|
239
|
-
|
|
240
|
-
render(
|
|
241
|
-
<AuthKitProvider>
|
|
242
|
-
<div>Test Child</div>
|
|
243
|
-
</AuthKitProvider>,
|
|
244
|
-
);
|
|
245
|
-
|
|
246
|
-
act(() => {
|
|
247
|
-
// Simulate visibility change
|
|
248
|
-
window.dispatchEvent(new Event('visibilitychange'));
|
|
230
|
+
describe('window.location.reload behavior', () => {
|
|
231
|
+
let originalLocation: Location;
|
|
232
|
+
|
|
233
|
+
beforeEach(() => {
|
|
234
|
+
originalLocation = window.location;
|
|
235
|
+
// @ts-expect-error - deleting window.location to mock it
|
|
236
|
+
delete window.location;
|
|
237
|
+
window.location = { reload: jest.fn() } as unknown as Location;
|
|
249
238
|
});
|
|
250
239
|
|
|
251
|
-
|
|
252
|
-
|
|
240
|
+
afterEach(() => {
|
|
241
|
+
window.location = originalLocation;
|
|
253
242
|
});
|
|
254
243
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
});
|
|
244
|
+
it('should reload the page when session is expired and no onSessionExpired handler is provided', async () => {
|
|
245
|
+
(checkSessionAction as jest.Mock).mockRejectedValueOnce(new Error('Failed to fetch'));
|
|
258
246
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
247
|
+
render(
|
|
248
|
+
<AuthKitProvider>
|
|
249
|
+
<div>Test Child</div>
|
|
250
|
+
</AuthKitProvider>,
|
|
251
|
+
);
|
|
262
252
|
|
|
263
|
-
|
|
253
|
+
act(() => {
|
|
254
|
+
// Simulate visibility change
|
|
255
|
+
window.dispatchEvent(new Event('visibilitychange'));
|
|
256
|
+
});
|
|
264
257
|
|
|
265
|
-
|
|
266
|
-
|
|
258
|
+
await waitFor(() => {
|
|
259
|
+
expect(window.location.reload).toHaveBeenCalled();
|
|
260
|
+
});
|
|
261
|
+
});
|
|
267
262
|
|
|
268
|
-
|
|
263
|
+
it('should not call onSessionExpired or reload the page if session is valid', async () => {
|
|
264
|
+
(checkSessionAction as jest.Mock).mockResolvedValueOnce(true);
|
|
265
|
+
const onSessionExpired = jest.fn();
|
|
269
266
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
267
|
+
render(
|
|
268
|
+
<AuthKitProvider onSessionExpired={onSessionExpired}>
|
|
269
|
+
<div>Test Child</div>
|
|
270
|
+
</AuthKitProvider>,
|
|
271
|
+
);
|
|
275
272
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
273
|
+
act(() => {
|
|
274
|
+
// Simulate visibility change
|
|
275
|
+
window.dispatchEvent(new Event('visibilitychange'));
|
|
276
|
+
});
|
|
280
277
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
278
|
+
await waitFor(() => {
|
|
279
|
+
expect(onSessionExpired).not.toHaveBeenCalled();
|
|
280
|
+
expect(window.location.reload).not.toHaveBeenCalled();
|
|
281
|
+
});
|
|
284
282
|
});
|
|
285
|
-
|
|
286
|
-
window.location = originalLocation;
|
|
287
283
|
});
|
|
288
284
|
});
|
|
289
285
|
|
package/src/cookie.spec.ts
CHANGED
|
@@ -147,11 +147,17 @@ describe('cookie.ts', () => {
|
|
|
147
147
|
});
|
|
148
148
|
|
|
149
149
|
describe('getJwtCookie', () => {
|
|
150
|
+
const originalEnv = process.env;
|
|
151
|
+
|
|
150
152
|
beforeEach(() => {
|
|
151
|
-
|
|
153
|
+
process.env = { ...originalEnv };
|
|
152
154
|
delete process.env.NODE_ENV;
|
|
153
155
|
});
|
|
154
156
|
|
|
157
|
+
afterEach(() => {
|
|
158
|
+
process.env = originalEnv;
|
|
159
|
+
});
|
|
160
|
+
|
|
155
161
|
it('should create JWT cookie with Secure flag for HTTPS URLs', async () => {
|
|
156
162
|
const { getJwtCookie } = await import('./cookie');
|
|
157
163
|
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { AuthKitError, TokenRefreshError, getSessionErrorContext } from './errors.js';
|
|
2
|
+
import type { Session } from './interfaces.js';
|
|
3
|
+
import type { User } from '@workos-inc/node';
|
|
4
|
+
|
|
5
|
+
describe('AuthKitError', () => {
|
|
6
|
+
it('creates error with message', () => {
|
|
7
|
+
const error = new AuthKitError('Test error');
|
|
8
|
+
|
|
9
|
+
expect(error.message).toBe('Test error');
|
|
10
|
+
expect(error.name).toBe('AuthKitError');
|
|
11
|
+
expect(error).toBeInstanceOf(Error);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('creates error with cause and data', () => {
|
|
15
|
+
const originalError = new Error('Original error');
|
|
16
|
+
const data = { userId: '123' };
|
|
17
|
+
const error = new AuthKitError('Test error', originalError, data);
|
|
18
|
+
|
|
19
|
+
expect(error.cause).toBe(originalError);
|
|
20
|
+
expect(error.data).toEqual(data);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe('TokenRefreshError', () => {
|
|
25
|
+
it('creates error with correct name and inheritance', () => {
|
|
26
|
+
const error = new TokenRefreshError('Refresh failed');
|
|
27
|
+
|
|
28
|
+
expect(error.name).toBe('TokenRefreshError');
|
|
29
|
+
expect(error.message).toBe('Refresh failed');
|
|
30
|
+
expect(error).toBeInstanceOf(AuthKitError);
|
|
31
|
+
expect(error).toBeInstanceOf(Error);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('creates error with cause and context', () => {
|
|
35
|
+
const originalError = new Error('Network error');
|
|
36
|
+
const error = new TokenRefreshError('Refresh failed', originalError, {
|
|
37
|
+
userId: 'user_123',
|
|
38
|
+
sessionId: 'session_456',
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
expect(error.cause).toBe(originalError);
|
|
42
|
+
expect(error.userId).toBe('user_123');
|
|
43
|
+
expect(error.sessionId).toBe('session_456');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('has undefined properties when no context provided', () => {
|
|
47
|
+
const error = new TokenRefreshError('Refresh failed');
|
|
48
|
+
|
|
49
|
+
expect(error.userId).toBeUndefined();
|
|
50
|
+
expect(error.sessionId).toBeUndefined();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('getSessionErrorContext', () => {
|
|
55
|
+
function createTestJwt(payload: Record<string, unknown>): string {
|
|
56
|
+
const header = btoa(JSON.stringify({ alg: 'HS256', typ: 'JWT' }));
|
|
57
|
+
const payloadStr = btoa(JSON.stringify(payload));
|
|
58
|
+
return `${header}.${payloadStr}.test-signature`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
it('returns empty object for missing session', () => {
|
|
62
|
+
expect(getSessionErrorContext(null)).toEqual({});
|
|
63
|
+
expect(getSessionErrorContext(undefined)).toEqual({});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('extracts userId and sessionId from access token', () => {
|
|
67
|
+
const session: Session = {
|
|
68
|
+
accessToken: createTestJwt({ sub: 'user_456', sid: 'session_123' }),
|
|
69
|
+
refreshToken: 'refresh_token',
|
|
70
|
+
user: {
|
|
71
|
+
id: 'user_456',
|
|
72
|
+
email: 'test@example.com',
|
|
73
|
+
emailVerified: true,
|
|
74
|
+
profilePictureUrl: null,
|
|
75
|
+
firstName: null,
|
|
76
|
+
lastName: null,
|
|
77
|
+
createdAt: '2024-01-01',
|
|
78
|
+
updatedAt: '2024-01-01',
|
|
79
|
+
object: 'user',
|
|
80
|
+
} as User,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const context = getSessionErrorContext(session);
|
|
84
|
+
expect(context.userId).toBe('user_456');
|
|
85
|
+
expect(context.sessionId).toBe('session_123');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('returns empty object for invalid JWT', () => {
|
|
89
|
+
const session: Session = {
|
|
90
|
+
accessToken: 'invalid-jwt',
|
|
91
|
+
refreshToken: 'refresh_token',
|
|
92
|
+
user: {
|
|
93
|
+
id: 'user_123',
|
|
94
|
+
email: 'test@example.com',
|
|
95
|
+
emailVerified: true,
|
|
96
|
+
profilePictureUrl: null,
|
|
97
|
+
firstName: null,
|
|
98
|
+
lastName: null,
|
|
99
|
+
createdAt: '2024-01-01',
|
|
100
|
+
updatedAt: '2024-01-01',
|
|
101
|
+
object: 'user',
|
|
102
|
+
} as User,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const context = getSessionErrorContext(session);
|
|
106
|
+
expect(context).toEqual({});
|
|
107
|
+
});
|
|
108
|
+
});
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Session } from './interfaces.js';
|
|
2
|
+
import { decodeJwt } from './jwt.js';
|
|
3
|
+
|
|
4
|
+
export class AuthKitError extends Error {
|
|
5
|
+
data?: Record<string, unknown>;
|
|
6
|
+
|
|
7
|
+
constructor(message: string, cause?: unknown, data?: Record<string, unknown>) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'AuthKitError';
|
|
10
|
+
this.cause = cause;
|
|
11
|
+
this.data = data;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface TokenRefreshErrorContext {
|
|
16
|
+
userId?: string;
|
|
17
|
+
sessionId?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class TokenRefreshError extends AuthKitError {
|
|
21
|
+
readonly userId?: string;
|
|
22
|
+
readonly sessionId?: string;
|
|
23
|
+
|
|
24
|
+
constructor(message: string, cause?: unknown, context?: TokenRefreshErrorContext) {
|
|
25
|
+
super(message, cause);
|
|
26
|
+
this.name = 'TokenRefreshError';
|
|
27
|
+
this.userId = context?.userId;
|
|
28
|
+
this.sessionId = context?.sessionId;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function getSessionErrorContext(session?: Session | null): TokenRefreshErrorContext {
|
|
33
|
+
if (!session?.accessToken) {
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
const { payload } = decodeJwt(session.accessToken);
|
|
39
|
+
return {
|
|
40
|
+
userId: payload.sub,
|
|
41
|
+
sessionId: payload.sid,
|
|
42
|
+
};
|
|
43
|
+
} catch {
|
|
44
|
+
return {};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -4,16 +4,12 @@ import { GetAuthURLOptions } from './interfaces.js';
|
|
|
4
4
|
import { headers } from 'next/headers';
|
|
5
5
|
|
|
6
6
|
async function getAuthorizationUrl(options: GetAuthURLOptions = {}) {
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
loginHint,
|
|
14
|
-
prompt,
|
|
15
|
-
state: customState,
|
|
16
|
-
} = options;
|
|
7
|
+
const { returnPathname, screenHint, organizationId, loginHint, prompt, state: customState } = options;
|
|
8
|
+
let redirectUri = options.redirectUri;
|
|
9
|
+
if (!redirectUri) {
|
|
10
|
+
const headersList = await headers();
|
|
11
|
+
redirectUri = headersList.get('x-redirect-uri') ?? undefined;
|
|
12
|
+
}
|
|
17
13
|
|
|
18
14
|
const internalState = returnPathname
|
|
19
15
|
? btoa(JSON.stringify({ returnPathname })).replace(/\+/g, '-').replace(/\//g, '_')
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { getSignInUrl, getSignUpUrl, signOut, switchToOrganization } from './auth.js';
|
|
2
2
|
import { handleAuth } from './authkit-callback-route.js';
|
|
3
|
+
import { AuthKitError, TokenRefreshError } from './errors.js';
|
|
3
4
|
import { authkit, authkitMiddleware } from './middleware.js';
|
|
5
|
+
export {
|
|
6
|
+
applyResponseHeaders,
|
|
7
|
+
handleAuthkitHeaders,
|
|
8
|
+
partitionAuthkitHeaders,
|
|
9
|
+
isAuthkitRequestHeader,
|
|
10
|
+
AUTHKIT_REQUEST_HEADERS,
|
|
11
|
+
type AuthkitHeadersResult,
|
|
12
|
+
type AuthkitRedirectStatus,
|
|
13
|
+
type AuthkitRequestHeader,
|
|
14
|
+
type HandleAuthkitHeadersOptions,
|
|
15
|
+
} from './middleware-helpers.js';
|
|
4
16
|
import { getTokenClaims, refreshSession, saveSession, withAuth } from './session.js';
|
|
5
17
|
import { validateApiKey } from './validate-api-key.js';
|
|
6
18
|
import { getWorkOS } from './workos.js';
|
|
@@ -8,17 +20,19 @@ import { getWorkOS } from './workos.js';
|
|
|
8
20
|
export * from './interfaces.js';
|
|
9
21
|
|
|
10
22
|
export {
|
|
23
|
+
AuthKitError,
|
|
24
|
+
TokenRefreshError,
|
|
11
25
|
authkit,
|
|
12
26
|
authkitMiddleware,
|
|
13
27
|
getSignInUrl,
|
|
14
28
|
getSignUpUrl,
|
|
29
|
+
getTokenClaims,
|
|
15
30
|
getWorkOS,
|
|
16
31
|
handleAuth,
|
|
17
32
|
refreshSession,
|
|
18
33
|
saveSession,
|
|
19
34
|
signOut,
|
|
20
35
|
switchToOrganization,
|
|
21
|
-
withAuth,
|
|
22
|
-
getTokenClaims,
|
|
23
36
|
validateApiKey,
|
|
37
|
+
withAuth,
|
|
24
38
|
};
|