@useauthio/react 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.
@@ -0,0 +1,201 @@
1
+ // src/errors.ts
2
+ var AuthioError = class extends Error {
3
+ code;
4
+ status;
5
+ requestId;
6
+ constructor(opts) {
7
+ super(opts.message);
8
+ this.name = "AuthioError";
9
+ this.code = opts.code;
10
+ this.status = opts.status;
11
+ this.requestId = opts.requestId;
12
+ }
13
+ };
14
+
15
+ // src/jwks.ts
16
+ import { createRemoteJWKSet, jwtVerify } from "jose";
17
+ var JwtVerifier = class {
18
+ constructor(apiUrl, issuer, audience) {
19
+ this.apiUrl = apiUrl;
20
+ this.issuer = issuer;
21
+ this.audience = audience;
22
+ this.jwks = createRemoteJWKSet(
23
+ new URL(this.apiUrl.replace(/\/$/, "") + "/v1/auth/.well-known/jwks.json"),
24
+ {
25
+ cooldownDuration: 3e4,
26
+ cacheMaxAge: 6e5
27
+ }
28
+ );
29
+ }
30
+ apiUrl;
31
+ issuer;
32
+ audience;
33
+ jwks;
34
+ async verify(token) {
35
+ const { payload } = await jwtVerify(token, this.jwks, {
36
+ issuer: this.issuer,
37
+ audience: this.audience,
38
+ algorithms: ["EdDSA"]
39
+ });
40
+ if (!payload.sub) {
41
+ throw new Error("authio: token missing sub claim");
42
+ }
43
+ return payload;
44
+ }
45
+ };
46
+
47
+ // src/client.ts
48
+ var DEFAULT_API_URL = "https://api.authio.com";
49
+ var DEFAULT_ISSUER = "https://api.authio.com";
50
+ var DEFAULT_AUDIENCE = "authio";
51
+ var Authio = class {
52
+ constructor(options) {
53
+ this.options = options;
54
+ if (!options.apiKey) {
55
+ throw new Error(
56
+ "Authio: apiKey is required. Pass it directly or set AUTHIO_SECRET_KEY."
57
+ );
58
+ }
59
+ const apiUrl = options.apiUrl ?? DEFAULT_API_URL;
60
+ this.verifier = new JwtVerifier(
61
+ apiUrl,
62
+ options.jwtIssuer ?? DEFAULT_ISSUER,
63
+ options.jwtAudience ?? DEFAULT_AUDIENCE
64
+ );
65
+ this.sessions = new SessionsAPI(this, this.verifier);
66
+ }
67
+ options;
68
+ users = new UsersAPI(this);
69
+ organizations = new OrganizationsAPI(this);
70
+ memberships = new MembershipsAPI(this);
71
+ sessions;
72
+ verifier;
73
+ async request(method, path, body) {
74
+ const url = (this.options.apiUrl ?? DEFAULT_API_URL) + path;
75
+ const fetchFn = this.options.fetch ?? globalThis.fetch;
76
+ const res = await fetchFn(url, {
77
+ method,
78
+ headers: {
79
+ "content-type": "application/json",
80
+ "user-agent": "authio-node/0.1.0",
81
+ authorization: `Bearer ${this.options.apiKey}`
82
+ },
83
+ body: body ? JSON.stringify(body) : void 0
84
+ });
85
+ if (!res.ok) {
86
+ const data = await res.json().catch(() => ({}));
87
+ throw new AuthioError({
88
+ code: data.code ?? "request_failed",
89
+ message: data.message ?? `Request failed with status ${res.status}`,
90
+ status: res.status,
91
+ requestId: data.request_id
92
+ });
93
+ }
94
+ if (res.status === 204) return void 0;
95
+ return await res.json();
96
+ }
97
+ };
98
+ var UsersAPI = class {
99
+ constructor(client) {
100
+ this.client = client;
101
+ }
102
+ client;
103
+ get(userId) {
104
+ return this.client.request("GET", `/v1/users/${userId}`);
105
+ }
106
+ listMemberships(userId) {
107
+ return this.client.request(
108
+ "GET",
109
+ `/v1/users/${userId}/memberships`
110
+ );
111
+ }
112
+ };
113
+ var OrganizationsAPI = class {
114
+ constructor(client) {
115
+ this.client = client;
116
+ }
117
+ client;
118
+ list() {
119
+ return this.client.request("GET", "/v1/organizations");
120
+ }
121
+ create(input) {
122
+ return this.client.request("POST", "/v1/organizations", input);
123
+ }
124
+ get(orgId) {
125
+ return this.client.request("GET", `/v1/organizations/${orgId}`);
126
+ }
127
+ };
128
+ var MembershipsAPI = class {
129
+ constructor(client) {
130
+ this.client = client;
131
+ }
132
+ client;
133
+ listForOrganization(orgId) {
134
+ return this.client.request(
135
+ "GET",
136
+ `/v1/organizations/${orgId}/memberships`
137
+ );
138
+ }
139
+ add(orgId, input) {
140
+ return this.client.request(
141
+ "POST",
142
+ `/v1/organizations/${orgId}/memberships`,
143
+ { user_id: input.userId, role: input.role }
144
+ );
145
+ }
146
+ remove(orgId, membershipId) {
147
+ return this.client.request(
148
+ "DELETE",
149
+ `/v1/organizations/${orgId}/memberships/${membershipId}`
150
+ );
151
+ }
152
+ };
153
+ var SessionsAPI = class {
154
+ constructor(client, verifier) {
155
+ this.client = client;
156
+ this.verifier = verifier;
157
+ }
158
+ client;
159
+ verifier;
160
+ /**
161
+ * Verify an Authio access token (JWT). Returns the typed Session, or
162
+ * null when the token is invalid/expired.
163
+ *
164
+ * `session.userId` is always set; `session.orgId` may be null when the
165
+ * user has authenticated but not yet selected an organization (multi-org
166
+ * users coming straight out of /v1/auth/passkey/login/verify).
167
+ */
168
+ async verify(accessToken) {
169
+ if (!accessToken) return null;
170
+ try {
171
+ const claims = await this.verifier.verify(accessToken);
172
+ return {
173
+ sessionId: claims.sid ?? "",
174
+ userId: claims.sub,
175
+ orgId: claims.act_org ? claims.act_org : null,
176
+ role: claims.act_role ? claims.act_role : null,
177
+ expiresAt: claims.exp ? new Date(claims.exp * 1e3).toISOString() : (/* @__PURE__ */ new Date()).toISOString()
178
+ };
179
+ } catch {
180
+ return null;
181
+ }
182
+ }
183
+ /** Pivot a session into a different organization without re-authentication. */
184
+ switchOrg(_sessionId, input) {
185
+ return this.client.request(
186
+ "POST",
187
+ "/v1/sessions/switch-org",
188
+ { organization_id: input.organizationId }
189
+ );
190
+ }
191
+ revoke(sessionId) {
192
+ return this.client.request("POST", "/v1/sessions/revoke", {
193
+ session_id: sessionId
194
+ });
195
+ }
196
+ };
197
+ export {
198
+ Authio,
199
+ AuthioError,
200
+ JwtVerifier
201
+ };
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@useauthio/node",
3
+ "version": "0.1.0",
4
+ "description": "Authio server-side SDK for Node.js, Bun, Deno, and edge runtimes. Vendored from authio-com/authio_node so @useauthio/react can resolve it locally before the npm scope is claimed.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/authio-com/authio_node.git"
9
+ },
10
+ "type": "module",
11
+ "main": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md",
23
+ "LICENSE"
24
+ ],
25
+ "dependencies": {
26
+ "jose": "^5.9.0"
27
+ },
28
+ "engines": {
29
+ "node": ">=20"
30
+ }
31
+ }