naystack 1.5.8 → 1.5.10
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 +646 -91
- package/dist/auth/constants.d.mts +4 -0
- package/dist/auth/constants.d.ts +4 -0
- package/dist/auth/email/client.d.mts +149 -0
- package/dist/auth/email/client.d.ts +149 -0
- package/dist/auth/email/index.cjs.js +2 -14
- package/dist/auth/email/index.d.mts +41 -1
- package/dist/auth/email/index.d.ts +41 -1
- package/dist/auth/email/index.esm.js +1 -12
- package/dist/auth/email/routes/delete.cjs.js +0 -1
- package/dist/auth/email/routes/delete.d.mts +5 -0
- package/dist/auth/email/routes/delete.d.ts +5 -0
- package/dist/auth/email/routes/delete.esm.js +0 -1
- package/dist/auth/email/routes/get.d.mts +5 -0
- package/dist/auth/email/routes/get.d.ts +5 -0
- package/dist/auth/email/routes/post.cjs.js +0 -1
- package/dist/auth/email/routes/post.d.mts +5 -0
- package/dist/auth/email/routes/post.d.ts +5 -0
- package/dist/auth/email/routes/post.esm.js +0 -1
- package/dist/auth/email/routes/put.cjs.js +0 -1
- package/dist/auth/email/routes/put.d.mts +5 -0
- package/dist/auth/email/routes/put.d.ts +5 -0
- package/dist/auth/email/routes/put.esm.js +0 -1
- package/dist/auth/email/token.d.mts +62 -0
- package/dist/auth/email/token.d.ts +62 -0
- package/dist/auth/email/types.d.mts +22 -0
- package/dist/auth/email/types.d.ts +22 -0
- package/dist/auth/email/utils.cjs.js +0 -12
- package/dist/auth/email/utils.d.mts +41 -2
- package/dist/auth/email/utils.d.ts +41 -2
- package/dist/auth/email/utils.esm.js +0 -11
- package/dist/auth/google/get.d.mts +5 -0
- package/dist/auth/google/get.d.ts +5 -0
- package/dist/auth/google/index.d.mts +39 -0
- package/dist/auth/google/index.d.ts +39 -0
- package/dist/auth/index.cjs.js +4 -16
- package/dist/auth/index.d.mts +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.esm.js +3 -14
- package/dist/auth/instagram/client.d.mts +19 -0
- package/dist/auth/instagram/client.d.ts +19 -0
- package/dist/auth/instagram/index.d.mts +37 -0
- package/dist/auth/instagram/index.d.ts +37 -0
- package/dist/auth/instagram/route.d.mts +5 -0
- package/dist/auth/instagram/route.d.ts +5 -0
- package/dist/auth/instagram/utils.d.mts +13 -0
- package/dist/auth/instagram/utils.d.ts +13 -0
- package/dist/auth/types.d.mts +24 -0
- package/dist/auth/types.d.ts +24 -0
- package/dist/auth/utils/errors.d.mts +10 -0
- package/dist/auth/utils/errors.d.ts +10 -0
- package/dist/auth/utils/token.d.mts +20 -0
- package/dist/auth/utils/token.d.ts +20 -0
- package/dist/client/hooks.d.mts +59 -0
- package/dist/client/hooks.d.ts +59 -0
- package/dist/client/seo.d.mts +46 -0
- package/dist/client/seo.d.ts +46 -0
- package/dist/env.d.mts +61 -0
- package/dist/env.d.ts +61 -0
- package/dist/file/client.d.mts +53 -1
- package/dist/file/client.d.ts +53 -1
- package/dist/file/index.cjs.js +0 -1
- package/dist/file/index.esm.js +0 -1
- package/dist/file/put.cjs.js +0 -1
- package/dist/file/put.d.mts +11 -0
- package/dist/file/put.d.ts +11 -0
- package/dist/file/put.esm.js +0 -1
- package/dist/file/setup.cjs.js +0 -1
- package/dist/file/setup.d.mts +48 -0
- package/dist/file/setup.d.ts +48 -0
- package/dist/file/setup.esm.js +0 -1
- package/dist/file/utils.d.mts +41 -0
- package/dist/file/utils.d.ts +41 -0
- package/dist/graphql/client.d.mts +113 -0
- package/dist/graphql/client.d.ts +113 -0
- package/dist/graphql/errors.d.mts +26 -0
- package/dist/graphql/errors.d.ts +26 -0
- package/dist/graphql/index.cjs.js +2 -3
- package/dist/graphql/index.esm.js +2 -3
- package/dist/graphql/init.cjs.js +0 -1
- package/dist/graphql/init.d.mts +33 -0
- package/dist/graphql/init.d.ts +33 -0
- package/dist/graphql/init.esm.js +0 -1
- package/dist/graphql/server.d.mts +88 -0
- package/dist/graphql/server.d.ts +88 -0
- package/dist/graphql/types.d.mts +21 -0
- package/dist/graphql/types.d.ts +21 -0
- package/dist/graphql/utils.d.mts +217 -0
- package/dist/graphql/utils.d.ts +217 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/socials/instagram/getters.d.mts +115 -0
- package/dist/socials/instagram/getters.d.ts +115 -0
- package/dist/socials/instagram/setters.d.mts +18 -0
- package/dist/socials/instagram/setters.d.ts +18 -0
- package/dist/socials/instagram/types.d.mts +46 -0
- package/dist/socials/instagram/types.d.ts +46 -0
- package/dist/socials/instagram/utils.d.mts +19 -0
- package/dist/socials/instagram/utils.d.ts +19 -0
- package/dist/socials/instagram/webhook.d.mts +31 -0
- package/dist/socials/instagram/webhook.d.ts +31 -0
- package/dist/socials/meta-webhook.d.mts +11 -0
- package/dist/socials/meta-webhook.d.ts +11 -0
- package/dist/socials/threads/getters.d.mts +57 -0
- package/dist/socials/threads/getters.d.ts +57 -0
- package/dist/socials/threads/setters.d.mts +59 -0
- package/dist/socials/threads/setters.d.ts +59 -0
- package/dist/socials/threads/types.d.mts +9 -0
- package/dist/socials/threads/types.d.ts +9 -0
- package/dist/socials/threads/utils.d.mts +19 -0
- package/dist/socials/threads/utils.d.ts +19 -0
- package/dist/socials/threads/webhook.d.mts +30 -0
- package/dist/socials/threads/webhook.d.ts +30 -0
- package/package.json +4 -2
package/dist/auth/constants.d.ts
CHANGED
|
@@ -1,16 +1,165 @@
|
|
|
1
1
|
import React__default, { Dispatch, SetStateAction } from 'react';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* React context holding the current access token and setter; used by useToken/useSetToken and auth hooks.
|
|
5
|
+
* @category Auth
|
|
6
|
+
*/
|
|
3
7
|
declare const TokenContext: React__default.Context<{
|
|
4
8
|
token: string | null;
|
|
5
9
|
setToken: Dispatch<SetStateAction<string | null>>;
|
|
6
10
|
}>;
|
|
11
|
+
/**
|
|
12
|
+
* Provider that fetches the current access token from your auth endpoint and exposes it via TokenContext.
|
|
13
|
+
* Wrap your app (or the part that needs auth) so that `useToken()`, `useLogin()`, `useSignUp()`, `useLogout()` work.
|
|
14
|
+
* On mount it GETs `NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT` with credentials; the response's `accessToken` is stored and provided to children.
|
|
15
|
+
*
|
|
16
|
+
* Must be placed **above** `ApolloWrapper` in the component tree (since `ApolloWrapper` needs the token).
|
|
17
|
+
*
|
|
18
|
+
* @param children - React children (e.g. your app or a layout).
|
|
19
|
+
* @returns TokenContext.Provider wrapping children.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* // app/layout.tsx
|
|
24
|
+
* import { AuthWrapper } from "naystack/auth/email/client";
|
|
25
|
+
* import { ApolloWrapper } from "naystack/graphql/client";
|
|
26
|
+
*
|
|
27
|
+
* export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
28
|
+
* return (
|
|
29
|
+
* <html lang="en">
|
|
30
|
+
* <body>
|
|
31
|
+
* <AuthWrapper>
|
|
32
|
+
* <ApolloWrapper>{children}</ApolloWrapper>
|
|
33
|
+
* </AuthWrapper>
|
|
34
|
+
* </body>
|
|
35
|
+
* </html>
|
|
36
|
+
* );
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @category Auth
|
|
41
|
+
*/
|
|
7
42
|
declare const AuthWrapper: ({ children }: {
|
|
8
43
|
children: React__default.ReactNode;
|
|
9
44
|
}) => React__default.JSX.Element;
|
|
45
|
+
/**
|
|
46
|
+
* Returns the current JWT access token from TokenContext. Must be used inside `AuthWrapper`.
|
|
47
|
+
*
|
|
48
|
+
* Common uses: conditional rendering based on auth state, passing to custom fetch headers.
|
|
49
|
+
*
|
|
50
|
+
* @returns The access token string, or `null` if not yet loaded or user is logged out.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```tsx
|
|
54
|
+
* import { useToken } from "naystack/auth/email/client";
|
|
55
|
+
*
|
|
56
|
+
* function Navbar() {
|
|
57
|
+
* const token = useToken();
|
|
58
|
+
* return <Link href={token ? "/dashboard" : "/login"}>{token ? "Dashboard" : "Log in"}</Link>;
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example Using the token for custom API calls:
|
|
63
|
+
* ```tsx
|
|
64
|
+
* const token = useToken();
|
|
65
|
+
* const response = await fetch("/api/custom", {
|
|
66
|
+
* headers: { Authorization: `Bearer ${token}` },
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @category Auth
|
|
71
|
+
*/
|
|
10
72
|
declare function useToken(): string | null;
|
|
73
|
+
/**
|
|
74
|
+
* Returns the setter for the access token in TokenContext. Use to update token after login/signup or clear it on logout.
|
|
75
|
+
* Must be used inside `AuthWrapper`. Typically you won't need this directly — use `useLogin`, `useSignUp`, and `useLogout` instead.
|
|
76
|
+
*
|
|
77
|
+
* @returns `Dispatch<SetStateAction<string | null>>` — call with a string to set the token, or `null` to clear.
|
|
78
|
+
*
|
|
79
|
+
* @category Auth
|
|
80
|
+
*/
|
|
11
81
|
declare function useSetToken(): React__default.Dispatch<React__default.SetStateAction<string | null>>;
|
|
82
|
+
/**
|
|
83
|
+
* Returns a sign-up function that POSTs to the auth endpoint with credentials. On success, the response's
|
|
84
|
+
* `accessToken` is stored and the token context updates automatically.
|
|
85
|
+
*
|
|
86
|
+
* The payload must include at least `email` and `password`. You can include any extra fields (e.g. `name`, `designation`)
|
|
87
|
+
* and they will be forwarded to the `createUser` callback on the server.
|
|
88
|
+
*
|
|
89
|
+
* @returns A function `(data) => Promise<null | string>`. Call with sign-up payload. Returns `null` on success, or the error response text on failure.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```tsx
|
|
93
|
+
* import { useSignUp } from "naystack/auth/email/client";
|
|
94
|
+
*
|
|
95
|
+
* function SignUpForm() {
|
|
96
|
+
* const signUp = useSignUp();
|
|
97
|
+
*
|
|
98
|
+
* const handleSubmit = async (data: { name: string; email: string; password: string }) => {
|
|
99
|
+
* const error = await signUp(data);
|
|
100
|
+
* if (error) {
|
|
101
|
+
* setMessage(error); // e.g. "A user already exists"
|
|
102
|
+
* } else {
|
|
103
|
+
* router.replace("/dashboard");
|
|
104
|
+
* }
|
|
105
|
+
* };
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @category Auth
|
|
110
|
+
*/
|
|
12
111
|
declare function useSignUp(): (data: object) => Promise<string | null>;
|
|
112
|
+
/**
|
|
113
|
+
* Returns a login function that PUTs to the auth endpoint with credentials. On success, the response's
|
|
114
|
+
* `accessToken` is stored and the token context updates automatically.
|
|
115
|
+
*
|
|
116
|
+
* @returns A function `(data) => Promise<null | string>`. Call with `{ email, password }`. Returns `null` on success, or the error message on failure (e.g. `"Invalid password"`).
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```tsx
|
|
120
|
+
* import { useLogin } from "naystack/auth/email/client";
|
|
121
|
+
*
|
|
122
|
+
* function LoginForm() {
|
|
123
|
+
* const login = useLogin();
|
|
124
|
+
*
|
|
125
|
+
* const handleSubmit = async (data: { email: string; password: string }) => {
|
|
126
|
+
* const error = await login(data);
|
|
127
|
+
* if (error) {
|
|
128
|
+
* form.setError("password", { message: error });
|
|
129
|
+
* } else {
|
|
130
|
+
* router.replace("/dashboard");
|
|
131
|
+
* }
|
|
132
|
+
* };
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @category Auth
|
|
137
|
+
*/
|
|
13
138
|
declare function useLogin(): (data: object) => Promise<string | null>;
|
|
139
|
+
/**
|
|
140
|
+
* Returns a logout function that clears the token in context and sends DELETE to the auth endpoint with credentials.
|
|
141
|
+
* The token is cleared **immediately** (optimistic), and the DELETE request runs in the background.
|
|
142
|
+
*
|
|
143
|
+
* @returns A function `(data?) => Promise<void>`. Call with optional body (forwarded to the server's `onLogout` callback). Token is cleared immediately.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```tsx
|
|
147
|
+
* import { useLogout } from "naystack/auth/email/client";
|
|
148
|
+
*
|
|
149
|
+
* function SettingsPage() {
|
|
150
|
+
* const logout = useLogout();
|
|
151
|
+
* const router = useRouter();
|
|
152
|
+
*
|
|
153
|
+
* return (
|
|
154
|
+
* <button onClick={() => { logout(); router.push("/login"); }}>
|
|
155
|
+
* Log out
|
|
156
|
+
* </button>
|
|
157
|
+
* );
|
|
158
|
+
* }
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @category Auth
|
|
162
|
+
*/
|
|
14
163
|
declare function useLogout(): (data?: object) => Promise<void>;
|
|
15
164
|
|
|
16
165
|
export { AuthWrapper, TokenContext, useLogin, useLogout, useSetToken, useSignUp, useToken };
|
|
@@ -1,16 +1,165 @@
|
|
|
1
1
|
import React__default, { Dispatch, SetStateAction } from 'react';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* React context holding the current access token and setter; used by useToken/useSetToken and auth hooks.
|
|
5
|
+
* @category Auth
|
|
6
|
+
*/
|
|
3
7
|
declare const TokenContext: React__default.Context<{
|
|
4
8
|
token: string | null;
|
|
5
9
|
setToken: Dispatch<SetStateAction<string | null>>;
|
|
6
10
|
}>;
|
|
11
|
+
/**
|
|
12
|
+
* Provider that fetches the current access token from your auth endpoint and exposes it via TokenContext.
|
|
13
|
+
* Wrap your app (or the part that needs auth) so that `useToken()`, `useLogin()`, `useSignUp()`, `useLogout()` work.
|
|
14
|
+
* On mount it GETs `NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT` with credentials; the response's `accessToken` is stored and provided to children.
|
|
15
|
+
*
|
|
16
|
+
* Must be placed **above** `ApolloWrapper` in the component tree (since `ApolloWrapper` needs the token).
|
|
17
|
+
*
|
|
18
|
+
* @param children - React children (e.g. your app or a layout).
|
|
19
|
+
* @returns TokenContext.Provider wrapping children.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* // app/layout.tsx
|
|
24
|
+
* import { AuthWrapper } from "naystack/auth/email/client";
|
|
25
|
+
* import { ApolloWrapper } from "naystack/graphql/client";
|
|
26
|
+
*
|
|
27
|
+
* export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
28
|
+
* return (
|
|
29
|
+
* <html lang="en">
|
|
30
|
+
* <body>
|
|
31
|
+
* <AuthWrapper>
|
|
32
|
+
* <ApolloWrapper>{children}</ApolloWrapper>
|
|
33
|
+
* </AuthWrapper>
|
|
34
|
+
* </body>
|
|
35
|
+
* </html>
|
|
36
|
+
* );
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @category Auth
|
|
41
|
+
*/
|
|
7
42
|
declare const AuthWrapper: ({ children }: {
|
|
8
43
|
children: React__default.ReactNode;
|
|
9
44
|
}) => React__default.JSX.Element;
|
|
45
|
+
/**
|
|
46
|
+
* Returns the current JWT access token from TokenContext. Must be used inside `AuthWrapper`.
|
|
47
|
+
*
|
|
48
|
+
* Common uses: conditional rendering based on auth state, passing to custom fetch headers.
|
|
49
|
+
*
|
|
50
|
+
* @returns The access token string, or `null` if not yet loaded or user is logged out.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```tsx
|
|
54
|
+
* import { useToken } from "naystack/auth/email/client";
|
|
55
|
+
*
|
|
56
|
+
* function Navbar() {
|
|
57
|
+
* const token = useToken();
|
|
58
|
+
* return <Link href={token ? "/dashboard" : "/login"}>{token ? "Dashboard" : "Log in"}</Link>;
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example Using the token for custom API calls:
|
|
63
|
+
* ```tsx
|
|
64
|
+
* const token = useToken();
|
|
65
|
+
* const response = await fetch("/api/custom", {
|
|
66
|
+
* headers: { Authorization: `Bearer ${token}` },
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @category Auth
|
|
71
|
+
*/
|
|
10
72
|
declare function useToken(): string | null;
|
|
73
|
+
/**
|
|
74
|
+
* Returns the setter for the access token in TokenContext. Use to update token after login/signup or clear it on logout.
|
|
75
|
+
* Must be used inside `AuthWrapper`. Typically you won't need this directly — use `useLogin`, `useSignUp`, and `useLogout` instead.
|
|
76
|
+
*
|
|
77
|
+
* @returns `Dispatch<SetStateAction<string | null>>` — call with a string to set the token, or `null` to clear.
|
|
78
|
+
*
|
|
79
|
+
* @category Auth
|
|
80
|
+
*/
|
|
11
81
|
declare function useSetToken(): React__default.Dispatch<React__default.SetStateAction<string | null>>;
|
|
82
|
+
/**
|
|
83
|
+
* Returns a sign-up function that POSTs to the auth endpoint with credentials. On success, the response's
|
|
84
|
+
* `accessToken` is stored and the token context updates automatically.
|
|
85
|
+
*
|
|
86
|
+
* The payload must include at least `email` and `password`. You can include any extra fields (e.g. `name`, `designation`)
|
|
87
|
+
* and they will be forwarded to the `createUser` callback on the server.
|
|
88
|
+
*
|
|
89
|
+
* @returns A function `(data) => Promise<null | string>`. Call with sign-up payload. Returns `null` on success, or the error response text on failure.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```tsx
|
|
93
|
+
* import { useSignUp } from "naystack/auth/email/client";
|
|
94
|
+
*
|
|
95
|
+
* function SignUpForm() {
|
|
96
|
+
* const signUp = useSignUp();
|
|
97
|
+
*
|
|
98
|
+
* const handleSubmit = async (data: { name: string; email: string; password: string }) => {
|
|
99
|
+
* const error = await signUp(data);
|
|
100
|
+
* if (error) {
|
|
101
|
+
* setMessage(error); // e.g. "A user already exists"
|
|
102
|
+
* } else {
|
|
103
|
+
* router.replace("/dashboard");
|
|
104
|
+
* }
|
|
105
|
+
* };
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @category Auth
|
|
110
|
+
*/
|
|
12
111
|
declare function useSignUp(): (data: object) => Promise<string | null>;
|
|
112
|
+
/**
|
|
113
|
+
* Returns a login function that PUTs to the auth endpoint with credentials. On success, the response's
|
|
114
|
+
* `accessToken` is stored and the token context updates automatically.
|
|
115
|
+
*
|
|
116
|
+
* @returns A function `(data) => Promise<null | string>`. Call with `{ email, password }`. Returns `null` on success, or the error message on failure (e.g. `"Invalid password"`).
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```tsx
|
|
120
|
+
* import { useLogin } from "naystack/auth/email/client";
|
|
121
|
+
*
|
|
122
|
+
* function LoginForm() {
|
|
123
|
+
* const login = useLogin();
|
|
124
|
+
*
|
|
125
|
+
* const handleSubmit = async (data: { email: string; password: string }) => {
|
|
126
|
+
* const error = await login(data);
|
|
127
|
+
* if (error) {
|
|
128
|
+
* form.setError("password", { message: error });
|
|
129
|
+
* } else {
|
|
130
|
+
* router.replace("/dashboard");
|
|
131
|
+
* }
|
|
132
|
+
* };
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @category Auth
|
|
137
|
+
*/
|
|
13
138
|
declare function useLogin(): (data: object) => Promise<string | null>;
|
|
139
|
+
/**
|
|
140
|
+
* Returns a logout function that clears the token in context and sends DELETE to the auth endpoint with credentials.
|
|
141
|
+
* The token is cleared **immediately** (optimistic), and the DELETE request runs in the background.
|
|
142
|
+
*
|
|
143
|
+
* @returns A function `(data?) => Promise<void>`. Call with optional body (forwarded to the server's `onLogout` callback). Token is cleared immediately.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```tsx
|
|
147
|
+
* import { useLogout } from "naystack/auth/email/client";
|
|
148
|
+
*
|
|
149
|
+
* function SettingsPage() {
|
|
150
|
+
* const logout = useLogout();
|
|
151
|
+
* const router = useRouter();
|
|
152
|
+
*
|
|
153
|
+
* return (
|
|
154
|
+
* <button onClick={() => { logout(); router.push("/login"); }}>
|
|
155
|
+
* Log out
|
|
156
|
+
* </button>
|
|
157
|
+
* );
|
|
158
|
+
* }
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @category Auth
|
|
162
|
+
*/
|
|
14
163
|
declare function useLogout(): (data?: object) => Promise<void>;
|
|
15
164
|
|
|
16
165
|
export { AuthWrapper, TokenContext, useLogin, useLogout, useSetToken, useSignUp, useToken };
|
|
@@ -22,8 +22,7 @@ var email_exports = {};
|
|
|
22
22
|
__export(email_exports, {
|
|
23
23
|
checkAuthStatus: () => checkAuthStatus,
|
|
24
24
|
getContext: () => getContext,
|
|
25
|
-
getEmailAuthRoutes: () => getEmailAuthRoutes
|
|
26
|
-
logout: () => logout
|
|
25
|
+
getEmailAuthRoutes: () => getEmailAuthRoutes
|
|
27
26
|
});
|
|
28
27
|
module.exports = __toCommonJS(email_exports);
|
|
29
28
|
|
|
@@ -142,7 +141,6 @@ async function checkAuthStatus(redirectUnauthorizedURL) {
|
|
|
142
141
|
|
|
143
142
|
// src/auth/email/utils.ts
|
|
144
143
|
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
145
|
-
var import_headers2 = require("next/headers");
|
|
146
144
|
|
|
147
145
|
// src/auth/utils/errors.ts
|
|
148
146
|
var import_server2 = require("next/server");
|
|
@@ -219,15 +217,6 @@ var getContext = (req) => {
|
|
|
219
217
|
}
|
|
220
218
|
return { userId: null };
|
|
221
219
|
};
|
|
222
|
-
async function logout(data) {
|
|
223
|
-
const Cookie = await (0, import_headers2.cookies)();
|
|
224
|
-
Cookie.delete(REFRESH_COOKIE_NAME);
|
|
225
|
-
await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
|
|
226
|
-
method: "DELETE",
|
|
227
|
-
credentials: "include",
|
|
228
|
-
body: JSON.stringify(data)
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
220
|
|
|
232
221
|
// src/auth/email/routes/delete.ts
|
|
233
222
|
var getDeleteRoute = (options) => async (req) => {
|
|
@@ -322,6 +311,5 @@ function getEmailAuthRoutes(options) {
|
|
|
322
311
|
0 && (module.exports = {
|
|
323
312
|
checkAuthStatus,
|
|
324
313
|
getContext,
|
|
325
|
-
getEmailAuthRoutes
|
|
326
|
-
logout
|
|
314
|
+
getEmailAuthRoutes
|
|
327
315
|
});
|
|
@@ -1,10 +1,50 @@
|
|
|
1
1
|
import * as next_server from 'next/server';
|
|
2
2
|
import { InitRoutesOptions } from './types.mjs';
|
|
3
3
|
export { checkAuthStatus } from './token.mjs';
|
|
4
|
-
export { getContext
|
|
4
|
+
export { getContext } from './utils.mjs';
|
|
5
5
|
import '../types.mjs';
|
|
6
6
|
import '../../graphql/types.mjs';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Returns Next.js route handlers for email auth. Mount them in your auth API route (e.g. `app/api/(auth)/email/route.ts`).
|
|
10
|
+
*
|
|
11
|
+
* The library automatically hashes passwords on sign-up and reads `SIGNING_KEY` / `REFRESH_KEY` from env vars.
|
|
12
|
+
* Optionally supports Cloudflare Turnstile captcha validation when `TURNSTILE_KEY` is set.
|
|
13
|
+
*
|
|
14
|
+
* @param options - Configuration for the auth routes. See {@link InitRoutesOptions}.
|
|
15
|
+
* @param options.getUser - Given request body (e.g. `{ email }`), returns the user from DB or undefined. Used for login and duplicate check on sign-up.
|
|
16
|
+
* @param options.createUser - Given sign-up data with hashed password, inserts the user and returns the created record.
|
|
17
|
+
* @param options.onError - Optional. Called on validation/auth errors; return a NextResponse to customize the error response.
|
|
18
|
+
* @param options.onSignUp - Optional. Called after successful sign-up with `(userId, requestBody)`.
|
|
19
|
+
* @param options.onLogin - Optional. Called after successful login with `(userId, requestBody)`.
|
|
20
|
+
* @param options.onRefresh - Optional. Called when GET is used to refresh tokens with `(userId, requestBody)`.
|
|
21
|
+
* @param options.onLogout - Optional. Called when DELETE is used to logout with `(userId, requestBody)`.
|
|
22
|
+
* @returns Object with `GET` (refresh tokens), `POST` (sign up), `PUT` (login), `DELETE` (logout) — export as your route's handlers.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* // app/api/(auth)/email/route.ts
|
|
27
|
+
* import { getEmailAuthRoutes } from "naystack/auth";
|
|
28
|
+
*
|
|
29
|
+
* export const { GET, POST, PUT, DELETE } = getEmailAuthRoutes({
|
|
30
|
+
* getUser: async ({ email }: { email: string }) => {
|
|
31
|
+
* const [user] = await db.select({ id: UserTable.id, password: UserTable.password })
|
|
32
|
+
* .from(UserTable).where(eq(UserTable.email, email));
|
|
33
|
+
* return user;
|
|
34
|
+
* },
|
|
35
|
+
* createUser: async (data) => {
|
|
36
|
+
* const [user] = await db.insert(UserTable).values(data)
|
|
37
|
+
* .returning({ id: UserTable.id, password: UserTable.password });
|
|
38
|
+
* return user;
|
|
39
|
+
* },
|
|
40
|
+
* onSignUp: async (userId, body) => {
|
|
41
|
+
* // Optional: run logic after sign-up (e.g. create an org)
|
|
42
|
+
* },
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @category Auth
|
|
47
|
+
*/
|
|
8
48
|
declare function getEmailAuthRoutes(options: InitRoutesOptions): {
|
|
9
49
|
GET: (req: next_server.NextRequest) => Promise<next_server.NextResponse<{
|
|
10
50
|
accessToken: string | undefined;
|
|
@@ -1,10 +1,50 @@
|
|
|
1
1
|
import * as next_server from 'next/server';
|
|
2
2
|
import { InitRoutesOptions } from './types.js';
|
|
3
3
|
export { checkAuthStatus } from './token.js';
|
|
4
|
-
export { getContext
|
|
4
|
+
export { getContext } from './utils.js';
|
|
5
5
|
import '../types.js';
|
|
6
6
|
import '../../graphql/types.js';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Returns Next.js route handlers for email auth. Mount them in your auth API route (e.g. `app/api/(auth)/email/route.ts`).
|
|
10
|
+
*
|
|
11
|
+
* The library automatically hashes passwords on sign-up and reads `SIGNING_KEY` / `REFRESH_KEY` from env vars.
|
|
12
|
+
* Optionally supports Cloudflare Turnstile captcha validation when `TURNSTILE_KEY` is set.
|
|
13
|
+
*
|
|
14
|
+
* @param options - Configuration for the auth routes. See {@link InitRoutesOptions}.
|
|
15
|
+
* @param options.getUser - Given request body (e.g. `{ email }`), returns the user from DB or undefined. Used for login and duplicate check on sign-up.
|
|
16
|
+
* @param options.createUser - Given sign-up data with hashed password, inserts the user and returns the created record.
|
|
17
|
+
* @param options.onError - Optional. Called on validation/auth errors; return a NextResponse to customize the error response.
|
|
18
|
+
* @param options.onSignUp - Optional. Called after successful sign-up with `(userId, requestBody)`.
|
|
19
|
+
* @param options.onLogin - Optional. Called after successful login with `(userId, requestBody)`.
|
|
20
|
+
* @param options.onRefresh - Optional. Called when GET is used to refresh tokens with `(userId, requestBody)`.
|
|
21
|
+
* @param options.onLogout - Optional. Called when DELETE is used to logout with `(userId, requestBody)`.
|
|
22
|
+
* @returns Object with `GET` (refresh tokens), `POST` (sign up), `PUT` (login), `DELETE` (logout) — export as your route's handlers.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* // app/api/(auth)/email/route.ts
|
|
27
|
+
* import { getEmailAuthRoutes } from "naystack/auth";
|
|
28
|
+
*
|
|
29
|
+
* export const { GET, POST, PUT, DELETE } = getEmailAuthRoutes({
|
|
30
|
+
* getUser: async ({ email }: { email: string }) => {
|
|
31
|
+
* const [user] = await db.select({ id: UserTable.id, password: UserTable.password })
|
|
32
|
+
* .from(UserTable).where(eq(UserTable.email, email));
|
|
33
|
+
* return user;
|
|
34
|
+
* },
|
|
35
|
+
* createUser: async (data) => {
|
|
36
|
+
* const [user] = await db.insert(UserTable).values(data)
|
|
37
|
+
* .returning({ id: UserTable.id, password: UserTable.password });
|
|
38
|
+
* return user;
|
|
39
|
+
* },
|
|
40
|
+
* onSignUp: async (userId, body) => {
|
|
41
|
+
* // Optional: run logic after sign-up (e.g. create an org)
|
|
42
|
+
* },
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @category Auth
|
|
47
|
+
*/
|
|
8
48
|
declare function getEmailAuthRoutes(options: InitRoutesOptions): {
|
|
9
49
|
GET: (req: next_server.NextRequest) => Promise<next_server.NextResponse<{
|
|
10
50
|
accessToken: string | undefined;
|
|
@@ -113,7 +113,6 @@ async function checkAuthStatus(redirectUnauthorizedURL) {
|
|
|
113
113
|
|
|
114
114
|
// src/auth/email/utils.ts
|
|
115
115
|
import { verify as verify2 } from "jsonwebtoken";
|
|
116
|
-
import { cookies as cookies2 } from "next/headers";
|
|
117
116
|
|
|
118
117
|
// src/auth/utils/errors.ts
|
|
119
118
|
import { NextResponse as NextResponse2 } from "next/server";
|
|
@@ -190,15 +189,6 @@ var getContext = (req) => {
|
|
|
190
189
|
}
|
|
191
190
|
return { userId: null };
|
|
192
191
|
};
|
|
193
|
-
async function logout(data) {
|
|
194
|
-
const Cookie = await cookies2();
|
|
195
|
-
Cookie.delete(REFRESH_COOKIE_NAME);
|
|
196
|
-
await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
|
|
197
|
-
method: "DELETE",
|
|
198
|
-
credentials: "include",
|
|
199
|
-
body: JSON.stringify(data)
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
192
|
|
|
203
193
|
// src/auth/email/routes/delete.ts
|
|
204
194
|
var getDeleteRoute = (options) => async (req) => {
|
|
@@ -292,6 +282,5 @@ function getEmailAuthRoutes(options) {
|
|
|
292
282
|
export {
|
|
293
283
|
checkAuthStatus,
|
|
294
284
|
getContext,
|
|
295
|
-
getEmailAuthRoutes
|
|
296
|
-
logout
|
|
285
|
+
getEmailAuthRoutes
|
|
297
286
|
};
|
|
@@ -120,7 +120,6 @@ function getUserIdFromRefreshToken(refreshToken) {
|
|
|
120
120
|
|
|
121
121
|
// src/auth/email/utils.ts
|
|
122
122
|
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
123
|
-
var import_headers2 = require("next/headers");
|
|
124
123
|
|
|
125
124
|
// src/auth/utils/errors.ts
|
|
126
125
|
var import_server2 = require("next/server");
|
|
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
|
|
|
3
3
|
import { InitRoutesOptions } from '../types.mjs';
|
|
4
4
|
import '../../types.mjs';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Returns the DELETE route handler for logout.
|
|
8
|
+
* @param options - InitRoutesOptions
|
|
9
|
+
* @returns Async route handler
|
|
10
|
+
*/
|
|
6
11
|
declare const getDeleteRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<{
|
|
7
12
|
accessToken: string | undefined;
|
|
8
13
|
refreshToken: string | undefined;
|
|
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
|
|
|
3
3
|
import { InitRoutesOptions } from '../types.js';
|
|
4
4
|
import '../../types.js';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Returns the DELETE route handler for logout.
|
|
8
|
+
* @param options - InitRoutesOptions
|
|
9
|
+
* @returns Async route handler
|
|
10
|
+
*/
|
|
6
11
|
declare const getDeleteRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<{
|
|
7
12
|
accessToken: string | undefined;
|
|
8
13
|
refreshToken: string | undefined;
|
|
@@ -94,7 +94,6 @@ function getUserIdFromRefreshToken(refreshToken) {
|
|
|
94
94
|
|
|
95
95
|
// src/auth/email/utils.ts
|
|
96
96
|
import { verify as verify2 } from "jsonwebtoken";
|
|
97
|
-
import { cookies as cookies2 } from "next/headers";
|
|
98
97
|
|
|
99
98
|
// src/auth/utils/errors.ts
|
|
100
99
|
import { NextResponse as NextResponse2 } from "next/server";
|
|
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
|
|
|
3
3
|
import { InitRoutesOptions } from '../types.mjs';
|
|
4
4
|
import '../../types.mjs';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Returns the GET route handler for token refresh (exchange refresh cookie for new tokens).
|
|
8
|
+
* @param options - InitRoutesOptions
|
|
9
|
+
* @returns Async route handler
|
|
10
|
+
*/
|
|
6
11
|
declare const getGetRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<{
|
|
7
12
|
accessToken: string | undefined;
|
|
8
13
|
refreshToken: string | undefined;
|
|
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
|
|
|
3
3
|
import { InitRoutesOptions } from '../types.js';
|
|
4
4
|
import '../../types.js';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Returns the GET route handler for token refresh (exchange refresh cookie for new tokens).
|
|
8
|
+
* @param options - InitRoutesOptions
|
|
9
|
+
* @returns Async route handler
|
|
10
|
+
*/
|
|
6
11
|
declare const getGetRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<{
|
|
7
12
|
accessToken: string | undefined;
|
|
8
13
|
refreshToken: string | undefined;
|
|
@@ -129,7 +129,6 @@ function verifyUser(user, password) {
|
|
|
129
129
|
|
|
130
130
|
// src/auth/email/utils.ts
|
|
131
131
|
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
132
|
-
var import_headers2 = require("next/headers");
|
|
133
132
|
async function massageRequest(req, options) {
|
|
134
133
|
const data = await req.json();
|
|
135
134
|
if (!data.password)
|
|
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
|
|
|
3
3
|
import { InitRoutesOptions } from '../types.mjs';
|
|
4
4
|
import '../../types.mjs';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Returns the POST route handler for sign up.
|
|
8
|
+
* @param options - InitRoutesOptions
|
|
9
|
+
* @returns Async route handler
|
|
10
|
+
*/
|
|
6
11
|
declare const getPostRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
7
12
|
|
|
8
13
|
export { getPostRoute };
|
|
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
|
|
|
3
3
|
import { InitRoutesOptions } from '../types.js';
|
|
4
4
|
import '../../types.js';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Returns the POST route handler for sign up.
|
|
8
|
+
* @param options - InitRoutesOptions
|
|
9
|
+
* @returns Async route handler
|
|
10
|
+
*/
|
|
6
11
|
declare const getPostRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
7
12
|
|
|
8
13
|
export { getPostRoute };
|
|
@@ -105,7 +105,6 @@ function verifyUser(user, password) {
|
|
|
105
105
|
|
|
106
106
|
// src/auth/email/utils.ts
|
|
107
107
|
import { verify as verify2 } from "jsonwebtoken";
|
|
108
|
-
import { cookies as cookies2 } from "next/headers";
|
|
109
108
|
async function massageRequest(req, options) {
|
|
110
109
|
const data = await req.json();
|
|
111
110
|
if (!data.password)
|
|
@@ -128,7 +128,6 @@ function verifyUser(user, password) {
|
|
|
128
128
|
|
|
129
129
|
// src/auth/email/utils.ts
|
|
130
130
|
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
131
|
-
var import_headers2 = require("next/headers");
|
|
132
131
|
async function massageRequest(req, options) {
|
|
133
132
|
const data = await req.json();
|
|
134
133
|
if (!data.password)
|
|
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
|
|
|
3
3
|
import { InitRoutesOptions } from '../types.mjs';
|
|
4
4
|
import '../../types.mjs';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Returns the PUT route handler for login.
|
|
8
|
+
* @param options - InitRoutesOptions
|
|
9
|
+
* @returns Async route handler
|
|
10
|
+
*/
|
|
6
11
|
declare const getPutRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
7
12
|
|
|
8
13
|
export { getPutRoute };
|
|
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
|
|
|
3
3
|
import { InitRoutesOptions } from '../types.js';
|
|
4
4
|
import '../../types.js';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Returns the PUT route handler for login.
|
|
8
|
+
* @param options - InitRoutesOptions
|
|
9
|
+
* @returns Async route handler
|
|
10
|
+
*/
|
|
6
11
|
declare const getPutRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
7
12
|
|
|
8
13
|
export { getPutRoute };
|