@authgate/browser 0.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/README.md +101 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +22 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# @authgate/browser
|
|
2
|
+
|
|
3
|
+
Minimal browser-side helpers for applications using **AuthGate**.
|
|
4
|
+
|
|
5
|
+
This package provides small, explicit utilities to integrate browser-based UIs
|
|
6
|
+
with an AuthGate-backed authentication flow. It intentionally avoids framework
|
|
7
|
+
coupling and hidden behavior.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- Read AuthGate CSRF token from cookies
|
|
14
|
+
- Perform a safe logout request with CSRF protection
|
|
15
|
+
- Zero dependencies
|
|
16
|
+
- Framework-agnostic (works with React, Vue, vanilla JS, etc.)
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @authgate/browser
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Read CSRF token
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { getCSRFToken } from "@authgate/browser";
|
|
34
|
+
|
|
35
|
+
const csrf = getCSRFToken();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Returns the value of the `authgate_csrf` cookie, or `null` if not present.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
### Logout
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { logout } from "@authgate/browser";
|
|
46
|
+
|
|
47
|
+
await logout();
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
This will:
|
|
51
|
+
|
|
52
|
+
- Send a `POST /auth/logout` request
|
|
53
|
+
- Attach the CSRF token via `X-CSRF-Token`
|
|
54
|
+
- Include credentials (`cookies`)
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
### Logout with redirect
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
await logout({ redirectTo: "/" });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
After a successful logout request, the browser is redirected to the given path.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Security Model
|
|
69
|
+
|
|
70
|
+
- CSRF tokens are **not generated** by this package
|
|
71
|
+
- CSRF validation is **enforced by AuthGate**
|
|
72
|
+
- This package only forwards existing CSRF state
|
|
73
|
+
|
|
74
|
+
No cookies are set, modified, or cleared by this library.
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## What This Package Does NOT Do
|
|
79
|
+
|
|
80
|
+
- No authentication logic
|
|
81
|
+
- No token refresh
|
|
82
|
+
- No session management
|
|
83
|
+
- No redirects except when explicitly requested
|
|
84
|
+
- No framework-specific helpers
|
|
85
|
+
|
|
86
|
+
This package exists solely to reduce boilerplate and prevent integration mistakes.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Compatibility
|
|
91
|
+
|
|
92
|
+
- Works with any backend protected by AuthGate
|
|
93
|
+
- Compatible with SSR and SPA architectures
|
|
94
|
+
- Safe to use in multi-app or monorepo setups
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT
|
|
101
|
+
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
function getCookie(name) {
|
|
2
|
+
const match = document.cookie
|
|
3
|
+
.split("; ")
|
|
4
|
+
.find((c) => c.startsWith(name + "="));
|
|
5
|
+
return match ? decodeURIComponent(match.split("=")[1]) : null;
|
|
6
|
+
}
|
|
7
|
+
export function getCSRFToken() {
|
|
8
|
+
return getCookie("authgate_csrf");
|
|
9
|
+
}
|
|
10
|
+
export async function logout(opts) {
|
|
11
|
+
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) {
|
|
20
|
+
window.location.href = opts.redirectTo;
|
|
21
|
+
}
|
|
22
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@authgate/browser",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Browser-side helpers for AuthGate (logout, CSRF forwarding)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/index.js",
|
|
15
|
+
"dist/index.d.ts",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"format": "prettier --write \"src/**/*.{ts,tsx,js}\"",
|
|
21
|
+
"check:format": "prettier --check \"src/**/*.{ts,tsx,js}\"",
|
|
22
|
+
"lint": "eslint \"src/**/*.{ts,tsx,js}\"",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"prepublishOnly": "npm run build"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@eslint/js": "^9.39.2",
|
|
29
|
+
"@typescript-eslint/eslint-plugin": "^8.54.0",
|
|
30
|
+
"@typescript-eslint/parser": "^8.54.0",
|
|
31
|
+
"eslint": "^9.39.2",
|
|
32
|
+
"happy-dom": "^20.4.0",
|
|
33
|
+
"prettier": "^3.8.1",
|
|
34
|
+
"typescript": "^5.9.3",
|
|
35
|
+
"typescript-eslint": "^8.54.0",
|
|
36
|
+
"vitest": "^4.0.18"
|
|
37
|
+
}
|
|
38
|
+
}
|