@megacorp-ai/auth-react 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/LICENSE +21 -0
- package/README.md +69 -0
- package/dist/index.d.ts +1607 -0
- package/dist/index.js +143 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Megacorp Logistics
|
|
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
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# @megacorp-ai/auth-react
|
|
2
|
+
|
|
3
|
+
React client for [`@megacorp-ai/auth`](https://www.npmjs.com/package/@megacorp-ai/auth): a Better Auth React client preconfigured with the email-OTP and admin plugins, plus a `<SignIn />` form, `useSession`, `useRole`, and `<ImpersonationBanner />`. Talks only to `/api/auth/*` on your origin.
|
|
4
|
+
|
|
5
|
+
Requires React 19+.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @megacorp-ai/auth-react react react-dom
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
// auth.ts
|
|
17
|
+
import { createAuthClient } from "@megacorp-ai/auth-react";
|
|
18
|
+
export const authClient = createAuthClient(); // same origin; pass { baseURL } for a separate API host
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { ImpersonationBanner, SignIn, useRole, useSession } from "@megacorp-ai/auth-react";
|
|
23
|
+
import { authClient } from "./auth";
|
|
24
|
+
|
|
25
|
+
function SignInPage() {
|
|
26
|
+
const { refetch } = useSession(authClient);
|
|
27
|
+
return (
|
|
28
|
+
<SignIn
|
|
29
|
+
client={authClient}
|
|
30
|
+
initialEmail={new URLSearchParams(location.search).get("email") ?? ""}
|
|
31
|
+
onSignedIn={() => refetch().then(() => navigate("/app"))}
|
|
32
|
+
classNames={{ form: "stack", error: "error" }}
|
|
33
|
+
/>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function App() {
|
|
38
|
+
const { user, isPending, signOut } = useSession(authClient);
|
|
39
|
+
const { canInvite } = useRole(authClient);
|
|
40
|
+
if (isPending) return null;
|
|
41
|
+
if (!user) return <SignInPage />;
|
|
42
|
+
return (
|
|
43
|
+
<>
|
|
44
|
+
<ImpersonationBanner client={authClient} className="banner" />
|
|
45
|
+
{canInvite && <InvitePanel />}
|
|
46
|
+
<button onClick={() => signOut()}>Sign out</button>
|
|
47
|
+
</>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## API
|
|
53
|
+
|
|
54
|
+
- `createAuthClient({ baseURL? })` returns the Better Auth client. Use `authClient.emailOtp.*` and `authClient.admin.*` directly for custom UI.
|
|
55
|
+
- `useSession(client)` returns `{ data, user, isPending, error, refetch, signOut }`.
|
|
56
|
+
- `useRole(client)` returns `{ roles, isPending, hasRole(...roles), isMegacorpAdmin, isCustomerAdmin, canInvite }`.
|
|
57
|
+
- `parseRoles(role)` splits the server's role string. Roles are `megacorp_admin`, `customer_admin`, `member`.
|
|
58
|
+
- `<SignIn client initialEmail? onSignedIn? classNames? labels? />` renders the two-step email and code form. Unstyled; pass `classNames` for `root`, `form`, `label`, `input`, `button`, `secondaryButton`, `error`, `hint`, and `labels` to change copy.
|
|
59
|
+
- `<ImpersonationBanner client className? buttonClassName? onStopped? />` shows who is impersonating whom with a stop button. Renders nothing otherwise.
|
|
60
|
+
|
|
61
|
+
## Notes
|
|
62
|
+
|
|
63
|
+
- Cookies carry the session, so the API must be same-origin or behind a proxy that forwards `/api/auth`. In Vite dev, proxy `/api` to your server with `xfwd: true`.
|
|
64
|
+
- Sign-up is disabled server-side. Unknown emails see the same "code sent" screen and receive nothing.
|
|
65
|
+
- No analytics or telemetry in this package.
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT
|