@txstate-mws/sveltekit-utils 1.1.2 → 1.1.4

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.js CHANGED
@@ -83,15 +83,21 @@ export class APIBase {
83
83
  throw error(401);
84
84
  }
85
85
  else {
86
- const message = ((isJsonResponse ? (await rescue(resp.json()))?.message : await rescue(resp.text())) ?? resp.statusText);
87
- toasts.add(message);
86
+ const body = (isJsonResponse ? (await rescue(resp.json())) : await rescue(resp.text())) ?? resp.statusText;
87
+ let message = '';
88
+ if (typeof body === 'string')
89
+ message = body;
90
+ else if (body.message)
91
+ message = body.message;
92
+ else if (body[0]?.message)
93
+ message = body[0].message;
88
94
  throw error(resp.status, message);
89
95
  }
90
96
  }
91
97
  return ((isJsonResponse) ? await resp.json() : await resp.text());
92
98
  }
93
99
  catch (e) {
94
- toasts.add(e.message);
100
+ toasts.add(e.body?.message ?? e.message);
95
101
  throw e;
96
102
  }
97
103
  }
@@ -19,6 +19,7 @@ export declare const unifiedAuth: {
19
19
  */
20
20
  handle(api: APIBase, input: LoadEvent, opts?: HandleOpts): Promise<void>;
21
21
  loginRedirect(api: APIBase, currentUrl: string): URL;
22
+ logoutHref(api: APIBase): string;
22
23
  requireAuth(api: APIBase, input: LoadEvent): void;
23
24
  };
24
25
  export {};
@@ -1,4 +1,5 @@
1
1
  import { redirect } from '@sveltejs/kit';
2
+ import { isBlank } from 'txstate-utils';
2
3
  export const unifiedAuth = {
3
4
  /**
4
5
  * Your root +layout.ts' load function should call this method to ensure that it
@@ -28,6 +29,14 @@ export const unifiedAuth = {
28
29
  loginRedirect.searchParams.set('requestedUrl', currentUrl);
29
30
  return loginRedirect;
30
31
  },
32
+ logoutHref(api) {
33
+ if (isBlank(api.token))
34
+ return '#';
35
+ const authRedirect = new URL(api.authRedirect);
36
+ authRedirect.pathname = '/logout';
37
+ authRedirect.searchParams.set('unifiedJwt', api.token);
38
+ return authRedirect.toString();
39
+ },
31
40
  requireAuth(api, input) {
32
41
  if (!api.token) {
33
42
  throw redirect(302, this.loginRedirect(api, input.url.toString()));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@txstate-mws/sveltekit-utils",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Shared library for code that is specifically tied to sveltekit in addition to svelte.",
5
5
  "type": "module",
6
6
  "exports": {