@auth-gate/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/dist/index.cjs +104 -0
- package/dist/index.d.cts +122 -0
- package/dist/index.d.ts +122 -0
- package/dist/index.mjs +80 -0
- package/dist/server.cjs +348 -0
- package/dist/server.d.cts +316 -0
- package/dist/server.d.ts +316 -0
- package/dist/server.mjs +326 -0
- package/package.json +41 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
AuthGuard: () => AuthGuard,
|
|
24
|
+
AuthProvider: () => AuthProvider,
|
|
25
|
+
useAuth: () => useAuth,
|
|
26
|
+
useSession: () => useSession
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/provider.tsx
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
33
|
+
var AuthContext = (0, import_react.createContext)(null);
|
|
34
|
+
function AuthProvider({
|
|
35
|
+
children,
|
|
36
|
+
meEndpoint = "/api/auth/me",
|
|
37
|
+
logoutEndpoint = "/api/auth/logout"
|
|
38
|
+
}) {
|
|
39
|
+
const [user, setUser] = (0, import_react.useState)(null);
|
|
40
|
+
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
41
|
+
const refresh = (0, import_react.useCallback)(async () => {
|
|
42
|
+
var _a;
|
|
43
|
+
try {
|
|
44
|
+
const res = await fetch(meEndpoint);
|
|
45
|
+
const data = await res.json();
|
|
46
|
+
setUser((_a = data.user) != null ? _a : null);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
setUser(null);
|
|
49
|
+
} finally {
|
|
50
|
+
setLoading(false);
|
|
51
|
+
}
|
|
52
|
+
}, [meEndpoint]);
|
|
53
|
+
const logout = (0, import_react.useCallback)(async () => {
|
|
54
|
+
await fetch(logoutEndpoint, { method: "POST" });
|
|
55
|
+
setUser(null);
|
|
56
|
+
}, [logoutEndpoint]);
|
|
57
|
+
(0, import_react.useEffect)(() => {
|
|
58
|
+
refresh();
|
|
59
|
+
}, [refresh]);
|
|
60
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext, { value: { user, loading, refresh, logout }, children });
|
|
61
|
+
}
|
|
62
|
+
function useAuthContext() {
|
|
63
|
+
const context = (0, import_react.useContext)(AuthContext);
|
|
64
|
+
if (!context) {
|
|
65
|
+
throw new Error("useAuth must be used within an AuthProvider");
|
|
66
|
+
}
|
|
67
|
+
return context;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/hooks.ts
|
|
71
|
+
function useAuth() {
|
|
72
|
+
return useAuthContext();
|
|
73
|
+
}
|
|
74
|
+
function useSession() {
|
|
75
|
+
const { user } = useAuthContext();
|
|
76
|
+
return user;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/guard.tsx
|
|
80
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
81
|
+
function AuthGuard({
|
|
82
|
+
children,
|
|
83
|
+
loadingFallback,
|
|
84
|
+
redirectTo = "/login"
|
|
85
|
+
}) {
|
|
86
|
+
const { user, loading } = useAuthContext();
|
|
87
|
+
if (loading) {
|
|
88
|
+
return loadingFallback != null ? loadingFallback : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { display: "flex", minHeight: "100vh", alignItems: "center", justifyContent: "center" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { children: "Loading..." }) });
|
|
89
|
+
}
|
|
90
|
+
if (!user) {
|
|
91
|
+
if (typeof window !== "undefined") {
|
|
92
|
+
window.location.href = redirectTo;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
|
|
97
|
+
}
|
|
98
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
99
|
+
0 && (module.exports = {
|
|
100
|
+
AuthGuard,
|
|
101
|
+
AuthProvider,
|
|
102
|
+
useAuth,
|
|
103
|
+
useSession
|
|
104
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as react from 'react';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
import { AuthGateUser } from '@auth-gate/core';
|
|
5
|
+
export { AuthGateUser, OAuthProvider } from '@auth-gate/core';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Shape of the auth context available via {@link useAuth}.
|
|
9
|
+
*/
|
|
10
|
+
interface AuthContextValue {
|
|
11
|
+
/** The authenticated user, or `null` if not signed in. */
|
|
12
|
+
user: AuthGateUser | null;
|
|
13
|
+
/** `true` while the initial session check is in progress. */
|
|
14
|
+
loading: boolean;
|
|
15
|
+
/** Re-fetch the session from the `/me` endpoint. */
|
|
16
|
+
refresh: () => Promise<void>;
|
|
17
|
+
/** Sign the user out by calling the logout endpoint and clearing local state. */
|
|
18
|
+
logout: () => Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Props for {@link AuthProvider}.
|
|
22
|
+
*/
|
|
23
|
+
interface AuthProviderProps {
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
/**
|
|
26
|
+
* URL of the endpoint that returns the current user (e.g. `{ user: { ... } }`).
|
|
27
|
+
* @defaultValue `"/api/auth/me"`
|
|
28
|
+
*/
|
|
29
|
+
meEndpoint?: string;
|
|
30
|
+
/**
|
|
31
|
+
* URL of the endpoint that logs the user out (called with `POST`).
|
|
32
|
+
* @defaultValue `"/api/auth/logout"`
|
|
33
|
+
*/
|
|
34
|
+
logoutEndpoint?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Provides auth state to the component tree.
|
|
38
|
+
*
|
|
39
|
+
* Fetches the current session on mount by calling the `meEndpoint`.
|
|
40
|
+
* All children can access `user`, `loading`, `refresh()`, and `logout()`
|
|
41
|
+
* via the {@link useAuth} hook.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```tsx
|
|
45
|
+
* <AuthProvider meEndpoint="/api/auth/me" logoutEndpoint="/api/auth/logout">
|
|
46
|
+
* <App />
|
|
47
|
+
* </AuthProvider>
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function AuthProvider({ children, meEndpoint, logoutEndpoint, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Access the full auth context: `user`, `loading`, `refresh()`, and `logout()`.
|
|
54
|
+
*
|
|
55
|
+
* Must be called inside an {@link AuthProvider}.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```tsx
|
|
59
|
+
* function Header() {
|
|
60
|
+
* const { user, loading, logout } = useAuth();
|
|
61
|
+
*
|
|
62
|
+
* if (loading) return <Spinner />;
|
|
63
|
+
* if (!user) return <a href="/login">Sign in</a>;
|
|
64
|
+
*
|
|
65
|
+
* return (
|
|
66
|
+
* <div>
|
|
67
|
+
* {user.name}
|
|
68
|
+
* <button onClick={logout}>Logout</button>
|
|
69
|
+
* </div>
|
|
70
|
+
* );
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare function useAuth(): AuthContextValue;
|
|
75
|
+
/**
|
|
76
|
+
* Shorthand hook that returns just the current user (or `null`).
|
|
77
|
+
*
|
|
78
|
+
* Equivalent to `useAuth().user`.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```tsx
|
|
82
|
+
* function Profile() {
|
|
83
|
+
* const user = useSession();
|
|
84
|
+
* if (!user) return null;
|
|
85
|
+
* return <p>Hello, {user.name}</p>;
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
declare function useSession(): AuthGateUser | null;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Props for {@link AuthGuard}.
|
|
93
|
+
*/
|
|
94
|
+
interface AuthGuardProps {
|
|
95
|
+
children: ReactNode;
|
|
96
|
+
/**
|
|
97
|
+
* Content to show while the session is being loaded.
|
|
98
|
+
* Defaults to a centered "Loading..." message.
|
|
99
|
+
*/
|
|
100
|
+
loadingFallback?: ReactNode;
|
|
101
|
+
/**
|
|
102
|
+
* URL to redirect to when the user is not authenticated.
|
|
103
|
+
* @defaultValue `"/login"`
|
|
104
|
+
*/
|
|
105
|
+
redirectTo?: string;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Client-side route guard that redirects unauthenticated users.
|
|
109
|
+
*
|
|
110
|
+
* Renders `loadingFallback` while the session is loading, redirects to
|
|
111
|
+
* `redirectTo` if no user is found, and renders `children` when authenticated.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```tsx
|
|
115
|
+
* <AuthGuard redirectTo="/login" loadingFallback={<Spinner />}>
|
|
116
|
+
* <ProtectedContent />
|
|
117
|
+
* </AuthGuard>
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
declare function AuthGuard({ children, loadingFallback, redirectTo, }: AuthGuardProps): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | react.ReactPortal | react.ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | react_jsx_runtime.JSX.Element | null;
|
|
121
|
+
|
|
122
|
+
export { AuthGuard, type AuthGuardProps, AuthProvider, type AuthProviderProps, useAuth, useSession };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as react from 'react';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
import { AuthGateUser } from '@auth-gate/core';
|
|
5
|
+
export { AuthGateUser, OAuthProvider } from '@auth-gate/core';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Shape of the auth context available via {@link useAuth}.
|
|
9
|
+
*/
|
|
10
|
+
interface AuthContextValue {
|
|
11
|
+
/** The authenticated user, or `null` if not signed in. */
|
|
12
|
+
user: AuthGateUser | null;
|
|
13
|
+
/** `true` while the initial session check is in progress. */
|
|
14
|
+
loading: boolean;
|
|
15
|
+
/** Re-fetch the session from the `/me` endpoint. */
|
|
16
|
+
refresh: () => Promise<void>;
|
|
17
|
+
/** Sign the user out by calling the logout endpoint and clearing local state. */
|
|
18
|
+
logout: () => Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Props for {@link AuthProvider}.
|
|
22
|
+
*/
|
|
23
|
+
interface AuthProviderProps {
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
/**
|
|
26
|
+
* URL of the endpoint that returns the current user (e.g. `{ user: { ... } }`).
|
|
27
|
+
* @defaultValue `"/api/auth/me"`
|
|
28
|
+
*/
|
|
29
|
+
meEndpoint?: string;
|
|
30
|
+
/**
|
|
31
|
+
* URL of the endpoint that logs the user out (called with `POST`).
|
|
32
|
+
* @defaultValue `"/api/auth/logout"`
|
|
33
|
+
*/
|
|
34
|
+
logoutEndpoint?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Provides auth state to the component tree.
|
|
38
|
+
*
|
|
39
|
+
* Fetches the current session on mount by calling the `meEndpoint`.
|
|
40
|
+
* All children can access `user`, `loading`, `refresh()`, and `logout()`
|
|
41
|
+
* via the {@link useAuth} hook.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```tsx
|
|
45
|
+
* <AuthProvider meEndpoint="/api/auth/me" logoutEndpoint="/api/auth/logout">
|
|
46
|
+
* <App />
|
|
47
|
+
* </AuthProvider>
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function AuthProvider({ children, meEndpoint, logoutEndpoint, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Access the full auth context: `user`, `loading`, `refresh()`, and `logout()`.
|
|
54
|
+
*
|
|
55
|
+
* Must be called inside an {@link AuthProvider}.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```tsx
|
|
59
|
+
* function Header() {
|
|
60
|
+
* const { user, loading, logout } = useAuth();
|
|
61
|
+
*
|
|
62
|
+
* if (loading) return <Spinner />;
|
|
63
|
+
* if (!user) return <a href="/login">Sign in</a>;
|
|
64
|
+
*
|
|
65
|
+
* return (
|
|
66
|
+
* <div>
|
|
67
|
+
* {user.name}
|
|
68
|
+
* <button onClick={logout}>Logout</button>
|
|
69
|
+
* </div>
|
|
70
|
+
* );
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare function useAuth(): AuthContextValue;
|
|
75
|
+
/**
|
|
76
|
+
* Shorthand hook that returns just the current user (or `null`).
|
|
77
|
+
*
|
|
78
|
+
* Equivalent to `useAuth().user`.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```tsx
|
|
82
|
+
* function Profile() {
|
|
83
|
+
* const user = useSession();
|
|
84
|
+
* if (!user) return null;
|
|
85
|
+
* return <p>Hello, {user.name}</p>;
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
declare function useSession(): AuthGateUser | null;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Props for {@link AuthGuard}.
|
|
93
|
+
*/
|
|
94
|
+
interface AuthGuardProps {
|
|
95
|
+
children: ReactNode;
|
|
96
|
+
/**
|
|
97
|
+
* Content to show while the session is being loaded.
|
|
98
|
+
* Defaults to a centered "Loading..." message.
|
|
99
|
+
*/
|
|
100
|
+
loadingFallback?: ReactNode;
|
|
101
|
+
/**
|
|
102
|
+
* URL to redirect to when the user is not authenticated.
|
|
103
|
+
* @defaultValue `"/login"`
|
|
104
|
+
*/
|
|
105
|
+
redirectTo?: string;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Client-side route guard that redirects unauthenticated users.
|
|
109
|
+
*
|
|
110
|
+
* Renders `loadingFallback` while the session is loading, redirects to
|
|
111
|
+
* `redirectTo` if no user is found, and renders `children` when authenticated.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```tsx
|
|
115
|
+
* <AuthGuard redirectTo="/login" loadingFallback={<Spinner />}>
|
|
116
|
+
* <ProtectedContent />
|
|
117
|
+
* </AuthGuard>
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
declare function AuthGuard({ children, loadingFallback, redirectTo, }: AuthGuardProps): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | react.ReactPortal | react.ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | react_jsx_runtime.JSX.Element | null;
|
|
121
|
+
|
|
122
|
+
export { AuthGuard, type AuthGuardProps, AuthProvider, type AuthProviderProps, useAuth, useSession };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// src/provider.tsx
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
useContext,
|
|
5
|
+
useState,
|
|
6
|
+
useEffect,
|
|
7
|
+
useCallback
|
|
8
|
+
} from "react";
|
|
9
|
+
import { jsx } from "react/jsx-runtime";
|
|
10
|
+
var AuthContext = createContext(null);
|
|
11
|
+
function AuthProvider({
|
|
12
|
+
children,
|
|
13
|
+
meEndpoint = "/api/auth/me",
|
|
14
|
+
logoutEndpoint = "/api/auth/logout"
|
|
15
|
+
}) {
|
|
16
|
+
const [user, setUser] = useState(null);
|
|
17
|
+
const [loading, setLoading] = useState(true);
|
|
18
|
+
const refresh = useCallback(async () => {
|
|
19
|
+
var _a;
|
|
20
|
+
try {
|
|
21
|
+
const res = await fetch(meEndpoint);
|
|
22
|
+
const data = await res.json();
|
|
23
|
+
setUser((_a = data.user) != null ? _a : null);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
setUser(null);
|
|
26
|
+
} finally {
|
|
27
|
+
setLoading(false);
|
|
28
|
+
}
|
|
29
|
+
}, [meEndpoint]);
|
|
30
|
+
const logout = useCallback(async () => {
|
|
31
|
+
await fetch(logoutEndpoint, { method: "POST" });
|
|
32
|
+
setUser(null);
|
|
33
|
+
}, [logoutEndpoint]);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
refresh();
|
|
36
|
+
}, [refresh]);
|
|
37
|
+
return /* @__PURE__ */ jsx(AuthContext, { value: { user, loading, refresh, logout }, children });
|
|
38
|
+
}
|
|
39
|
+
function useAuthContext() {
|
|
40
|
+
const context = useContext(AuthContext);
|
|
41
|
+
if (!context) {
|
|
42
|
+
throw new Error("useAuth must be used within an AuthProvider");
|
|
43
|
+
}
|
|
44
|
+
return context;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/hooks.ts
|
|
48
|
+
function useAuth() {
|
|
49
|
+
return useAuthContext();
|
|
50
|
+
}
|
|
51
|
+
function useSession() {
|
|
52
|
+
const { user } = useAuthContext();
|
|
53
|
+
return user;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/guard.tsx
|
|
57
|
+
import { Fragment, jsx as jsx2 } from "react/jsx-runtime";
|
|
58
|
+
function AuthGuard({
|
|
59
|
+
children,
|
|
60
|
+
loadingFallback,
|
|
61
|
+
redirectTo = "/login"
|
|
62
|
+
}) {
|
|
63
|
+
const { user, loading } = useAuthContext();
|
|
64
|
+
if (loading) {
|
|
65
|
+
return loadingFallback != null ? loadingFallback : /* @__PURE__ */ jsx2("div", { style: { display: "flex", minHeight: "100vh", alignItems: "center", justifyContent: "center" }, children: /* @__PURE__ */ jsx2("p", { children: "Loading..." }) });
|
|
66
|
+
}
|
|
67
|
+
if (!user) {
|
|
68
|
+
if (typeof window !== "undefined") {
|
|
69
|
+
window.location.href = redirectTo;
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return /* @__PURE__ */ jsx2(Fragment, { children });
|
|
74
|
+
}
|
|
75
|
+
export {
|
|
76
|
+
AuthGuard,
|
|
77
|
+
AuthProvider,
|
|
78
|
+
useAuth,
|
|
79
|
+
useSession
|
|
80
|
+
};
|