@txstate-mws/sveltekit-utils 1.0.3 → 1.1.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/dist/api.d.ts +3 -2
- package/dist/api.js +5 -4
- package/dist/api.js.map +1 -1
- package/dist/index.d.ts +1 -20
- package/dist/index.js +1 -31
- package/dist/index.js.map +1 -1
- package/dist/unifiedauth.d.ts +24 -0
- package/dist/unifiedauth.js +37 -0
- package/dist/unifiedauth.js.map +1 -0
- package/package.json +1 -1
package/dist/api.d.ts
CHANGED
|
@@ -11,12 +11,13 @@ type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
|
|
11
11
|
export declare function recordNavigations(callback: (evt: InteractionEvent) => void): void;
|
|
12
12
|
export declare class APIBase {
|
|
13
13
|
protected apiBase: string;
|
|
14
|
-
authRedirect: string;
|
|
14
|
+
authRedirect: string | URL;
|
|
15
|
+
loginRedirect: (api: APIBase, currentUrl: string) => URL;
|
|
15
16
|
token?: string;
|
|
16
17
|
fetch: (info: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
17
18
|
protected ready: () => void;
|
|
18
19
|
protected readyPromise: Promise<void>;
|
|
19
|
-
constructor(apiBase: string, authRedirect: string);
|
|
20
|
+
constructor(apiBase: string, authRedirect: string | URL, loginRedirect?: (api: APIBase, currentUrl: string) => URL);
|
|
20
21
|
init(token: string | undefined, fetch?: (info: RequestInfo, init?: RequestInit) => Promise<Response>): Promise<void>;
|
|
21
22
|
stringifyQuery(query: undefined | APIBaseQueryPayload): string;
|
|
22
23
|
protected request<ReturnType = any>(path: string, method: string, opts?: {
|
package/dist/api.js
CHANGED
|
@@ -4,6 +4,7 @@ import { get } from 'svelte/store';
|
|
|
4
4
|
import { rescue, toArray } from 'txstate-utils';
|
|
5
5
|
import { page } from '$app/stores';
|
|
6
6
|
import { afterNavigate } from '$app/navigation';
|
|
7
|
+
import { unifiedAuth } from './unifiedauth.js';
|
|
7
8
|
/**
|
|
8
9
|
* Provided for convenience in case you are not using APIBase but still want to record navigations
|
|
9
10
|
*
|
|
@@ -28,13 +29,15 @@ export function recordNavigations(callback) {
|
|
|
28
29
|
export class APIBase {
|
|
29
30
|
apiBase;
|
|
30
31
|
authRedirect;
|
|
32
|
+
loginRedirect;
|
|
31
33
|
token;
|
|
32
34
|
fetch;
|
|
33
35
|
ready;
|
|
34
36
|
readyPromise;
|
|
35
|
-
constructor(apiBase, authRedirect) {
|
|
37
|
+
constructor(apiBase, authRedirect, loginRedirect = unifiedAuth.loginRedirect) {
|
|
36
38
|
this.apiBase = apiBase;
|
|
37
39
|
this.authRedirect = authRedirect;
|
|
40
|
+
this.loginRedirect = loginRedirect;
|
|
38
41
|
this.readyPromise = new Promise(resolve => { this.ready = resolve; });
|
|
39
42
|
}
|
|
40
43
|
async init(token, fetch) {
|
|
@@ -76,9 +79,7 @@ export class APIBase {
|
|
|
76
79
|
const isJsonResponse = contentType && contentType.indexOf("application/json") !== -1;
|
|
77
80
|
if (!resp.ok && !(resp.status === 422 && opts?.inlineValidation)) {
|
|
78
81
|
if (resp.status === 401) {
|
|
79
|
-
|
|
80
|
-
loginRedirect.searchParams.set('requestedUrl', location.href);
|
|
81
|
-
location.href = loginRedirect.toString();
|
|
82
|
+
location.href = this.loginRedirect(this, location.href).toString();
|
|
82
83
|
throw error(401);
|
|
83
84
|
}
|
|
84
85
|
else {
|
package/dist/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAA;AACvD,OAAO,EAAyB,KAAK,EAAE,MAAM,eAAe,CAAA;AAC5D,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAClC,OAAO,EAAE,MAAM,EAAE,OA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AAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;YACzC,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAA;QACrD,CAAC;QACD,OAAO,WAAW,CAAC,IAAI,CAAA;IACzB,CAAC;IAES,cAAc,GAAuB,EAAE,CAAA;IACjD,iBAAiB,CAAC,GAAyC;QACzD,GAAG,CAAC,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,EAAG,CAAA;QAClC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,GAAuB,CAAC,CAAA;QACjD,UAAU,CAAC,GAAG,EAAE;YACd,MAAM,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC,CAAA;YACvC,IAAI,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,CAAA;YAC9B,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;QAChE,CAAC,EAAE,IAAI,CAAC,CAAA;IACV,CAAC;IAED;;;OAGG;IACH,iBAAiB;QACf,iBAAiB,CAAC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IACtD,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,2 @@
|
|
|
1
|
-
import { LoadEvent } from '@sveltejs/kit';
|
|
2
|
-
import { APIBase } from './api.js';
|
|
3
1
|
export * from './api.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* handleUnifiedAuth redirects users to unified auth by default if they don't have
|
|
7
|
-
* a token yet. If your UI is at least partially available to the public, set
|
|
8
|
-
* allowUnauthenticated to true.
|
|
9
|
-
*/
|
|
10
|
-
allowUnauthenticated?: boolean;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Your root +layout.ts' load function should call this method to ensure that it
|
|
14
|
-
* handles authentication from Unified Auth. By default it will redirect unauthenticated
|
|
15
|
-
* users to Unified Auth without a screen flash.
|
|
16
|
-
*
|
|
17
|
-
* If you set allowUnauthenticated: true and wait for the API to send a 401, there
|
|
18
|
-
* will often be a screen flash.
|
|
19
|
-
*/
|
|
20
|
-
export declare function handleUnifiedAuth(api: APIBase, input: LoadEvent, opts?: HandleOpts): Promise<void>;
|
|
21
|
-
export declare function requireAuthentication(api: APIBase, input: LoadEvent): void;
|
|
2
|
+
export * from './unifiedauth.js';
|
package/dist/index.js
CHANGED
|
@@ -1,33 +1,3 @@
|
|
|
1
|
-
import { redirect } from '@sveltejs/kit';
|
|
2
1
|
export * from './api.js';
|
|
3
|
-
|
|
4
|
-
* Your root +layout.ts' load function should call this method to ensure that it
|
|
5
|
-
* handles authentication from Unified Auth. By default it will redirect unauthenticated
|
|
6
|
-
* users to Unified Auth without a screen flash.
|
|
7
|
-
*
|
|
8
|
-
* If you set allowUnauthenticated: true and wait for the API to send a 401, there
|
|
9
|
-
* will often be a screen flash.
|
|
10
|
-
*/
|
|
11
|
-
export async function handleUnifiedAuth(api, input, opts) {
|
|
12
|
-
const unifiedJwt = input.url.searchParams.get('unifiedJwt') ?? undefined;
|
|
13
|
-
await api.init(unifiedJwt);
|
|
14
|
-
if (unifiedJwt) {
|
|
15
|
-
let redirectUrl = input.url.searchParams.get('requestedUrl') ?? undefined;
|
|
16
|
-
if (!redirectUrl) {
|
|
17
|
-
const currentUrl = new URL(input.url);
|
|
18
|
-
currentUrl.searchParams.delete('unifiedJwt');
|
|
19
|
-
redirectUrl = currentUrl.toString();
|
|
20
|
-
}
|
|
21
|
-
throw redirect(302, redirectUrl);
|
|
22
|
-
}
|
|
23
|
-
if (!opts?.allowUnauthenticated)
|
|
24
|
-
requireAuthentication(api, input);
|
|
25
|
-
}
|
|
26
|
-
export function requireAuthentication(api, input) {
|
|
27
|
-
if (!api.token) {
|
|
28
|
-
const loginRedirect = new URL(api.authRedirect);
|
|
29
|
-
loginRedirect.searchParams.set('requestedUrl', input.url.toString());
|
|
30
|
-
throw redirect(302, loginRedirect);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
2
|
+
export * from './unifiedauth.js';
|
|
33
3
|
//# 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,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type LoadEvent } from '@sveltejs/kit';
|
|
2
|
+
import type { APIBase } from './api.js';
|
|
3
|
+
interface HandleOpts {
|
|
4
|
+
/**
|
|
5
|
+
* handleUnifiedAuth redirects users to unified auth by default if they don't have
|
|
6
|
+
* a token yet. If your UI is at least partially available to the public, set
|
|
7
|
+
* allowUnauthenticated to true.
|
|
8
|
+
*/
|
|
9
|
+
allowUnauthenticated?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare const unifiedAuth: {
|
|
12
|
+
/**
|
|
13
|
+
* Your root +layout.ts' load function should call this method to ensure that it
|
|
14
|
+
* handles authentication from Unified Auth. By default it will redirect unauthenticated
|
|
15
|
+
* users to Unified Auth without a screen flash.
|
|
16
|
+
*
|
|
17
|
+
* If you set allowUnauthenticated: true and wait for the API to send a 401, there
|
|
18
|
+
* will often be a screen flash.
|
|
19
|
+
*/
|
|
20
|
+
handle(api: APIBase, input: LoadEvent, opts?: HandleOpts): Promise<void>;
|
|
21
|
+
loginRedirect(api: APIBase, currentUrl: string): URL;
|
|
22
|
+
requireAuth(api: APIBase, input: LoadEvent): void;
|
|
23
|
+
};
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { redirect } from '@sveltejs/kit';
|
|
2
|
+
export const unifiedAuth = {
|
|
3
|
+
/**
|
|
4
|
+
* Your root +layout.ts' load function should call this method to ensure that it
|
|
5
|
+
* handles authentication from Unified Auth. By default it will redirect unauthenticated
|
|
6
|
+
* users to Unified Auth without a screen flash.
|
|
7
|
+
*
|
|
8
|
+
* If you set allowUnauthenticated: true and wait for the API to send a 401, there
|
|
9
|
+
* will often be a screen flash.
|
|
10
|
+
*/
|
|
11
|
+
async handle(api, input, opts) {
|
|
12
|
+
const unifiedJwt = input.url.searchParams.get('unifiedJwt') ?? undefined;
|
|
13
|
+
await api.init(unifiedJwt);
|
|
14
|
+
if (unifiedJwt) {
|
|
15
|
+
let redirectUrl = input.url.searchParams.get('requestedUrl') ?? undefined;
|
|
16
|
+
if (!redirectUrl) {
|
|
17
|
+
const currentUrl = new URL(input.url);
|
|
18
|
+
currentUrl.searchParams.delete('unifiedJwt');
|
|
19
|
+
redirectUrl = currentUrl.toString();
|
|
20
|
+
}
|
|
21
|
+
throw redirect(302, redirectUrl);
|
|
22
|
+
}
|
|
23
|
+
if (!opts?.allowUnauthenticated)
|
|
24
|
+
this.requireAuth(api, input);
|
|
25
|
+
},
|
|
26
|
+
loginRedirect(api, currentUrl) {
|
|
27
|
+
const loginRedirect = new URL(api.authRedirect);
|
|
28
|
+
loginRedirect.searchParams.set('requestedUrl', currentUrl);
|
|
29
|
+
return loginRedirect;
|
|
30
|
+
},
|
|
31
|
+
requireAuth(api, input) {
|
|
32
|
+
if (!api.token) {
|
|
33
|
+
throw redirect(302, this.loginRedirect(api, input.url.toString()));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=unifiedauth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unifiedauth.js","sourceRoot":"","sources":["../src/unifiedauth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,QAAQ,EAAE,MAA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