@authgate/browser 0.1.0 → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alexander Lupatsiy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -12,6 +12,8 @@ coupling and hidden behavior.
12
12
 
13
13
  - Read AuthGate CSRF token from cookies
14
14
  - Perform a safe logout request with CSRF protection
15
+ - Explicit success/failure signaling
16
+ - Optional redirect after logout
15
17
  - Zero dependencies
16
18
  - Framework-agnostic (works with React, Vue, vanilla JS, etc.)
17
19
 
@@ -37,6 +39,8 @@ const csrf = getCSRFToken();
37
39
 
38
40
  Returns the value of the `authgate_csrf` cookie, or `null` if not present.
39
41
 
42
+ This function only **reads** the CSRF token. It does not generate or validate it.
43
+
40
44
  ---
41
45
 
42
46
  ### Logout
@@ -44,7 +48,7 @@ Returns the value of the `authgate_csrf` cookie, or `null` if not present.
44
48
  ```ts
45
49
  import { logout } from "@authgate/browser";
46
50
 
47
- await logout();
51
+ const result = await logout();
48
52
  ```
49
53
 
50
54
  This will:
@@ -53,6 +57,17 @@ This will:
53
57
  - Attach the CSRF token via `X-CSRF-Token`
54
58
  - Include credentials (`cookies`)
55
59
 
60
+ The function returns a result indicating whether logout succeeded:
61
+
62
+ ```ts
63
+ type LogoutResult =
64
+ | { ok: true }
65
+ | { ok: false; reason: "missing_csrf" | "request_failed" | "unauthorized" };
66
+ ```
67
+
68
+ Applications that do not need to react programmatically to logout may safely
69
+ ignore the return value.
70
+
56
71
  ---
57
72
 
58
73
  ### Logout with redirect
@@ -61,7 +76,24 @@ This will:
61
76
  await logout({ redirectTo: "/" });
62
77
  ```
63
78
 
64
- After a successful logout request, the browser is redirected to the given path.
79
+ If the logout request succeeds, the browser is redirected to the given path.
80
+
81
+ Redirecting is an optional side-effect and does **not** define success.
82
+ Applications may choose to handle navigation themselves instead.
83
+
84
+ ---
85
+
86
+ ### Example (React / SPA)
87
+
88
+ ```ts
89
+ const result = await logout();
90
+
91
+ if (result.ok) {
92
+ setUser(null);
93
+ } else {
94
+ console.error("Logout failed:", result.reason);
95
+ }
96
+ ```
65
97
 
66
98
  ---
67
99
 
@@ -80,7 +112,7 @@ No cookies are set, modified, or cleared by this library.
80
112
  - No authentication logic
81
113
  - No token refresh
82
114
  - No session management
83
- - No redirects except when explicitly requested
115
+ - No implicit redirects
84
116
  - No framework-specific helpers
85
117
 
86
118
  This package exists solely to reduce boilerplate and prevent integration mistakes.
@@ -91,7 +123,6 @@ This package exists solely to reduce boilerplate and prevent integration mistake
91
123
 
92
124
  - Works with any backend protected by AuthGate
93
125
  - Compatible with SSR and SPA architectures
94
- - Safe to use in multi-app or monorepo setups
95
126
 
96
127
  ---
97
128
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,40 @@
1
+ /**
2
+ * Returns the AuthGate CSRF token from the browser cookies.
3
+ *
4
+ * The CSRF token is issued by AuthGate and stored in the `authgate_csrf`
5
+ * cookie. This helper does not validate the token; it only reads it.
6
+ *
7
+ * @returns The CSRF token string, or `null` if the cookie is missing.
8
+ */
1
9
  export declare function getCSRFToken(): string | null;
10
+ /**
11
+ * The result of a logout attempt.
12
+ *
13
+ * - `{ ok: true }` indicates that logout succeeded.
14
+ * - `{ ok: false, reason }` indicates that logout failed for a known reason.
15
+ */
16
+ type LogoutResult = {
17
+ ok: true;
18
+ } | {
19
+ ok: false;
20
+ reason: "missing_csrf" | "request_failed" | "unauthorized";
21
+ };
22
+ /**
23
+ * Logs the user out by calling the AuthGate logout endpoint.
24
+ *
25
+ * This function:
26
+ * - Reads the CSRF token from the browser cookies
27
+ * - Sends a POST request to `/auth/logout`
28
+ * - Optionally redirects the browser on success
29
+ *
30
+ * Redirecting is a side-effect and does not define success. Applications
31
+ * that need to react to logout programmatically (e.g. SPA state updates)
32
+ * should inspect the returned result instead.
33
+ *
34
+ * @param opts.redirectTo Optional URL to redirect to after successful logout.
35
+ * @returns A `LogoutResult` indicating whether logout succeeded or failed.
36
+ */
2
37
  export declare function logout(opts?: {
3
38
  redirectTo?: string;
4
- }): Promise<void>;
39
+ }): Promise<LogoutResult>;
40
+ export {};
package/dist/index.js CHANGED
@@ -1,22 +1,69 @@
1
+ /**
2
+ * Reads a cookie value by name from `document.cookie`.
3
+ *
4
+ * This is a small internal helper used by the AuthGate browser SDK.
5
+ * It returns `null` if the cookie is not present.
6
+ */
1
7
  function getCookie(name) {
2
8
  const match = document.cookie
3
9
  .split("; ")
4
10
  .find((c) => c.startsWith(name + "="));
5
11
  return match ? decodeURIComponent(match.split("=")[1]) : null;
6
12
  }
13
+ /**
14
+ * Returns the AuthGate CSRF token from the browser cookies.
15
+ *
16
+ * The CSRF token is issued by AuthGate and stored in the `authgate_csrf`
17
+ * cookie. This helper does not validate the token; it only reads it.
18
+ *
19
+ * @returns The CSRF token string, or `null` if the cookie is missing.
20
+ */
7
21
  export function getCSRFToken() {
8
22
  return getCookie("authgate_csrf");
9
23
  }
24
+ /**
25
+ * Logs the user out by calling the AuthGate logout endpoint.
26
+ *
27
+ * This function:
28
+ * - Reads the CSRF token from the browser cookies
29
+ * - Sends a POST request to `/auth/logout`
30
+ * - Optionally redirects the browser on success
31
+ *
32
+ * Redirecting is a side-effect and does not define success. Applications
33
+ * that need to react to logout programmatically (e.g. SPA state updates)
34
+ * should inspect the returned result instead.
35
+ *
36
+ * @param opts.redirectTo Optional URL to redirect to after successful logout.
37
+ * @returns A `LogoutResult` indicating whether logout succeeded or failed.
38
+ */
10
39
  export async function logout(opts) {
11
40
  const csrf = getCSRFToken();
12
- await fetch("/auth/logout", {
13
- method: "POST",
14
- headers: {
15
- "X-CSRF-Token": csrf ?? "",
16
- },
17
- credentials: "include",
18
- });
19
- if (opts?.redirectTo !== undefined) {
41
+ if (!csrf) {
42
+ return { ok: false, reason: "missing_csrf" };
43
+ }
44
+ let res;
45
+ try {
46
+ res = await fetch("/auth/logout", {
47
+ method: "POST",
48
+ headers: {
49
+ "X-CSRF-Token": csrf,
50
+ },
51
+ credentials: "include",
52
+ });
53
+ }
54
+ catch {
55
+ return { ok: false, reason: "request_failed" };
56
+ }
57
+ if (!res.ok) {
58
+ return {
59
+ ok: false,
60
+ reason: res.status === 401 || res.status === 403
61
+ ? "unauthorized"
62
+ : "request_failed",
63
+ };
64
+ }
65
+ if (opts?.redirectTo) {
20
66
  window.location.href = opts.redirectTo;
21
67
  }
68
+ return { ok: true };
22
69
  }
package/package.json CHANGED
@@ -1,6 +1,10 @@
1
1
  {
2
2
  "name": "@authgate/browser",
3
- "version": "0.1.0",
3
+ "repository": {
4
+ "type": "git",
5
+ "url": "https://github.com/alexlup06-authgate/authgate-browser.git"
6
+ },
7
+ "version": "0.2.0",
4
8
  "description": "Browser-side helpers for AuthGate (logout, CSRF forwarding)",
5
9
  "license": "MIT",
6
10
  "type": "module",