@neutralauth/internal-auth 0.10.11
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 +201 -0
- package/README.md +39 -0
- package/dist/auth-config.d.ts +43 -0
- package/dist/auth-config.d.ts.map +1 -0
- package/dist/auth-config.js +43 -0
- package/dist/auth-config.js.map +1 -0
- package/dist/auth-options.d.ts +3 -0
- package/dist/auth-options.d.ts.map +1 -0
- package/dist/auth-options.js +40 -0
- package/dist/auth-options.js.map +1 -0
- package/dist/auth.d.ts +2 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +4 -0
- package/dist/auth.js.map +1 -0
- package/dist/client/adapter-utils.d.ts +66 -0
- package/dist/client/adapter-utils.d.ts.map +1 -0
- package/dist/client/adapter-utils.js +437 -0
- package/dist/client/adapter-utils.js.map +1 -0
- package/dist/client/adapter.d.ts +14 -0
- package/dist/client/adapter.d.ts.map +1 -0
- package/dist/client/adapter.js +274 -0
- package/dist/client/adapter.js.map +1 -0
- package/dist/client/create-api.d.ts +141 -0
- package/dist/client/create-api.d.ts.map +1 -0
- package/dist/client/create-api.js +205 -0
- package/dist/client/create-api.js.map +1 -0
- package/dist/client/create-client.d.ts +183 -0
- package/dist/client/create-client.d.ts.map +1 -0
- package/dist/client/create-client.js +311 -0
- package/dist/client/create-client.js.map +1 -0
- package/dist/client/create-schema.d.ts +19 -0
- package/dist/client/create-schema.d.ts.map +1 -0
- package/dist/client/create-schema.js +114 -0
- package/dist/client/create-schema.js.map +1 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +10 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/plugins/index.d.ts +3 -0
- package/dist/client/plugins/index.d.ts.map +1 -0
- package/dist/client/plugins/index.js +3 -0
- package/dist/client/plugins/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +36 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +787 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/adapter.d.ts +130 -0
- package/dist/component/adapter.d.ts.map +1 -0
- package/dist/component/adapter.js +5 -0
- package/dist/component/adapter.js.map +1 -0
- package/dist/component/adapterTest.d.ts +10 -0
- package/dist/component/adapterTest.d.ts.map +1 -0
- package/dist/component/adapterTest.js +409 -0
- package/dist/component/adapterTest.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +4 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/schema.d.ts +474 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +139 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/nextjs/client.d.ts +4 -0
- package/dist/nextjs/client.d.ts.map +1 -0
- package/dist/nextjs/client.js +37 -0
- package/dist/nextjs/client.js.map +1 -0
- package/dist/nextjs/index.d.ts +22 -0
- package/dist/nextjs/index.d.ts.map +1 -0
- package/dist/nextjs/index.js +98 -0
- package/dist/nextjs/index.js.map +1 -0
- package/dist/plugins/convex/client.d.ts +6 -0
- package/dist/plugins/convex/client.d.ts.map +1 -0
- package/dist/plugins/convex/client.js +7 -0
- package/dist/plugins/convex/client.js.map +1 -0
- package/dist/plugins/convex/index.d.ts +322 -0
- package/dist/plugins/convex/index.d.ts.map +1 -0
- package/dist/plugins/convex/index.js +422 -0
- package/dist/plugins/convex/index.js.map +1 -0
- package/dist/plugins/cross-domain/client.d.ts +132 -0
- package/dist/plugins/cross-domain/client.d.ts.map +1 -0
- package/dist/plugins/cross-domain/client.js +192 -0
- package/dist/plugins/cross-domain/client.js.map +1 -0
- package/dist/plugins/cross-domain/index.d.ts +51 -0
- package/dist/plugins/cross-domain/index.d.ts.map +1 -0
- package/dist/plugins/cross-domain/index.js +173 -0
- package/dist/plugins/cross-domain/index.js.map +1 -0
- package/dist/plugins/index.d.ts +3 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +3 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/react/index.d.ts +80 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +190 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react-start/index.d.ts +13 -0
- package/dist/react-start/index.d.ts.map +1 -0
- package/dist/react-start/index.js +101 -0
- package/dist/react-start/index.js.map +1 -0
- package/dist/utils/index.d.ts +33 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +91 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +208 -0
- package/src/auth-config.ts +80 -0
- package/src/auth-options.ts +54 -0
- package/src/auth.ts +4 -0
- package/src/client/adapter-utils.ts +639 -0
- package/src/client/adapter.test.ts +83 -0
- package/src/client/adapter.ts +363 -0
- package/src/client/create-api.ts +339 -0
- package/src/client/create-client.ts +452 -0
- package/src/client/create-schema.ts +166 -0
- package/src/client/index.ts +22 -0
- package/src/client/plugins/index.ts +2 -0
- package/src/component/_generated/api.ts +52 -0
- package/src/component/_generated/component.ts +2008 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +161 -0
- package/src/component/adapter.ts +13 -0
- package/src/component/adapterTest.ts +505 -0
- package/src/component/convex.config.ts +5 -0
- package/src/component/schema.ts +142 -0
- package/src/nextjs/client.tsx +54 -0
- package/src/nextjs/index.ts +152 -0
- package/src/plugins/convex/client.ts +9 -0
- package/src/plugins/convex/index.ts +596 -0
- package/src/plugins/cross-domain/client.test.ts +217 -0
- package/src/plugins/cross-domain/client.ts +234 -0
- package/src/plugins/cross-domain/index.ts +199 -0
- package/src/plugins/index.ts +2 -0
- package/src/react/index.tsx +304 -0
- package/src/react-start/index.ts +153 -0
- package/src/react-start/vite-env.d.ts +2 -0
- package/src/test.ts +18 -0
- package/src/utils/index.ts +171 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
export function parseSetCookieHeader(header) {
|
|
2
|
+
const cookieMap = new Map();
|
|
3
|
+
const cookies = header.split(", ");
|
|
4
|
+
cookies.forEach((cookie) => {
|
|
5
|
+
const [nameValue, ...attributes] = cookie.split("; ");
|
|
6
|
+
const [name, value] = nameValue.split("=");
|
|
7
|
+
const cookieObj = { value };
|
|
8
|
+
attributes.forEach((attr) => {
|
|
9
|
+
const [attrName, attrValue] = attr.split("=");
|
|
10
|
+
cookieObj[attrName.toLowerCase()] = attrValue;
|
|
11
|
+
});
|
|
12
|
+
cookieMap.set(name, cookieObj);
|
|
13
|
+
});
|
|
14
|
+
return cookieMap;
|
|
15
|
+
}
|
|
16
|
+
export function getSetCookie(header, prevCookie) {
|
|
17
|
+
const parsed = parseSetCookieHeader(header);
|
|
18
|
+
let toSetCookie = {};
|
|
19
|
+
parsed.forEach((cookie, key) => {
|
|
20
|
+
const expiresAt = cookie["expires"];
|
|
21
|
+
const maxAge = cookie["max-age"];
|
|
22
|
+
const expires = expiresAt
|
|
23
|
+
? new Date(String(expiresAt))
|
|
24
|
+
: maxAge
|
|
25
|
+
? new Date(Date.now() + Number(maxAge) * 1000)
|
|
26
|
+
: null;
|
|
27
|
+
toSetCookie[key] = {
|
|
28
|
+
value: cookie["value"],
|
|
29
|
+
expires: expires ? expires.toISOString() : null,
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
if (prevCookie) {
|
|
33
|
+
try {
|
|
34
|
+
const prevCookieParsed = JSON.parse(prevCookie);
|
|
35
|
+
toSetCookie = {
|
|
36
|
+
...prevCookieParsed,
|
|
37
|
+
...toSetCookie,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
//
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return JSON.stringify(toSetCookie);
|
|
45
|
+
}
|
|
46
|
+
export function getCookie(cookie) {
|
|
47
|
+
let parsed = {};
|
|
48
|
+
try {
|
|
49
|
+
parsed = JSON.parse(cookie);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// noop
|
|
53
|
+
}
|
|
54
|
+
const toSend = Object.entries(parsed).reduce((acc, [key, value]) => {
|
|
55
|
+
if (value.expires && new Date(value.expires) < new Date()) {
|
|
56
|
+
return acc;
|
|
57
|
+
}
|
|
58
|
+
return `${acc}; ${key}=${value.value}`;
|
|
59
|
+
}, "");
|
|
60
|
+
return toSend;
|
|
61
|
+
}
|
|
62
|
+
export const crossDomainClient = (opts = {}) => {
|
|
63
|
+
let store = null;
|
|
64
|
+
const cookieName = `${opts?.storagePrefix || "better-auth"}_cookie`;
|
|
65
|
+
const localCacheName = `${opts?.storagePrefix || "better-auth"}_session_data`;
|
|
66
|
+
const storage = opts?.storage || (typeof localStorage !== "undefined" ? localStorage : undefined);
|
|
67
|
+
return {
|
|
68
|
+
id: "cross-domain",
|
|
69
|
+
$InferServerPlugin: {},
|
|
70
|
+
getActions(_, $store) {
|
|
71
|
+
store = $store;
|
|
72
|
+
return {
|
|
73
|
+
/**
|
|
74
|
+
* Get the stored cookie.
|
|
75
|
+
*
|
|
76
|
+
* You can use this to get the cookie stored in the device and use it in your fetch
|
|
77
|
+
* requests.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* const cookie = client.getCookie();
|
|
82
|
+
* fetch("https://api.example.com", {
|
|
83
|
+
* headers: {
|
|
84
|
+
* cookie,
|
|
85
|
+
* },
|
|
86
|
+
* });
|
|
87
|
+
*/
|
|
88
|
+
getCookie: () => {
|
|
89
|
+
const cookie = storage?.getItem(cookieName);
|
|
90
|
+
return getCookie(cookie || "{}");
|
|
91
|
+
},
|
|
92
|
+
/**
|
|
93
|
+
* Notify the session signal.
|
|
94
|
+
*
|
|
95
|
+
* This is used to trigger an update in useSession, generally when a new session
|
|
96
|
+
* token is set.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* client.notifySessionSignal();
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
updateSession: () => {
|
|
104
|
+
$store.notify("$sessionSignal");
|
|
105
|
+
},
|
|
106
|
+
/**
|
|
107
|
+
* Get the stored session data.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* const sessionData = client.getSessionData();
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
getSessionData: () => {
|
|
115
|
+
const sessionData = storage?.getItem(localCacheName);
|
|
116
|
+
if (!sessionData)
|
|
117
|
+
return null;
|
|
118
|
+
try {
|
|
119
|
+
const parsed = JSON.parse(sessionData);
|
|
120
|
+
if (parsed && typeof parsed === "object" && Object.keys(parsed).length === 0)
|
|
121
|
+
return null;
|
|
122
|
+
return parsed;
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
fetchPlugins: [
|
|
131
|
+
{
|
|
132
|
+
id: "convex",
|
|
133
|
+
name: "Convex",
|
|
134
|
+
hooks: {
|
|
135
|
+
async onSuccess(context) {
|
|
136
|
+
if (!storage) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const setCookie = context.response.headers.get("set-better-auth-cookie");
|
|
140
|
+
if (setCookie) {
|
|
141
|
+
const prevCookie = storage.getItem(cookieName);
|
|
142
|
+
const toSetCookie = getSetCookie(setCookie || "", prevCookie ?? undefined);
|
|
143
|
+
await storage.setItem(cookieName, toSetCookie);
|
|
144
|
+
// Only notify on session cookie set
|
|
145
|
+
if (setCookie.includes(".session_token=")) {
|
|
146
|
+
store?.notify("$sessionSignal");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (context.request.url.toString().includes("/get-session") &&
|
|
150
|
+
!opts?.disableCache) {
|
|
151
|
+
const data = context.data;
|
|
152
|
+
storage.setItem(localCacheName, JSON.stringify(data));
|
|
153
|
+
if (data === null) {
|
|
154
|
+
storage.setItem(cookieName, "{}");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
async init(url, options) {
|
|
160
|
+
if (!storage) {
|
|
161
|
+
return {
|
|
162
|
+
url,
|
|
163
|
+
options: options,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
options = options || {};
|
|
167
|
+
const storedCookie = storage.getItem(cookieName);
|
|
168
|
+
const cookie = getCookie(storedCookie || "{}");
|
|
169
|
+
options.credentials = "omit";
|
|
170
|
+
options.headers = {
|
|
171
|
+
...options.headers,
|
|
172
|
+
"Better-Auth-Cookie": cookie,
|
|
173
|
+
};
|
|
174
|
+
if (url.includes("/sign-out")) {
|
|
175
|
+
await storage.setItem(cookieName, "{}");
|
|
176
|
+
store?.atoms.session?.set({
|
|
177
|
+
data: null,
|
|
178
|
+
error: null,
|
|
179
|
+
isPending: false,
|
|
180
|
+
});
|
|
181
|
+
storage.setItem(localCacheName, "{}");
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
url,
|
|
185
|
+
options: options,
|
|
186
|
+
};
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/plugins/cross-domain/client.ts"],"names":[],"mappings":"AAeA,MAAM,UAAU,oB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AO,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;4BACpC,CAAC;wBACH,CAAC;oBACH,CAAC;iBACF;gBACD,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO;oBACrB,IAAI,CAAC,OAAO,EAAE,CAAC;wBACb,OAAO;4BACL,GAAG;4BACH,OAAO,EAAE,OAA4B;yBACtC,CAAC;oBACJ,CAAC;oBACD,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;oBACxB,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;oBACjD,MAAM,MAAM,GAAG,SAAS,CAAC,YAAY,IAAI,IAAI,CAAC,CAAC;oBAC/C,OAAO,CAAC,WAAW,GAAG,MAAM,CAAC;oBAC7B,OAAO,CAAC,OAAO,GAAG;wBAChB,GAAG,OAAO,CAAC,OAAO;wBAClB,oBAAoB,EAAE,MAAM;qBAC7B,CAAC;oBACF,IAAI,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;wBAC9B,MAAM,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;wBACxC,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC;4BACxB,IAAI,EAAE,IAAI;4BACV,KAAK,EAAE,IAAI;4BACX,SAAS,EAAE,KAAK;yBACjB,CAAC,CAAC;wBACH,OAAO,CAAC,OAAO,CAAC,cAAc,EAAE,IAAI,CAAC,CAAC;oBACxC,CAAC;oBACD,OAAO;wBACL,GAAG;wBACH,OAAO,EAAE,OAA4B;qBACtC,CAAC;gBACJ,CAAC;aACF;SACF;KAC+B,CAAC;AACrC,CAAC,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const crossDomain: ({ siteUrl }: {
|
|
3
|
+
siteUrl: string;
|
|
4
|
+
}) => {
|
|
5
|
+
id: "cross-domain";
|
|
6
|
+
init(): {
|
|
7
|
+
options: {
|
|
8
|
+
trustedOrigins: string[];
|
|
9
|
+
};
|
|
10
|
+
context: {
|
|
11
|
+
oauthConfig: {
|
|
12
|
+
storeStateStrategy: "database";
|
|
13
|
+
skipStateCookieCheck: true;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
hooks: {
|
|
18
|
+
before: ({
|
|
19
|
+
matcher(ctx: import("better-auth").HookEndpointContext): boolean;
|
|
20
|
+
handler: (inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{
|
|
21
|
+
context: {
|
|
22
|
+
headers: Headers;
|
|
23
|
+
};
|
|
24
|
+
} | undefined>;
|
|
25
|
+
} | {
|
|
26
|
+
matcher: (ctx: import("better-auth").HookEndpointContext) => boolean;
|
|
27
|
+
handler: (inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{
|
|
28
|
+
context: import("better-call").MiddlewareContext<import("better-call").MiddlewareOptions, import("better-auth").AuthContext<import("better-auth").BetterAuthOptions> & {
|
|
29
|
+
returned?: unknown | undefined;
|
|
30
|
+
responseHeaders?: Headers | undefined;
|
|
31
|
+
}>;
|
|
32
|
+
}>;
|
|
33
|
+
})[];
|
|
34
|
+
after: {
|
|
35
|
+
matcher(ctx: import("better-auth").HookEndpointContext): boolean;
|
|
36
|
+
handler: (inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<void>;
|
|
37
|
+
}[];
|
|
38
|
+
};
|
|
39
|
+
endpoints: {
|
|
40
|
+
verifyOneTimeToken: import("better-call").StrictEndpoint<"/cross-domain/one-time-token/verify", {
|
|
41
|
+
method: "POST";
|
|
42
|
+
body: z.ZodObject<{
|
|
43
|
+
token: z.ZodString;
|
|
44
|
+
}, z.core.$strip>;
|
|
45
|
+
}, {
|
|
46
|
+
session: import("better-auth").Session & Record<string, any>;
|
|
47
|
+
user: import("better-auth").User & Record<string, any>;
|
|
48
|
+
}>;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/cross-domain/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,eAAO,MAAM,WAAW,GAAI,aAAa;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4L3D,CAAC"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { setSessionCookie } from "better-auth/cookies";
|
|
2
|
+
import { generateRandomString } from "better-auth/crypto";
|
|
3
|
+
import { createAuthEndpoint, createAuthMiddleware, oneTimeToken as oneTimeTokenPlugin, } from "better-auth/plugins";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
export const crossDomain = ({ siteUrl }) => {
|
|
6
|
+
const oneTimeToken = oneTimeTokenPlugin();
|
|
7
|
+
const rewriteCallbackURL = (callbackURL) => {
|
|
8
|
+
if (callbackURL && !callbackURL.startsWith("/")) {
|
|
9
|
+
return callbackURL;
|
|
10
|
+
}
|
|
11
|
+
const relativeCallbackURL = callbackURL || "/";
|
|
12
|
+
return new URL(relativeCallbackURL, siteUrl).toString();
|
|
13
|
+
};
|
|
14
|
+
const isExpoNative = (ctx) => {
|
|
15
|
+
return ctx.headers?.has("expo-origin");
|
|
16
|
+
};
|
|
17
|
+
return {
|
|
18
|
+
id: "cross-domain",
|
|
19
|
+
// TODO: remove this in the next minor release, it doesn't
|
|
20
|
+
// actually affect ctx.trustedOrigins. cors allowedOrigins
|
|
21
|
+
// is using it, via options.trustedOrigins, though, so it's
|
|
22
|
+
// a breaking change.
|
|
23
|
+
init() {
|
|
24
|
+
return {
|
|
25
|
+
options: {
|
|
26
|
+
trustedOrigins: [siteUrl],
|
|
27
|
+
},
|
|
28
|
+
context: {
|
|
29
|
+
oauthConfig: {
|
|
30
|
+
storeStateStrategy: "database",
|
|
31
|
+
// We could fake the cookie by sending a header, but it would need
|
|
32
|
+
// to be set on a 302 redirect from the identity provider, and we
|
|
33
|
+
// don't have a way to do that. This only means we can't stop an
|
|
34
|
+
// oauth flow that started in one browser from continuing in
|
|
35
|
+
// another. We still verify the state token from the query string
|
|
36
|
+
// against the database.
|
|
37
|
+
skipStateCookieCheck: true,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
hooks: {
|
|
43
|
+
before: [
|
|
44
|
+
{
|
|
45
|
+
matcher(ctx) {
|
|
46
|
+
return (Boolean(ctx.request?.headers.get("better-auth-cookie") ||
|
|
47
|
+
ctx.headers?.get("better-auth-cookie")) && !isExpoNative(ctx));
|
|
48
|
+
},
|
|
49
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
50
|
+
const existingHeaders = (ctx.request?.headers ||
|
|
51
|
+
ctx.headers);
|
|
52
|
+
const headers = new Headers({
|
|
53
|
+
...Object.fromEntries(existingHeaders?.entries()),
|
|
54
|
+
});
|
|
55
|
+
// Skip if the request has an authorization header
|
|
56
|
+
if (headers.get("authorization")) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const cookie = headers.get("better-auth-cookie");
|
|
60
|
+
if (!cookie) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
headers.append("cookie", cookie);
|
|
64
|
+
return {
|
|
65
|
+
context: {
|
|
66
|
+
headers,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
matcher: (ctx) => {
|
|
73
|
+
return (ctx.method === "GET" &&
|
|
74
|
+
ctx.path.startsWith("/verify-email") &&
|
|
75
|
+
!isExpoNative(ctx));
|
|
76
|
+
},
|
|
77
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
78
|
+
if (ctx.query?.callbackURL) {
|
|
79
|
+
ctx.query.callbackURL = rewriteCallbackURL(ctx.query.callbackURL);
|
|
80
|
+
}
|
|
81
|
+
return { context: ctx };
|
|
82
|
+
}),
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
matcher: (ctx) => {
|
|
86
|
+
return (((ctx.method === "POST" && ctx.path.startsWith("/link-social")) ||
|
|
87
|
+
ctx.path.startsWith("/send-verification-email") ||
|
|
88
|
+
ctx.path.startsWith("/sign-in/email") ||
|
|
89
|
+
ctx.path.startsWith("/sign-in/social") ||
|
|
90
|
+
ctx.path.startsWith("/sign-in/magic-link") ||
|
|
91
|
+
ctx.path.startsWith("/delete-user") ||
|
|
92
|
+
ctx.path.startsWith("/change-email")) &&
|
|
93
|
+
!isExpoNative(ctx));
|
|
94
|
+
},
|
|
95
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
96
|
+
const isSignIn = ctx.path.startsWith("/sign-in");
|
|
97
|
+
ctx.body.callbackURL = rewriteCallbackURL(ctx.body.callbackURL);
|
|
98
|
+
if (isSignIn && ctx.body.newUserCallbackURL) {
|
|
99
|
+
ctx.body.newUserCallbackURL = rewriteCallbackURL(ctx.body.newUserCallbackURL);
|
|
100
|
+
}
|
|
101
|
+
if (isSignIn && ctx.body.errorCallbackURL) {
|
|
102
|
+
ctx.body.errorCallbackURL = rewriteCallbackURL(ctx.body.errorCallbackURL);
|
|
103
|
+
}
|
|
104
|
+
return { context: ctx };
|
|
105
|
+
}),
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
after: [
|
|
109
|
+
{
|
|
110
|
+
matcher(ctx) {
|
|
111
|
+
return !isExpoNative(ctx);
|
|
112
|
+
},
|
|
113
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
114
|
+
const setCookie = ctx.context.responseHeaders?.get("set-cookie");
|
|
115
|
+
if (!setCookie) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
ctx.context.responseHeaders?.delete("set-cookie");
|
|
119
|
+
ctx.setHeader("Set-Better-Auth-Cookie", setCookie);
|
|
120
|
+
}),
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
matcher: (ctx) => {
|
|
124
|
+
return ((ctx.path?.startsWith("/callback") ||
|
|
125
|
+
ctx.path?.startsWith("/oauth2/callback") ||
|
|
126
|
+
ctx.path?.startsWith("/magic-link/verify")) &&
|
|
127
|
+
!isExpoNative(ctx));
|
|
128
|
+
},
|
|
129
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
130
|
+
// Mostly copied from the one-time-token plugin
|
|
131
|
+
const session = ctx.context.newSession;
|
|
132
|
+
if (!session) {
|
|
133
|
+
ctx.context.logger.error("No session found");
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const token = generateRandomString(32);
|
|
137
|
+
const expiresAt = new Date(Date.now() + 3 * 60 * 1000);
|
|
138
|
+
await ctx.context.internalAdapter.createVerificationValue({
|
|
139
|
+
value: session.session.token,
|
|
140
|
+
identifier: `one-time-token:${token}`,
|
|
141
|
+
expiresAt,
|
|
142
|
+
});
|
|
143
|
+
const redirectTo = ctx.context.responseHeaders?.get("location");
|
|
144
|
+
if (!redirectTo) {
|
|
145
|
+
ctx.context.logger.error("No redirect to found");
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const url = new URL(redirectTo);
|
|
149
|
+
url.searchParams.set("ott", token);
|
|
150
|
+
throw ctx.redirect(url.toString());
|
|
151
|
+
}),
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
endpoints: {
|
|
156
|
+
verifyOneTimeToken: createAuthEndpoint("/cross-domain/one-time-token/verify", {
|
|
157
|
+
method: "POST",
|
|
158
|
+
body: z.object({
|
|
159
|
+
token: z.string(),
|
|
160
|
+
}),
|
|
161
|
+
}, async (ctx) => {
|
|
162
|
+
const response = await oneTimeToken.endpoints.verifyOneTimeToken({
|
|
163
|
+
...ctx,
|
|
164
|
+
returnHeaders: false,
|
|
165
|
+
returnStatus: false,
|
|
166
|
+
});
|
|
167
|
+
await setSessionCookie(ctx, response);
|
|
168
|
+
return response;
|
|
169
|
+
}),
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/plugins/cross-domain/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,YAAY,IAAI,kBAAkB,GACnC,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,EAAE,OAAO,EAAuB,EAAE,EAAE;IAC9D,MAAM,YAAY,GAAG,kBAAkB,EAAE,CAAC;IAE1C,MAAM,kBAAkB,GAAG,CAAC,WAAoB,EAAE,EAAE;QAClD,IAAI,WAAW,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YAChD,OAAO,WAAW,CAAC;QACrB,CAAC;QACD,MAAM,mBAAmB,GAAG,WAAW,IAAI,GAAG,CAAC;QAC/C,OAAO,IAAI,GAAG,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC;IAC1D,CAAC,CAAC;IAEF,MAAM,YAAY,GAAG,CAAC,GAA0B,EAAE,EAAE;QAClD,OAAO,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,aAAa,CAAC,CAAC;IACzC,CAAC,CAAC;IAEF,OAAO;QACL,EAAE,EAAE,cAAc;QAClB,0DAA0D;QAC1D,0DAA0D;QAC1D,2DAA2D;QAC3D,qBAAqB;QACrB,IAAI;YACF,OAAO;gBACL,OAAO,EAAE;oBACP,cAAc,EAAE,CAAC,OAAO,CAAC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kB,EAAE,kBAAkB,CACpC,qCAAqC,EACrC;gBACE,MAAM,EAAE,MAAM;gBACd,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC;oBACb,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;iBAClB,CAAC;aACH,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;gBACZ,MAAM,QAAQ,GAAG,MAAM,YAAY,CAAC,SAAS,CAAC,kBAAkB,CAAC;oBAC/D,GAAG,GAAG;oBACN,aAAa,EAAE,KAAK;oBACpB,YAAY,EAAE,KAAK;iBACpB,CAAC,CAAC;gBACH,MAAM,gBAAgB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;gBACtC,OAAO,QAAQ,CAAC;YAClB,CAAC,CACF;SACF;KACyB,CAAC;AAC/B,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugins/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/plugins/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { PropsWithChildren, ReactNode } from "react";
|
|
2
|
+
import type { AuthTokenFetcher } from "convex/browser";
|
|
3
|
+
import type { FunctionReference } from "convex/server";
|
|
4
|
+
import type { BetterAuthClientPlugin } from "better-auth";
|
|
5
|
+
import type { createAuthClient } from "better-auth/react";
|
|
6
|
+
import type { convexClient, crossDomainClient } from "../client/plugins/index.js";
|
|
7
|
+
import type { EmptyObject } from "convex-helpers";
|
|
8
|
+
type CrossDomainClient = ReturnType<typeof crossDomainClient>;
|
|
9
|
+
type ConvexClient = ReturnType<typeof convexClient>;
|
|
10
|
+
type PluginsWithCrossDomain = (CrossDomainClient | ConvexClient | BetterAuthClientPlugin)[];
|
|
11
|
+
type PluginsWithoutCrossDomain = (ConvexClient | BetterAuthClientPlugin)[];
|
|
12
|
+
type AuthClientWithPlugins<Plugins extends PluginsWithCrossDomain | PluginsWithoutCrossDomain> = ReturnType<typeof createAuthClient<BetterAuthClientPlugin & {
|
|
13
|
+
plugins: Plugins;
|
|
14
|
+
}>>;
|
|
15
|
+
export type AuthClient = AuthClientWithPlugins<PluginsWithCrossDomain> | AuthClientWithPlugins<PluginsWithoutCrossDomain>;
|
|
16
|
+
type IConvexReactClient = {
|
|
17
|
+
setAuth(fetchToken: AuthTokenFetcher): void;
|
|
18
|
+
clearAuth(): void;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* A wrapper React component which provides a {@link react.ConvexReactClient}
|
|
22
|
+
* authenticated with Better Auth.
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export declare function ConvexBetterAuthProvider({ children, client, authClient, initialToken, }: {
|
|
27
|
+
children: ReactNode;
|
|
28
|
+
client: IConvexReactClient;
|
|
29
|
+
authClient: AuthClient;
|
|
30
|
+
initialToken?: string | null;
|
|
31
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
32
|
+
/**
|
|
33
|
+
* _Experimental_
|
|
34
|
+
*
|
|
35
|
+
* A wrapper React component which provides error handling for auth related errors.
|
|
36
|
+
* This is typically used to redirect the user to the login page when they are
|
|
37
|
+
* unauthenticated, and does so reactively based on the getAuthUserFn query.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* // convex/auth.ts
|
|
42
|
+
* export const { getAuthUser } = authComponent.clientApi();
|
|
43
|
+
*
|
|
44
|
+
* // auth-client.tsx
|
|
45
|
+
* import { AuthBoundary } from "@convex-dev/react";
|
|
46
|
+
* import { api } from '../../convex/_generated/api'
|
|
47
|
+
* import { isAuthError } from '../lib/utils'
|
|
48
|
+
*
|
|
49
|
+
* export const ClientAuthBoundary = ({ children }: PropsWithChildren) => {
|
|
50
|
+
* return (
|
|
51
|
+
* <AuthBoundary
|
|
52
|
+
* onUnauth={() => redirect("/sign-in")}
|
|
53
|
+
* authClient={authClient}
|
|
54
|
+
* getAuthUserFn={api.auth.getAuthUser}
|
|
55
|
+
* isAuthError={isAuthError}
|
|
56
|
+
* >
|
|
57
|
+
* <>{children}</>
|
|
58
|
+
* </AuthBoundary>
|
|
59
|
+
* )
|
|
60
|
+
* ```
|
|
61
|
+
* @param props.children - Children to render.
|
|
62
|
+
* @param props.onUnauth - Function to call when the user is
|
|
63
|
+
* unauthenticated. Typically a redirect to the login page.
|
|
64
|
+
* @param props.authClient - Better Auth authClient to use.
|
|
65
|
+
* @param props.renderFallback - Fallback component to render when the user is
|
|
66
|
+
* unauthenticated. Defaults to null. Generally not rendered as error handling
|
|
67
|
+
* is typically a redirect.
|
|
68
|
+
* @param props.getAuthUserFn - Reference to a Convex query that returns user.
|
|
69
|
+
* The component provides a query for this via `export const { getAuthUser } = authComponent.clientApi()`.
|
|
70
|
+
* @param props.isAuthError - Function to check if the error is auth related.
|
|
71
|
+
*/
|
|
72
|
+
export declare const AuthBoundary: ({ children, onUnauth, authClient, renderFallback, getAuthUserFn, isAuthError, }: PropsWithChildren<{
|
|
73
|
+
onUnauth: () => void | Promise<void>;
|
|
74
|
+
authClient: AuthClient;
|
|
75
|
+
renderFallback?: () => React.ReactNode;
|
|
76
|
+
getAuthUserFn: FunctionReference<"query", "public", EmptyObject>;
|
|
77
|
+
isAuthError: (error: unknown) => boolean;
|
|
78
|
+
}>) => import("react/jsx-runtime").JSX.Element;
|
|
79
|
+
export {};
|
|
80
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,OAAO,CA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