@fragno-dev/auth 0.0.14 → 0.0.16
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 +196 -9
- package/dist/browser/client/react.d.ts +1194 -64
- package/dist/browser/client/react.d.ts.map +1 -1
- package/dist/browser/client/react.js +1 -1
- package/dist/browser/client/react.js.map +1 -1
- package/dist/browser/client/solid.d.ts +1446 -64
- package/dist/browser/client/solid.d.ts.map +1 -1
- package/dist/browser/client/solid.js +1 -1
- package/dist/browser/client/solid.js.map +1 -1
- package/dist/browser/client/svelte.d.ts +1194 -64
- package/dist/browser/client/svelte.d.ts.map +1 -1
- package/dist/browser/client/svelte.js +1 -1
- package/dist/browser/client/svelte.js.map +1 -1
- package/dist/browser/client/vanilla.d.ts +1194 -64
- package/dist/browser/client/vanilla.d.ts.map +1 -1
- package/dist/browser/client/vanilla.js +1 -1
- package/dist/browser/client/vanilla.js.map +1 -1
- package/dist/browser/client/vue.d.ts +1150 -20
- package/dist/browser/client/vue.d.ts.map +1 -1
- package/dist/browser/client/vue.js +1 -1
- package/dist/browser/client/vue.js.map +1 -1
- package/dist/browser/index-m_5zsra2.d.ts +7141 -0
- package/dist/browser/index-m_5zsra2.d.ts.map +1 -0
- package/dist/browser/index.d.ts +2 -600
- package/dist/browser/index.js +2 -2
- package/dist/browser/src-Ck4bl2NH.js +1892 -0
- package/dist/browser/src-Ck4bl2NH.js.map +1 -0
- package/dist/node/index.d.ts +6806 -265
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +5532 -266
- package/dist/node/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +20 -39
- package/dist/browser/index.d.ts.map +0 -1
- package/dist/browser/src-DNrh9CQq.js +0 -184
- package/dist/browser/src-DNrh9CQq.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,16 +1,203 @@
|
|
|
1
|
-
# Fragno Fragment
|
|
1
|
+
# Fragno Auth Fragment
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Auth fragment is a full-stack library: a single package that bundles backend routes, database
|
|
4
|
+
schema, and frontend hooks so you can drop authentication into any TypeScript app without wiring
|
|
5
|
+
everything by hand. It ships with typed routes, hooks, and client helpers.
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
- Email/password sign-up, sign-in, and sign-out
|
|
8
|
+
- Session cookies with configurable security attributes
|
|
9
|
+
- Organizations, roles, invitations, and active organization context
|
|
10
|
+
- OAuth providers (GitHub built-in)
|
|
11
|
+
- Hooks for user/session/org lifecycle events
|
|
12
|
+
|
|
13
|
+
## Install
|
|
6
14
|
|
|
7
15
|
```bash
|
|
8
|
-
npm
|
|
9
|
-
npm run build
|
|
16
|
+
npm install @fragno-dev/auth @fragno-dev/db
|
|
10
17
|
```
|
|
11
18
|
|
|
12
|
-
##
|
|
19
|
+
## Quickstart
|
|
20
|
+
|
|
21
|
+
### 1. Create the fragment server
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createAuthFragment } from "@fragno-dev/auth";
|
|
25
|
+
import { fragmentDbAdapter } from "./db";
|
|
26
|
+
|
|
27
|
+
export const authFragment = createAuthFragment(
|
|
28
|
+
{
|
|
29
|
+
cookieOptions: {
|
|
30
|
+
secure: true,
|
|
31
|
+
sameSite: "Lax",
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
databaseAdapter: fragmentDbAdapter,
|
|
36
|
+
mountRoute: "/api/auth",
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 2. Mount routes (React Router example)
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { authFragment } from "@/lib/auth";
|
|
45
|
+
|
|
46
|
+
export const handlers = authFragment.handlersFor("react-router");
|
|
47
|
+
export const action = handlers.action;
|
|
48
|
+
export const loader = handlers.loader;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 3. Create a client
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { createAuthFragmentClient } from "@fragno-dev/auth/react";
|
|
55
|
+
|
|
56
|
+
export const authClient = createAuthFragmentClient();
|
|
57
|
+
|
|
58
|
+
const { data: me } = authClient.useMe();
|
|
59
|
+
const { mutate: signIn } = authClient.useSignIn();
|
|
60
|
+
const { mutate: signOut } = authClient.useSignOut();
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Other clients:
|
|
64
|
+
|
|
65
|
+
- `@fragno-dev/auth/vanilla`
|
|
66
|
+
- `@fragno-dev/auth/solid`
|
|
67
|
+
- `@fragno-dev/auth/svelte`
|
|
68
|
+
- `@fragno-dev/auth/vue`
|
|
69
|
+
|
|
70
|
+
## Route Surface
|
|
71
|
+
|
|
72
|
+
Auth:
|
|
73
|
+
|
|
74
|
+
- `GET /me`
|
|
75
|
+
- `POST /sign-up`
|
|
76
|
+
- `POST /sign-in`
|
|
77
|
+
- `POST /sign-out`
|
|
78
|
+
- `POST /change-password`
|
|
79
|
+
- `GET /users`
|
|
80
|
+
- `PATCH /users/:userId/role`
|
|
81
|
+
|
|
82
|
+
Organizations (enabled by default):
|
|
83
|
+
|
|
84
|
+
- `POST /organizations`
|
|
85
|
+
- `GET /organizations`
|
|
86
|
+
- `GET /organizations/:organizationId`
|
|
87
|
+
- `PATCH /organizations/:organizationId`
|
|
88
|
+
- `DELETE /organizations/:organizationId`
|
|
89
|
+
- `GET /organizations/active`
|
|
90
|
+
- `POST /organizations/active`
|
|
91
|
+
- `GET /organizations/:organizationId/members`
|
|
92
|
+
- `POST /organizations/:organizationId/members`
|
|
93
|
+
- `PATCH /organizations/:organizationId/members/:memberId`
|
|
94
|
+
- `DELETE /organizations/:organizationId/members/:memberId`
|
|
95
|
+
- `GET /organizations/:organizationId/invitations`
|
|
96
|
+
- `POST /organizations/:organizationId/invitations`
|
|
97
|
+
- `GET /organizations/invitations`
|
|
98
|
+
- `PATCH /organizations/invitations/:invitationId`
|
|
99
|
+
|
|
100
|
+
OAuth:
|
|
101
|
+
|
|
102
|
+
- `GET /oauth/:provider/authorize`
|
|
103
|
+
- `GET /oauth/:provider/callback`
|
|
104
|
+
|
|
105
|
+
## Configuration
|
|
106
|
+
|
|
107
|
+
`createAuthFragment(config, options)` supports:
|
|
108
|
+
|
|
109
|
+
- `cookieOptions`: `httpOnly`, `secure`, `sameSite`, `maxAge`, `path`
|
|
110
|
+
- `hooks`: `onUserCreated`, `onSessionCreated`, `onOrganizationCreated`, and more
|
|
111
|
+
- `organizations`: `false` to disable or an organization config object
|
|
112
|
+
- `emailAndPassword`: `{ enabled?: boolean }` to toggle email/password routes
|
|
113
|
+
- `oauth`: providers and OAuth settings
|
|
114
|
+
|
|
115
|
+
Organization config fields:
|
|
116
|
+
|
|
117
|
+
- `roles`, `creatorRoles`, `defaultMemberRoles`
|
|
118
|
+
- `allowUserToCreateOrganization`, `invitationExpiresInDays`
|
|
119
|
+
- `autoCreateOrganization`, `limits`, `hooks`
|
|
120
|
+
|
|
121
|
+
OAuth config fields:
|
|
122
|
+
|
|
123
|
+
- `providers`: map of `OAuthProvider`
|
|
124
|
+
- `defaultRedirectUri`
|
|
125
|
+
- `stateTtlMs` (default is 10 minutes)
|
|
126
|
+
- `linkByEmail` (default is `true`)
|
|
127
|
+
- `tokenStorage`: `"none"` | `"refresh"` | `"all"`
|
|
128
|
+
|
|
129
|
+
## OAuth
|
|
130
|
+
|
|
131
|
+
OAuth is disabled unless configured. Use the authorize endpoint to get a provider URL, then redirect
|
|
132
|
+
the browser to complete the flow. The callback route sets the session cookie and can optionally
|
|
133
|
+
redirect to a `returnTo` path.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
const { url } = await authClient.oauth.getAuthorizationUrl({
|
|
137
|
+
provider: "github",
|
|
138
|
+
returnTo: "/app",
|
|
139
|
+
});
|
|
140
|
+
window.location.assign(url);
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Notes:
|
|
144
|
+
|
|
145
|
+
- `returnTo` must be a relative path starting with `/` (it is sanitized server-side).
|
|
146
|
+
- `link: true` links the provider to the currently signed-in user (session cookie required).
|
|
147
|
+
- `scope` and `loginHint` are passed through to the provider.
|
|
148
|
+
- You can set `defaultRedirectUri` once or override per provider with `redirectURI`.
|
|
149
|
+
|
|
150
|
+
## GitHub OAuth
|
|
151
|
+
|
|
152
|
+
### Server configuration
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
import { createAuthFragment, github } from "@fragno-dev/auth";
|
|
156
|
+
|
|
157
|
+
export const authFragment = createAuthFragment(
|
|
158
|
+
{
|
|
159
|
+
oauth: {
|
|
160
|
+
defaultRedirectUri: "https://your-app.com/api/auth/oauth/github/callback",
|
|
161
|
+
providers: {
|
|
162
|
+
github: github({
|
|
163
|
+
clientId: process.env.GITHUB_CLIENT_ID!,
|
|
164
|
+
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
|
|
165
|
+
}),
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
{ databaseAdapter, mountRoute: "/api/auth" },
|
|
170
|
+
);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Using GitHub auth in your app
|
|
174
|
+
|
|
175
|
+
1. Create a GitHub OAuth App and set its callback URL to your fragment callback route.
|
|
176
|
+
2. Add a "Continue with GitHub" button that starts the flow.
|
|
177
|
+
3. Let GitHub redirect back to `/api/auth/oauth/github/callback` to set the session cookie and
|
|
178
|
+
redirect the user.
|
|
179
|
+
|
|
180
|
+
Example button:
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
const handleGithubLogin = async () => {
|
|
184
|
+
const { url } = await authClient.oauth.getAuthorizationUrl({
|
|
185
|
+
provider: "github",
|
|
186
|
+
returnTo: "/app",
|
|
187
|
+
});
|
|
188
|
+
window.location.assign(url);
|
|
189
|
+
};
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
If you use a custom SPA callback page, finalize the login by calling the callback hook:
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
const params = new URLSearchParams(window.location.search);
|
|
196
|
+
await authClient.oauth.callback({
|
|
197
|
+
provider: "github",
|
|
198
|
+
code: params.get("code")!,
|
|
199
|
+
state: params.get("state")!,
|
|
200
|
+
});
|
|
201
|
+
```
|
|
13
202
|
|
|
14
|
-
|
|
15
|
-
- Add framework-specific clients in `src/client/`
|
|
16
|
-
- See `AGENTS.md` for detailed development patterns
|
|
203
|
+
This will set the session cookie on the same origin and return the signed-in user info.
|