@shakibdshy/signet-auth-react 0.0.0-stage → 1.0.0-beta.2
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 +21 -0
- package/README.md +216 -2
- package/dist/index.d.ts +19 -0
- package/dist/index.js +82 -0
- package/package.json +43 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 shakibdshy
|
|
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
|
@@ -1,3 +1,217 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Signet React hooks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**React session hooks for [Signet](https://github.com/shakibdshy/signet-auth), an authentication library written in Go.**
|
|
4
|
+
|
|
5
|
+
Signet's auth logic runs in your Go backend. The typed client `@shakibdshy/signet-auth-client`
|
|
6
|
+
calls its HTTP API from a frontend, and this package is the React layer on top of that client:
|
|
7
|
+
`SignetProvider`, `useSession()` and `sessionGuard`. Your components read one shared Session
|
|
8
|
+
state instead of each one calling the Go API on its own.
|
|
9
|
+
|
|
10
|
+
| Part of Signet | Runs in | Written in | Package |
|
|
11
|
+
| --- | --- | --- | --- |
|
|
12
|
+
| The auth logic: passwords, Sessions, OAuth, mail, security checks | Your backend | Go | `github.com/shakibdshy/signet-auth` |
|
|
13
|
+
| Calls that backend from a frontend | Browser, Node.js | TypeScript, JavaScript | `@shakibdshy/signet-auth-client` |
|
|
14
|
+
| **Shares that Session with React components and guards routes** | Browser | TypeScript, React | **`@shakibdshy/signet-auth-react`** (this package) |
|
|
15
|
+
|
|
16
|
+
Current version: **`1.0.0-beta.2`**, a pre-1.0 release, so the API may still change before
|
|
17
|
+
`v1.0.0`. The Go module (`v1.0.0-beta.2`) and both npm packages (`1.0.0-beta.2`) are released
|
|
18
|
+
together at the same version.
|
|
19
|
+
|
|
20
|
+
> **This package does not implement authentication.** It is a thin layer over the typed
|
|
21
|
+
> client, and every request still goes to the Signet handler in your Go backend. To set that
|
|
22
|
+
> backend up, start with the [Signet README for Go](https://github.com/shakibdshy/signet-auth).
|
|
23
|
+
|
|
24
|
+
## Which package do you need?
|
|
25
|
+
|
|
26
|
+
- **Any frontend other than React**, or a React app that manages Session state itself:
|
|
27
|
+
install only
|
|
28
|
+
[`@shakibdshy/signet-auth-client`](https://github.com/shakibdshy/signet-auth/tree/main/client/ts).
|
|
29
|
+
- **React, when you want your components to share one Session state and to guard routes:**
|
|
30
|
+
install this package. It depends on the client at the same version, so npm installs both.
|
|
31
|
+
|
|
32
|
+
## Requirements
|
|
33
|
+
|
|
34
|
+
- **React 18 or newer.** The client needs Node.js 20 or newer.
|
|
35
|
+
- **A backend that runs Signet.**
|
|
36
|
+
- **Same origin.** Signet keeps Sessions in cookies and requires same-origin requests for
|
|
37
|
+
CSRF protection, so a cross-origin fetch cannot use a cookie Session. Serve your app and
|
|
38
|
+
the Go handler from one origin, or route requests through a same-origin proxy.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
npm install @shakibdshy/signet-auth-react@1.0.0-beta.2
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
This also installs `@shakibdshy/signet-auth-client`. Do not install the client separately.
|
|
47
|
+
|
|
48
|
+
To use a change that is not published yet, install both packages from a checkout of the
|
|
49
|
+
repository:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
cd /path/to/signet-auth/client/ts
|
|
53
|
+
npm install
|
|
54
|
+
npm run build
|
|
55
|
+
|
|
56
|
+
cd /path/to/signet-auth/client/react
|
|
57
|
+
npm install
|
|
58
|
+
npm run build
|
|
59
|
+
|
|
60
|
+
cd /path/to/your-frontend
|
|
61
|
+
npm install /path/to/signet-auth/client/ts
|
|
62
|
+
npm install /path/to/signet-auth/client/react
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Quick start
|
|
66
|
+
|
|
67
|
+
### 1. Create the typed client
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
// signet-auth-client.ts
|
|
71
|
+
import { createSignetClient } from "@shakibdshy/signet-auth-client";
|
|
72
|
+
|
|
73
|
+
export const auth = createSignetClient({
|
|
74
|
+
// Your backend's origin, plus the prefix where it mounts Signet.
|
|
75
|
+
baseURL: "https://api.example.com/auth",
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Every method of the client is described in the
|
|
80
|
+
[client README](https://github.com/shakibdshy/signet-auth/blob/main/client/ts/README.md).
|
|
81
|
+
|
|
82
|
+
### 2. Wrap your app in the provider
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
// main.tsx
|
|
86
|
+
import { SignetProvider } from "@shakibdshy/signet-auth-react";
|
|
87
|
+
import { auth } from "./signet-auth-client";
|
|
88
|
+
import { App } from "./App";
|
|
89
|
+
|
|
90
|
+
export function Root() {
|
|
91
|
+
return (
|
|
92
|
+
<SignetProvider client={auth}>
|
|
93
|
+
<App />
|
|
94
|
+
</SignetProvider>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
On mount, the provider makes one `GET /me` request to the Go backend and shares the result
|
|
100
|
+
with every `useSession()` call under it.
|
|
101
|
+
|
|
102
|
+
### 3. Use the Session in a component
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
// Profile.tsx
|
|
106
|
+
import { useState } from "react";
|
|
107
|
+
import { useSession } from "@shakibdshy/signet-auth-react";
|
|
108
|
+
|
|
109
|
+
export function Profile() {
|
|
110
|
+
const { status, user, signIn, signOut } = useSession();
|
|
111
|
+
const [email, setEmail] = useState("");
|
|
112
|
+
const [password, setPassword] = useState("");
|
|
113
|
+
const [formError, setFormError] = useState("");
|
|
114
|
+
|
|
115
|
+
if (status === "loading") return <p>Loading…</p>;
|
|
116
|
+
|
|
117
|
+
if (status === "anonymous") {
|
|
118
|
+
return (
|
|
119
|
+
<form onSubmit={async (event) => {
|
|
120
|
+
event.preventDefault();
|
|
121
|
+
setFormError("");
|
|
122
|
+
try {
|
|
123
|
+
await signIn({ email, password });
|
|
124
|
+
} catch {
|
|
125
|
+
setFormError("Invalid email or password.");
|
|
126
|
+
}
|
|
127
|
+
}}>
|
|
128
|
+
<input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
|
|
129
|
+
<input type="password" value={password} onChange={(e) => setPassword(e.target.value)} />
|
|
130
|
+
<button type="submit">Sign in</button>
|
|
131
|
+
{formError && <p role="alert">{formError}</p>}
|
|
132
|
+
</form>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<>
|
|
138
|
+
<p>Signed in as {user?.email}</p>
|
|
139
|
+
<button onClick={() => signOut()}>Sign out</button>
|
|
140
|
+
</>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
`signIn` and `signOut` update the shared state, so every `useSession()` call re-renders.
|
|
146
|
+
|
|
147
|
+
### 4. Protect a server-rendered route
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
// middleware.ts (Next.js example)
|
|
151
|
+
import { sessionGuard } from "@shakibdshy/signet-auth-react";
|
|
152
|
+
|
|
153
|
+
export async function middleware(request: Request) {
|
|
154
|
+
const ok = await sessionGuard(
|
|
155
|
+
"https://api.example.com/auth",
|
|
156
|
+
request.headers.get("cookie") ?? "",
|
|
157
|
+
);
|
|
158
|
+
if (!ok) return Response.redirect(new URL("/signin", request.url));
|
|
159
|
+
return Response.next();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export const config = { matcher: ["/private/:path*"] };
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`sessionGuard` appends `/me` to the URL you pass, so that URL must include the prefix where
|
|
166
|
+
the backend mounts Signet. It forwards the request's cookies and never creates a Session.
|
|
167
|
+
|
|
168
|
+
## What useSession() returns
|
|
169
|
+
|
|
170
|
+
| Field | What it is |
|
|
171
|
+
| --- | --- |
|
|
172
|
+
| `status` | `"loading"`, `"authenticated"` or `"anonymous"`. It starts as `"loading"` and changes when the first `GET /me` finishes. |
|
|
173
|
+
| `user` | The signed-in User returned by `GET /me`, or `null` — with `email_verified`, `username` and the profile timestamps. |
|
|
174
|
+
| `error` | The `SignetApiError` from the last failed `GET /me`, or `null`. |
|
|
175
|
+
| `signIn(input)` | Calls the client's `signIn`, then reads the Session again. It throws the client's error if sign-in fails. |
|
|
176
|
+
| `signOut()` | Calls the client's `signOut`, then sets `status` to `"anonymous"`. |
|
|
177
|
+
| `refresh()` | Calls `GET /me` again and updates `status`, `user` and `error`. |
|
|
178
|
+
|
|
179
|
+
Branch on `status`, not on `error`. The backend answers `GET /me` with 401 for a visitor who
|
|
180
|
+
is not signed in, so for that visitor `status` is `"anonymous"` and `error` holds that 401.
|
|
181
|
+
|
|
182
|
+
Two limits to know about:
|
|
183
|
+
|
|
184
|
+
- `SignetProvider` makes its request after mount, so server-rendered HTML shows the
|
|
185
|
+
`"loading"` state.
|
|
186
|
+
- `signIn` does not handle a Second factor yet. If the backend asks for one, no Session is
|
|
187
|
+
created and `status` stays `"anonymous"`.
|
|
188
|
+
|
|
189
|
+
## Learn more
|
|
190
|
+
|
|
191
|
+
- [React session hooks guide](https://shakibdshy.github.io/signet-auth/docs/react-hooks): the
|
|
192
|
+
full walkthrough.
|
|
193
|
+
- [Client README](https://github.com/shakibdshy/signet-auth/blob/main/client/ts/README.md) and
|
|
194
|
+
[frontend client guide](https://shakibdshy.github.io/signet-auth/docs/frontend-client): every
|
|
195
|
+
client method and the `baseURL` rules.
|
|
196
|
+
- [Signet README for Go](https://github.com/shakibdshy/signet-auth): features, quickstart and
|
|
197
|
+
backend setup.
|
|
198
|
+
|
|
199
|
+
## Development
|
|
200
|
+
|
|
201
|
+
The package lives in
|
|
202
|
+
[`client/react`](https://github.com/shakibdshy/signet-auth/tree/main/client/react) in the
|
|
203
|
+
Signet repository.
|
|
204
|
+
|
|
205
|
+
```sh
|
|
206
|
+
npm install
|
|
207
|
+
npm run typecheck
|
|
208
|
+
npm run build
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
The dependency on the client is declared as a registry version in `package.json`; the
|
|
212
|
+
lockfile keeps a local `../ts` link so development in this checkout uses the current client
|
|
213
|
+
source.
|
|
214
|
+
|
|
215
|
+
## License
|
|
216
|
+
|
|
217
|
+
[MIT](https://github.com/shakibdshy/signet-auth/blob/main/LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import { SignetApiError, type SignetClient } from "@shakibdshy/signet-auth-client";
|
|
3
|
+
export type SessionStatus = "loading" | "authenticated" | "anonymous";
|
|
4
|
+
export type SessionUser = Awaited<ReturnType<SignetClient["me"]>>;
|
|
5
|
+
export interface SessionState {
|
|
6
|
+
status: SessionStatus;
|
|
7
|
+
user: SessionUser | null;
|
|
8
|
+
error: SignetApiError | null;
|
|
9
|
+
refresh: () => Promise<void>;
|
|
10
|
+
signIn: (input: Parameters<SignetClient["signIn"]>[0]) => Promise<void>;
|
|
11
|
+
signOut: () => Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export declare function SignetProvider({ client, children, }: {
|
|
14
|
+
client: SignetClient;
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
17
|
+
export declare function useSession(): SessionState;
|
|
18
|
+
/** Session check for server middleware (e.g. Next.js), given the request cookies. */
|
|
19
|
+
export declare function sessionGuard(authURL: string, cookieHeader: string): Promise<boolean>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useState, } from "react";
|
|
3
|
+
import { SignetApiError, } from "@shakibdshy/signet-auth-client";
|
|
4
|
+
const SignetContext = createContext(null);
|
|
5
|
+
export function SignetProvider({ client, children, }) {
|
|
6
|
+
const [state, setState] = useState({
|
|
7
|
+
status: "loading",
|
|
8
|
+
user: null,
|
|
9
|
+
error: null,
|
|
10
|
+
});
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
let active = true;
|
|
13
|
+
client
|
|
14
|
+
.me()
|
|
15
|
+
.then((user) => {
|
|
16
|
+
if (!active)
|
|
17
|
+
return;
|
|
18
|
+
setState({
|
|
19
|
+
status: user ? "authenticated" : "anonymous",
|
|
20
|
+
user: user ?? null,
|
|
21
|
+
error: null,
|
|
22
|
+
});
|
|
23
|
+
})
|
|
24
|
+
.catch((error) => {
|
|
25
|
+
if (!active)
|
|
26
|
+
return;
|
|
27
|
+
setState({
|
|
28
|
+
status: "anonymous",
|
|
29
|
+
user: null,
|
|
30
|
+
error: error instanceof SignetApiError ? error : new SignetApiError("internal_error", "session check failed", 0),
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
return () => {
|
|
34
|
+
active = false;
|
|
35
|
+
};
|
|
36
|
+
}, [client]);
|
|
37
|
+
const value = useMemo(() => ({ client, state, setState }), [client, state]);
|
|
38
|
+
return _jsx(SignetContext.Provider, { value: value, children: children });
|
|
39
|
+
}
|
|
40
|
+
export function useSession() {
|
|
41
|
+
const context = useContext(SignetContext);
|
|
42
|
+
if (!context) {
|
|
43
|
+
throw new Error("useSession requires <SignetProvider client={...}>");
|
|
44
|
+
}
|
|
45
|
+
const { client, state, setState } = context;
|
|
46
|
+
const refresh = useCallback(async () => {
|
|
47
|
+
try {
|
|
48
|
+
const user = await client.me();
|
|
49
|
+
setState({
|
|
50
|
+
status: user ? "authenticated" : "anonymous",
|
|
51
|
+
user: user ?? null,
|
|
52
|
+
error: null,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
setState({
|
|
57
|
+
status: "anonymous",
|
|
58
|
+
user: null,
|
|
59
|
+
error: error instanceof SignetApiError
|
|
60
|
+
? error
|
|
61
|
+
: new SignetApiError("internal_error", "session check failed", 0),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}, [client, setState]);
|
|
65
|
+
const signIn = useCallback(async (input) => {
|
|
66
|
+
await client.signIn(input);
|
|
67
|
+
await refresh();
|
|
68
|
+
}, [client, refresh]);
|
|
69
|
+
const signOut = useCallback(async () => {
|
|
70
|
+
await client.signOut({ session_token: "" });
|
|
71
|
+
client.clearCsrfToken();
|
|
72
|
+
setState({ status: "anonymous", user: null, error: null });
|
|
73
|
+
}, [client, setState]);
|
|
74
|
+
return useMemo(() => ({ ...state, refresh, signIn, signOut }), [state, refresh, signIn, signOut]);
|
|
75
|
+
}
|
|
76
|
+
/** Session check for server middleware (e.g. Next.js), given the request cookies. */
|
|
77
|
+
export async function sessionGuard(authURL, cookieHeader) {
|
|
78
|
+
const url = new URL(authURL);
|
|
79
|
+
url.pathname = url.pathname.replace(/\/$/, "") + "/me";
|
|
80
|
+
const response = await fetch(url, { headers: { cookie: cookieHeader } });
|
|
81
|
+
return response.ok;
|
|
82
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shakibdshy/signet-auth-react",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
|
+
"description": "React session hooks (SignetProvider, useSession, sessionGuard) for Signet, an embeddable authentication library for Go",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"hooks",
|
|
8
|
+
"auth",
|
|
9
|
+
"authentication",
|
|
10
|
+
"sessions",
|
|
11
|
+
"signet",
|
|
12
|
+
"go"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": ["dist", "!dist/type-contracts.js", "!dist/type-contracts.d.ts"],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc -p tsconfig.json",
|
|
21
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/shakibdshy/signet-auth.git",
|
|
27
|
+
"directory": "client/react"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://shakibdshy.github.io/signet-auth/docs/react-hooks",
|
|
30
|
+
"bugs": "https://github.com/shakibdshy/signet-auth/issues",
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=20"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": ">=18"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@shakibdshy/signet-auth-client": "1.0.0-beta.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": "5.9.3",
|
|
42
|
+
"react": "19.0.0",
|
|
43
|
+
"@types/react": "19.0.0"
|
|
44
|
+
}
|
|
45
|
+
}
|