better-auth 0.4.9-beta.6 → 0.4.9-beta.7
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/api.js +80 -97
- package/dist/index.js +84 -101
- package/dist/plugins.js +85 -102
- package/package.json +1 -1
package/dist/api.js
CHANGED
|
@@ -1074,39 +1074,39 @@ var signInOAuth = createAuthEndpoint(
|
|
|
1074
1074
|
}),
|
|
1075
1075
|
use: [redirectURLMiddleware]
|
|
1076
1076
|
},
|
|
1077
|
-
async (
|
|
1078
|
-
const provider =
|
|
1079
|
-
(p) => p.id ===
|
|
1077
|
+
async (c) => {
|
|
1078
|
+
const provider = c.context.socialProviders.find(
|
|
1079
|
+
(p) => p.id === c.body.provider
|
|
1080
1080
|
);
|
|
1081
1081
|
if (!provider) {
|
|
1082
|
-
|
|
1082
|
+
c.context.logger.error(
|
|
1083
1083
|
"Provider not found. Make sure to add the provider to your auth config",
|
|
1084
1084
|
{
|
|
1085
|
-
provider:
|
|
1085
|
+
provider: c.body.provider
|
|
1086
1086
|
}
|
|
1087
1087
|
);
|
|
1088
1088
|
throw new APIError5("NOT_FOUND", {
|
|
1089
1089
|
message: "Provider not found"
|
|
1090
1090
|
});
|
|
1091
1091
|
}
|
|
1092
|
-
const cookie =
|
|
1093
|
-
const currentURL =
|
|
1094
|
-
const callbackURL =
|
|
1092
|
+
const cookie = c.context.authCookies;
|
|
1093
|
+
const currentURL = c.query?.currentURL ? new URL(c.query?.currentURL) : null;
|
|
1094
|
+
const callbackURL = c.body.callbackURL?.startsWith("http") ? c.body.callbackURL : `${currentURL?.origin}${c.body.callbackURL || ""}`;
|
|
1095
1095
|
const state = generateState(
|
|
1096
|
-
callbackURL || currentURL?.origin ||
|
|
1097
|
-
|
|
1096
|
+
callbackURL || currentURL?.origin || c.context.baseURL,
|
|
1097
|
+
c.query?.currentURL
|
|
1098
1098
|
);
|
|
1099
|
-
await
|
|
1099
|
+
await c.setSignedCookie(
|
|
1100
1100
|
cookie.state.name,
|
|
1101
1101
|
state,
|
|
1102
|
-
|
|
1102
|
+
c.context.secret,
|
|
1103
1103
|
cookie.state.options
|
|
1104
1104
|
);
|
|
1105
1105
|
const codeVerifier = generateCodeVerifier();
|
|
1106
|
-
await
|
|
1106
|
+
await c.setSignedCookie(
|
|
1107
1107
|
cookie.pkCodeVerifier.name,
|
|
1108
1108
|
codeVerifier,
|
|
1109
|
-
|
|
1109
|
+
c.context.secret,
|
|
1110
1110
|
cookie.pkCodeVerifier.options
|
|
1111
1111
|
);
|
|
1112
1112
|
const url = await provider.createAuthorizationURL({
|
|
@@ -1115,9 +1115,9 @@ var signInOAuth = createAuthEndpoint(
|
|
|
1115
1115
|
});
|
|
1116
1116
|
url.searchParams.set(
|
|
1117
1117
|
"redirect_uri",
|
|
1118
|
-
`${
|
|
1118
|
+
`${c.context.baseURL}/callback/${c.body.provider}`
|
|
1119
1119
|
);
|
|
1120
|
-
return
|
|
1120
|
+
return c.json({
|
|
1121
1121
|
url: url.toString(),
|
|
1122
1122
|
state,
|
|
1123
1123
|
codeVerifier,
|
|
@@ -1150,12 +1150,6 @@ var signInEmail = createAuthEndpoint(
|
|
|
1150
1150
|
message: "Email and password is not enabled"
|
|
1151
1151
|
});
|
|
1152
1152
|
}
|
|
1153
|
-
const currentSession = await getSessionFromCtx(ctx);
|
|
1154
|
-
if (currentSession) {
|
|
1155
|
-
await ctx.context.internalAdapter.deleteSession(
|
|
1156
|
-
currentSession.session.id
|
|
1157
|
-
);
|
|
1158
|
-
}
|
|
1159
1153
|
const { email, password } = ctx.body;
|
|
1160
1154
|
const checkEmail = z4.string().email().safeParse(email);
|
|
1161
1155
|
if (!checkEmail.success) {
|
|
@@ -1219,17 +1213,6 @@ var signInEmail = createAuthEndpoint(
|
|
|
1219
1213
|
});
|
|
1220
1214
|
}
|
|
1221
1215
|
);
|
|
1222
|
-
var c = (o) => {
|
|
1223
|
-
};
|
|
1224
|
-
c({
|
|
1225
|
-
additional: {
|
|
1226
|
-
name: "string"
|
|
1227
|
-
},
|
|
1228
|
-
hooks: {
|
|
1229
|
-
create(user) {
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
});
|
|
1233
1216
|
|
|
1234
1217
|
// src/api/routes/callback.ts
|
|
1235
1218
|
import { APIError as APIError6 } from "better-call";
|
|
@@ -1400,63 +1383,63 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1400
1383
|
}),
|
|
1401
1384
|
metadata: HIDE_METADATA
|
|
1402
1385
|
},
|
|
1403
|
-
async (
|
|
1404
|
-
if (
|
|
1405
|
-
const parsedState2 = parseState(
|
|
1406
|
-
const callbackURL2 = parsedState2.data?.callbackURL || `${
|
|
1407
|
-
|
|
1408
|
-
throw
|
|
1409
|
-
`${callbackURL2}?error=${
|
|
1386
|
+
async (c) => {
|
|
1387
|
+
if (c.query.error || !c.query.code) {
|
|
1388
|
+
const parsedState2 = parseState(c.query.state);
|
|
1389
|
+
const callbackURL2 = parsedState2.data?.callbackURL || `${c.context.baseURL}/error`;
|
|
1390
|
+
c.context.logger.error(c.query.error, c.params.id);
|
|
1391
|
+
throw c.redirect(
|
|
1392
|
+
`${callbackURL2}?error=${c.query.error || "oAuth_code_missing"}`
|
|
1410
1393
|
);
|
|
1411
1394
|
}
|
|
1412
|
-
const provider =
|
|
1413
|
-
(p) => p.id ===
|
|
1395
|
+
const provider = c.context.socialProviders.find(
|
|
1396
|
+
(p) => p.id === c.params.id
|
|
1414
1397
|
);
|
|
1415
1398
|
if (!provider) {
|
|
1416
|
-
|
|
1399
|
+
c.context.logger.error(
|
|
1417
1400
|
"Oauth provider with id",
|
|
1418
|
-
|
|
1401
|
+
c.params.id,
|
|
1419
1402
|
"not found"
|
|
1420
1403
|
);
|
|
1421
|
-
throw
|
|
1422
|
-
`${
|
|
1404
|
+
throw c.redirect(
|
|
1405
|
+
`${c.context.baseURL}/error?error=oauth_provider_not_found`
|
|
1423
1406
|
);
|
|
1424
1407
|
}
|
|
1425
|
-
const parsedState = parseState(
|
|
1408
|
+
const parsedState = parseState(c.query.state);
|
|
1426
1409
|
if (!parsedState.success) {
|
|
1427
|
-
|
|
1428
|
-
throw
|
|
1429
|
-
`${
|
|
1410
|
+
c.context.logger.error("Unable to parse state");
|
|
1411
|
+
throw c.redirect(
|
|
1412
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1430
1413
|
);
|
|
1431
1414
|
}
|
|
1432
1415
|
const {
|
|
1433
1416
|
data: { callbackURL, currentURL }
|
|
1434
1417
|
} = parsedState;
|
|
1435
|
-
const storedState = await
|
|
1436
|
-
|
|
1437
|
-
|
|
1418
|
+
const storedState = await c.getSignedCookie(
|
|
1419
|
+
c.context.authCookies.state.name,
|
|
1420
|
+
c.context.secret
|
|
1438
1421
|
);
|
|
1439
|
-
if (storedState !==
|
|
1422
|
+
if (storedState !== c.query.state) {
|
|
1440
1423
|
logger.error("OAuth state mismatch");
|
|
1441
|
-
throw
|
|
1442
|
-
`${
|
|
1424
|
+
throw c.redirect(
|
|
1425
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1443
1426
|
);
|
|
1444
1427
|
}
|
|
1445
|
-
const codeVerifier = await
|
|
1446
|
-
|
|
1447
|
-
|
|
1428
|
+
const codeVerifier = await c.getSignedCookie(
|
|
1429
|
+
c.context.authCookies.pkCodeVerifier.name,
|
|
1430
|
+
c.context.secret
|
|
1448
1431
|
);
|
|
1449
1432
|
let tokens;
|
|
1450
1433
|
try {
|
|
1451
1434
|
tokens = await provider.validateAuthorizationCode(
|
|
1452
|
-
|
|
1435
|
+
c.query.code,
|
|
1453
1436
|
codeVerifier,
|
|
1454
|
-
`${
|
|
1437
|
+
`${c.context.baseURL}/callback/${provider.id}`
|
|
1455
1438
|
);
|
|
1456
1439
|
} catch (e) {
|
|
1457
|
-
|
|
1458
|
-
throw
|
|
1459
|
-
`${
|
|
1440
|
+
c.context.logger.error(e);
|
|
1441
|
+
throw c.redirect(
|
|
1442
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1460
1443
|
);
|
|
1461
1444
|
}
|
|
1462
1445
|
const user = await provider.getUserInfo(tokens).then((res) => res?.user);
|
|
@@ -1467,24 +1450,24 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1467
1450
|
});
|
|
1468
1451
|
if (!user || data.success === false) {
|
|
1469
1452
|
logger.error("Unable to get user info", data.error);
|
|
1470
|
-
throw
|
|
1471
|
-
`${
|
|
1453
|
+
throw c.redirect(
|
|
1454
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1472
1455
|
);
|
|
1473
1456
|
}
|
|
1474
1457
|
if (!callbackURL) {
|
|
1475
|
-
throw
|
|
1476
|
-
`${
|
|
1458
|
+
throw c.redirect(
|
|
1459
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1477
1460
|
);
|
|
1478
1461
|
}
|
|
1479
|
-
const dbUser = await
|
|
1462
|
+
const dbUser = await c.context.internalAdapter.findUserByEmail(user.email, {
|
|
1480
1463
|
includeAccounts: true
|
|
1481
1464
|
}).catch((e) => {
|
|
1482
1465
|
logger.error(
|
|
1483
1466
|
"Better auth was unable to query your database.\nError: ",
|
|
1484
1467
|
e
|
|
1485
1468
|
);
|
|
1486
|
-
throw
|
|
1487
|
-
`${
|
|
1469
|
+
throw c.redirect(
|
|
1470
|
+
`${c.context.baseURL}/error?error=internal_server_error`
|
|
1488
1471
|
);
|
|
1489
1472
|
});
|
|
1490
1473
|
const userId = dbUser?.user.id;
|
|
@@ -1492,7 +1475,7 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1492
1475
|
const hasBeenLinked = dbUser.accounts.find(
|
|
1493
1476
|
(a) => a.providerId === provider.id
|
|
1494
1477
|
);
|
|
1495
|
-
const trustedProviders =
|
|
1478
|
+
const trustedProviders = c.context.options.account?.accountLinking?.trustedProviders;
|
|
1496
1479
|
const isTrustedProvider = trustedProviders ? trustedProviders.includes(provider.id) : true;
|
|
1497
1480
|
if (!hasBeenLinked && (!user.emailVerified || !isTrustedProvider)) {
|
|
1498
1481
|
let url;
|
|
@@ -1500,15 +1483,15 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1500
1483
|
url = new URL(currentURL || callbackURL);
|
|
1501
1484
|
url.searchParams.set("error", "account_not_linked");
|
|
1502
1485
|
} catch (e) {
|
|
1503
|
-
throw
|
|
1504
|
-
`${
|
|
1486
|
+
throw c.redirect(
|
|
1487
|
+
`${c.context.baseURL}/error?error=account_not_linked`
|
|
1505
1488
|
);
|
|
1506
1489
|
}
|
|
1507
|
-
throw
|
|
1490
|
+
throw c.redirect(url.toString());
|
|
1508
1491
|
}
|
|
1509
1492
|
if (!hasBeenLinked) {
|
|
1510
1493
|
try {
|
|
1511
|
-
await
|
|
1494
|
+
await c.context.internalAdapter.linkAccount({
|
|
1512
1495
|
providerId: provider.id,
|
|
1513
1496
|
accountId: user.id.toString(),
|
|
1514
1497
|
id: `${provider.id}:${user.id}`,
|
|
@@ -1517,14 +1500,14 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1517
1500
|
});
|
|
1518
1501
|
} catch (e) {
|
|
1519
1502
|
console.log(e);
|
|
1520
|
-
throw
|
|
1521
|
-
`${
|
|
1503
|
+
throw c.redirect(
|
|
1504
|
+
`${c.context.baseURL}/error?error=failed_linking_account`
|
|
1522
1505
|
);
|
|
1523
1506
|
}
|
|
1524
1507
|
}
|
|
1525
1508
|
} else {
|
|
1526
1509
|
try {
|
|
1527
|
-
await
|
|
1510
|
+
await c.context.internalAdapter.createOAuthUser(data.data, {
|
|
1528
1511
|
...getAccountTokens(tokens),
|
|
1529
1512
|
id: `${provider.id}:${user.id}`,
|
|
1530
1513
|
providerId: provider.id,
|
|
@@ -1534,8 +1517,8 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1534
1517
|
} catch (e) {
|
|
1535
1518
|
const url = new URL(currentURL || callbackURL);
|
|
1536
1519
|
url.searchParams.set("error", "unable_to_create_user");
|
|
1537
|
-
|
|
1538
|
-
throw
|
|
1520
|
+
c.setHeader("Location", url.toString());
|
|
1521
|
+
throw c.redirect(url.toString());
|
|
1539
1522
|
}
|
|
1540
1523
|
}
|
|
1541
1524
|
if (!userId && !id)
|
|
@@ -1543,29 +1526,29 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1543
1526
|
message: "Unable to create user"
|
|
1544
1527
|
});
|
|
1545
1528
|
try {
|
|
1546
|
-
const session = await
|
|
1529
|
+
const session = await c.context.internalAdapter.createSession(
|
|
1547
1530
|
userId || id,
|
|
1548
|
-
|
|
1531
|
+
c.request
|
|
1549
1532
|
);
|
|
1550
1533
|
if (!session) {
|
|
1551
1534
|
const url = new URL(currentURL || callbackURL);
|
|
1552
1535
|
url.searchParams.set("error", "unable_to_create_session");
|
|
1553
|
-
throw
|
|
1536
|
+
throw c.redirect(url.toString());
|
|
1554
1537
|
}
|
|
1555
1538
|
try {
|
|
1556
|
-
await setSessionCookie(
|
|
1539
|
+
await setSessionCookie(c, session.id);
|
|
1557
1540
|
} catch (e) {
|
|
1558
|
-
|
|
1541
|
+
c.context.logger.error("Unable to set session cookie", e);
|
|
1559
1542
|
const url = new URL(currentURL || callbackURL);
|
|
1560
1543
|
url.searchParams.set("error", "unable_to_create_session");
|
|
1561
|
-
throw
|
|
1544
|
+
throw c.redirect(url.toString());
|
|
1562
1545
|
}
|
|
1563
1546
|
} catch {
|
|
1564
1547
|
const url = new URL(currentURL || callbackURL || "");
|
|
1565
1548
|
url.searchParams.set("error", "unable_to_create_session");
|
|
1566
|
-
throw
|
|
1549
|
+
throw c.redirect(url.toString());
|
|
1567
1550
|
}
|
|
1568
|
-
throw
|
|
1551
|
+
throw c.redirect(callbackURL);
|
|
1569
1552
|
}
|
|
1570
1553
|
);
|
|
1571
1554
|
|
|
@@ -2222,8 +2205,8 @@ var error = createAuthEndpoint(
|
|
|
2222
2205
|
method: "GET",
|
|
2223
2206
|
metadata: HIDE_METADATA
|
|
2224
2207
|
},
|
|
2225
|
-
async (
|
|
2226
|
-
const query = new URL(
|
|
2208
|
+
async (c) => {
|
|
2209
|
+
const query = new URL(c.request?.url || "").searchParams.get("error") || "Unknown";
|
|
2227
2210
|
return new Response(html(query), {
|
|
2228
2211
|
headers: {
|
|
2229
2212
|
"Content-Type": "text/html"
|
|
@@ -2617,26 +2600,26 @@ function getEndpoints(ctx, options) {
|
|
|
2617
2600
|
let api = {};
|
|
2618
2601
|
for (const [key, value] of Object.entries(endpoints)) {
|
|
2619
2602
|
api[key] = async (context) => {
|
|
2620
|
-
let
|
|
2603
|
+
let c = await ctx;
|
|
2621
2604
|
for (const plugin of options.plugins || []) {
|
|
2622
2605
|
if (plugin.hooks?.before) {
|
|
2623
2606
|
for (const hook of plugin.hooks.before) {
|
|
2624
2607
|
const match = hook.matcher({
|
|
2625
2608
|
...value,
|
|
2626
2609
|
...context,
|
|
2627
|
-
context:
|
|
2610
|
+
context: c
|
|
2628
2611
|
});
|
|
2629
2612
|
if (match) {
|
|
2630
2613
|
const hookRes = await hook.handler({
|
|
2631
2614
|
...context,
|
|
2632
2615
|
context: {
|
|
2633
|
-
...
|
|
2616
|
+
...c,
|
|
2634
2617
|
...context.context
|
|
2635
2618
|
}
|
|
2636
2619
|
});
|
|
2637
2620
|
if (hookRes && "context" in hookRes) {
|
|
2638
|
-
|
|
2639
|
-
...
|
|
2621
|
+
c = {
|
|
2622
|
+
...c,
|
|
2640
2623
|
...hookRes.context
|
|
2641
2624
|
};
|
|
2642
2625
|
}
|
|
@@ -2647,7 +2630,7 @@ function getEndpoints(ctx, options) {
|
|
|
2647
2630
|
const endpointRes = await value({
|
|
2648
2631
|
...context,
|
|
2649
2632
|
context: {
|
|
2650
|
-
...
|
|
2633
|
+
...c,
|
|
2651
2634
|
...context.context
|
|
2652
2635
|
}
|
|
2653
2636
|
});
|
package/dist/index.js
CHANGED
|
@@ -1180,39 +1180,39 @@ var signInOAuth = createAuthEndpoint(
|
|
|
1180
1180
|
}),
|
|
1181
1181
|
use: [redirectURLMiddleware]
|
|
1182
1182
|
},
|
|
1183
|
-
async (
|
|
1184
|
-
const provider =
|
|
1185
|
-
(p) => p.id ===
|
|
1183
|
+
async (c) => {
|
|
1184
|
+
const provider = c.context.socialProviders.find(
|
|
1185
|
+
(p) => p.id === c.body.provider
|
|
1186
1186
|
);
|
|
1187
1187
|
if (!provider) {
|
|
1188
|
-
|
|
1188
|
+
c.context.logger.error(
|
|
1189
1189
|
"Provider not found. Make sure to add the provider to your auth config",
|
|
1190
1190
|
{
|
|
1191
|
-
provider:
|
|
1191
|
+
provider: c.body.provider
|
|
1192
1192
|
}
|
|
1193
1193
|
);
|
|
1194
1194
|
throw new APIError5("NOT_FOUND", {
|
|
1195
1195
|
message: "Provider not found"
|
|
1196
1196
|
});
|
|
1197
1197
|
}
|
|
1198
|
-
const cookie =
|
|
1199
|
-
const currentURL =
|
|
1200
|
-
const callbackURL =
|
|
1198
|
+
const cookie = c.context.authCookies;
|
|
1199
|
+
const currentURL = c.query?.currentURL ? new URL(c.query?.currentURL) : null;
|
|
1200
|
+
const callbackURL = c.body.callbackURL?.startsWith("http") ? c.body.callbackURL : `${currentURL?.origin}${c.body.callbackURL || ""}`;
|
|
1201
1201
|
const state = generateState(
|
|
1202
|
-
callbackURL || currentURL?.origin ||
|
|
1203
|
-
|
|
1202
|
+
callbackURL || currentURL?.origin || c.context.baseURL,
|
|
1203
|
+
c.query?.currentURL
|
|
1204
1204
|
);
|
|
1205
|
-
await
|
|
1205
|
+
await c.setSignedCookie(
|
|
1206
1206
|
cookie.state.name,
|
|
1207
1207
|
state,
|
|
1208
|
-
|
|
1208
|
+
c.context.secret,
|
|
1209
1209
|
cookie.state.options
|
|
1210
1210
|
);
|
|
1211
1211
|
const codeVerifier = generateCodeVerifier();
|
|
1212
|
-
await
|
|
1212
|
+
await c.setSignedCookie(
|
|
1213
1213
|
cookie.pkCodeVerifier.name,
|
|
1214
1214
|
codeVerifier,
|
|
1215
|
-
|
|
1215
|
+
c.context.secret,
|
|
1216
1216
|
cookie.pkCodeVerifier.options
|
|
1217
1217
|
);
|
|
1218
1218
|
const url = await provider.createAuthorizationURL({
|
|
@@ -1221,9 +1221,9 @@ var signInOAuth = createAuthEndpoint(
|
|
|
1221
1221
|
});
|
|
1222
1222
|
url.searchParams.set(
|
|
1223
1223
|
"redirect_uri",
|
|
1224
|
-
`${
|
|
1224
|
+
`${c.context.baseURL}/callback/${c.body.provider}`
|
|
1225
1225
|
);
|
|
1226
|
-
return
|
|
1226
|
+
return c.json({
|
|
1227
1227
|
url: url.toString(),
|
|
1228
1228
|
state,
|
|
1229
1229
|
codeVerifier,
|
|
@@ -1256,12 +1256,6 @@ var signInEmail = createAuthEndpoint(
|
|
|
1256
1256
|
message: "Email and password is not enabled"
|
|
1257
1257
|
});
|
|
1258
1258
|
}
|
|
1259
|
-
const currentSession = await getSessionFromCtx(ctx);
|
|
1260
|
-
if (currentSession) {
|
|
1261
|
-
await ctx.context.internalAdapter.deleteSession(
|
|
1262
|
-
currentSession.session.id
|
|
1263
|
-
);
|
|
1264
|
-
}
|
|
1265
1259
|
const { email, password } = ctx.body;
|
|
1266
1260
|
const checkEmail = z4.string().email().safeParse(email);
|
|
1267
1261
|
if (!checkEmail.success) {
|
|
@@ -1325,17 +1319,6 @@ var signInEmail = createAuthEndpoint(
|
|
|
1325
1319
|
});
|
|
1326
1320
|
}
|
|
1327
1321
|
);
|
|
1328
|
-
var c = (o) => {
|
|
1329
|
-
};
|
|
1330
|
-
c({
|
|
1331
|
-
additional: {
|
|
1332
|
-
name: "string"
|
|
1333
|
-
},
|
|
1334
|
-
hooks: {
|
|
1335
|
-
create(user) {
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
});
|
|
1339
1322
|
|
|
1340
1323
|
// src/api/routes/callback.ts
|
|
1341
1324
|
import { APIError as APIError6 } from "better-call";
|
|
@@ -1506,63 +1489,63 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1506
1489
|
}),
|
|
1507
1490
|
metadata: HIDE_METADATA
|
|
1508
1491
|
},
|
|
1509
|
-
async (
|
|
1510
|
-
if (
|
|
1511
|
-
const parsedState2 = parseState(
|
|
1512
|
-
const callbackURL2 = parsedState2.data?.callbackURL || `${
|
|
1513
|
-
|
|
1514
|
-
throw
|
|
1515
|
-
`${callbackURL2}?error=${
|
|
1492
|
+
async (c) => {
|
|
1493
|
+
if (c.query.error || !c.query.code) {
|
|
1494
|
+
const parsedState2 = parseState(c.query.state);
|
|
1495
|
+
const callbackURL2 = parsedState2.data?.callbackURL || `${c.context.baseURL}/error`;
|
|
1496
|
+
c.context.logger.error(c.query.error, c.params.id);
|
|
1497
|
+
throw c.redirect(
|
|
1498
|
+
`${callbackURL2}?error=${c.query.error || "oAuth_code_missing"}`
|
|
1516
1499
|
);
|
|
1517
1500
|
}
|
|
1518
|
-
const provider =
|
|
1519
|
-
(p) => p.id ===
|
|
1501
|
+
const provider = c.context.socialProviders.find(
|
|
1502
|
+
(p) => p.id === c.params.id
|
|
1520
1503
|
);
|
|
1521
1504
|
if (!provider) {
|
|
1522
|
-
|
|
1505
|
+
c.context.logger.error(
|
|
1523
1506
|
"Oauth provider with id",
|
|
1524
|
-
|
|
1507
|
+
c.params.id,
|
|
1525
1508
|
"not found"
|
|
1526
1509
|
);
|
|
1527
|
-
throw
|
|
1528
|
-
`${
|
|
1510
|
+
throw c.redirect(
|
|
1511
|
+
`${c.context.baseURL}/error?error=oauth_provider_not_found`
|
|
1529
1512
|
);
|
|
1530
1513
|
}
|
|
1531
|
-
const parsedState = parseState(
|
|
1514
|
+
const parsedState = parseState(c.query.state);
|
|
1532
1515
|
if (!parsedState.success) {
|
|
1533
|
-
|
|
1534
|
-
throw
|
|
1535
|
-
`${
|
|
1516
|
+
c.context.logger.error("Unable to parse state");
|
|
1517
|
+
throw c.redirect(
|
|
1518
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1536
1519
|
);
|
|
1537
1520
|
}
|
|
1538
1521
|
const {
|
|
1539
1522
|
data: { callbackURL, currentURL }
|
|
1540
1523
|
} = parsedState;
|
|
1541
|
-
const storedState = await
|
|
1542
|
-
|
|
1543
|
-
|
|
1524
|
+
const storedState = await c.getSignedCookie(
|
|
1525
|
+
c.context.authCookies.state.name,
|
|
1526
|
+
c.context.secret
|
|
1544
1527
|
);
|
|
1545
|
-
if (storedState !==
|
|
1528
|
+
if (storedState !== c.query.state) {
|
|
1546
1529
|
logger.error("OAuth state mismatch");
|
|
1547
|
-
throw
|
|
1548
|
-
`${
|
|
1530
|
+
throw c.redirect(
|
|
1531
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1549
1532
|
);
|
|
1550
1533
|
}
|
|
1551
|
-
const codeVerifier = await
|
|
1552
|
-
|
|
1553
|
-
|
|
1534
|
+
const codeVerifier = await c.getSignedCookie(
|
|
1535
|
+
c.context.authCookies.pkCodeVerifier.name,
|
|
1536
|
+
c.context.secret
|
|
1554
1537
|
);
|
|
1555
1538
|
let tokens;
|
|
1556
1539
|
try {
|
|
1557
1540
|
tokens = await provider.validateAuthorizationCode(
|
|
1558
|
-
|
|
1541
|
+
c.query.code,
|
|
1559
1542
|
codeVerifier,
|
|
1560
|
-
`${
|
|
1543
|
+
`${c.context.baseURL}/callback/${provider.id}`
|
|
1561
1544
|
);
|
|
1562
1545
|
} catch (e) {
|
|
1563
|
-
|
|
1564
|
-
throw
|
|
1565
|
-
`${
|
|
1546
|
+
c.context.logger.error(e);
|
|
1547
|
+
throw c.redirect(
|
|
1548
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1566
1549
|
);
|
|
1567
1550
|
}
|
|
1568
1551
|
const user = await provider.getUserInfo(tokens).then((res) => res?.user);
|
|
@@ -1573,24 +1556,24 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1573
1556
|
});
|
|
1574
1557
|
if (!user || data.success === false) {
|
|
1575
1558
|
logger.error("Unable to get user info", data.error);
|
|
1576
|
-
throw
|
|
1577
|
-
`${
|
|
1559
|
+
throw c.redirect(
|
|
1560
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1578
1561
|
);
|
|
1579
1562
|
}
|
|
1580
1563
|
if (!callbackURL) {
|
|
1581
|
-
throw
|
|
1582
|
-
`${
|
|
1564
|
+
throw c.redirect(
|
|
1565
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1583
1566
|
);
|
|
1584
1567
|
}
|
|
1585
|
-
const dbUser = await
|
|
1568
|
+
const dbUser = await c.context.internalAdapter.findUserByEmail(user.email, {
|
|
1586
1569
|
includeAccounts: true
|
|
1587
1570
|
}).catch((e) => {
|
|
1588
1571
|
logger.error(
|
|
1589
1572
|
"Better auth was unable to query your database.\nError: ",
|
|
1590
1573
|
e
|
|
1591
1574
|
);
|
|
1592
|
-
throw
|
|
1593
|
-
`${
|
|
1575
|
+
throw c.redirect(
|
|
1576
|
+
`${c.context.baseURL}/error?error=internal_server_error`
|
|
1594
1577
|
);
|
|
1595
1578
|
});
|
|
1596
1579
|
const userId = dbUser?.user.id;
|
|
@@ -1598,7 +1581,7 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1598
1581
|
const hasBeenLinked = dbUser.accounts.find(
|
|
1599
1582
|
(a) => a.providerId === provider.id
|
|
1600
1583
|
);
|
|
1601
|
-
const trustedProviders =
|
|
1584
|
+
const trustedProviders = c.context.options.account?.accountLinking?.trustedProviders;
|
|
1602
1585
|
const isTrustedProvider = trustedProviders ? trustedProviders.includes(provider.id) : true;
|
|
1603
1586
|
if (!hasBeenLinked && (!user.emailVerified || !isTrustedProvider)) {
|
|
1604
1587
|
let url;
|
|
@@ -1606,15 +1589,15 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1606
1589
|
url = new URL(currentURL || callbackURL);
|
|
1607
1590
|
url.searchParams.set("error", "account_not_linked");
|
|
1608
1591
|
} catch (e) {
|
|
1609
|
-
throw
|
|
1610
|
-
`${
|
|
1592
|
+
throw c.redirect(
|
|
1593
|
+
`${c.context.baseURL}/error?error=account_not_linked`
|
|
1611
1594
|
);
|
|
1612
1595
|
}
|
|
1613
|
-
throw
|
|
1596
|
+
throw c.redirect(url.toString());
|
|
1614
1597
|
}
|
|
1615
1598
|
if (!hasBeenLinked) {
|
|
1616
1599
|
try {
|
|
1617
|
-
await
|
|
1600
|
+
await c.context.internalAdapter.linkAccount({
|
|
1618
1601
|
providerId: provider.id,
|
|
1619
1602
|
accountId: user.id.toString(),
|
|
1620
1603
|
id: `${provider.id}:${user.id}`,
|
|
@@ -1623,14 +1606,14 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1623
1606
|
});
|
|
1624
1607
|
} catch (e) {
|
|
1625
1608
|
console.log(e);
|
|
1626
|
-
throw
|
|
1627
|
-
`${
|
|
1609
|
+
throw c.redirect(
|
|
1610
|
+
`${c.context.baseURL}/error?error=failed_linking_account`
|
|
1628
1611
|
);
|
|
1629
1612
|
}
|
|
1630
1613
|
}
|
|
1631
1614
|
} else {
|
|
1632
1615
|
try {
|
|
1633
|
-
await
|
|
1616
|
+
await c.context.internalAdapter.createOAuthUser(data.data, {
|
|
1634
1617
|
...getAccountTokens(tokens),
|
|
1635
1618
|
id: `${provider.id}:${user.id}`,
|
|
1636
1619
|
providerId: provider.id,
|
|
@@ -1640,8 +1623,8 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1640
1623
|
} catch (e) {
|
|
1641
1624
|
const url = new URL(currentURL || callbackURL);
|
|
1642
1625
|
url.searchParams.set("error", "unable_to_create_user");
|
|
1643
|
-
|
|
1644
|
-
throw
|
|
1626
|
+
c.setHeader("Location", url.toString());
|
|
1627
|
+
throw c.redirect(url.toString());
|
|
1645
1628
|
}
|
|
1646
1629
|
}
|
|
1647
1630
|
if (!userId && !id)
|
|
@@ -1649,29 +1632,29 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1649
1632
|
message: "Unable to create user"
|
|
1650
1633
|
});
|
|
1651
1634
|
try {
|
|
1652
|
-
const session = await
|
|
1635
|
+
const session = await c.context.internalAdapter.createSession(
|
|
1653
1636
|
userId || id,
|
|
1654
|
-
|
|
1637
|
+
c.request
|
|
1655
1638
|
);
|
|
1656
1639
|
if (!session) {
|
|
1657
1640
|
const url = new URL(currentURL || callbackURL);
|
|
1658
1641
|
url.searchParams.set("error", "unable_to_create_session");
|
|
1659
|
-
throw
|
|
1642
|
+
throw c.redirect(url.toString());
|
|
1660
1643
|
}
|
|
1661
1644
|
try {
|
|
1662
|
-
await setSessionCookie(
|
|
1645
|
+
await setSessionCookie(c, session.id);
|
|
1663
1646
|
} catch (e) {
|
|
1664
|
-
|
|
1647
|
+
c.context.logger.error("Unable to set session cookie", e);
|
|
1665
1648
|
const url = new URL(currentURL || callbackURL);
|
|
1666
1649
|
url.searchParams.set("error", "unable_to_create_session");
|
|
1667
|
-
throw
|
|
1650
|
+
throw c.redirect(url.toString());
|
|
1668
1651
|
}
|
|
1669
1652
|
} catch {
|
|
1670
1653
|
const url = new URL(currentURL || callbackURL || "");
|
|
1671
1654
|
url.searchParams.set("error", "unable_to_create_session");
|
|
1672
|
-
throw
|
|
1655
|
+
throw c.redirect(url.toString());
|
|
1673
1656
|
}
|
|
1674
|
-
throw
|
|
1657
|
+
throw c.redirect(callbackURL);
|
|
1675
1658
|
}
|
|
1676
1659
|
);
|
|
1677
1660
|
|
|
@@ -2328,8 +2311,8 @@ var error = createAuthEndpoint(
|
|
|
2328
2311
|
method: "GET",
|
|
2329
2312
|
metadata: HIDE_METADATA
|
|
2330
2313
|
},
|
|
2331
|
-
async (
|
|
2332
|
-
const query = new URL(
|
|
2314
|
+
async (c) => {
|
|
2315
|
+
const query = new URL(c.request?.url || "").searchParams.get("error") || "Unknown";
|
|
2333
2316
|
return new Response(html(query), {
|
|
2334
2317
|
headers: {
|
|
2335
2318
|
"Content-Type": "text/html"
|
|
@@ -2723,26 +2706,26 @@ function getEndpoints(ctx, options) {
|
|
|
2723
2706
|
let api = {};
|
|
2724
2707
|
for (const [key, value] of Object.entries(endpoints)) {
|
|
2725
2708
|
api[key] = async (context) => {
|
|
2726
|
-
let
|
|
2709
|
+
let c = await ctx;
|
|
2727
2710
|
for (const plugin of options.plugins || []) {
|
|
2728
2711
|
if (plugin.hooks?.before) {
|
|
2729
2712
|
for (const hook of plugin.hooks.before) {
|
|
2730
2713
|
const match = hook.matcher({
|
|
2731
2714
|
...value,
|
|
2732
2715
|
...context,
|
|
2733
|
-
context:
|
|
2716
|
+
context: c
|
|
2734
2717
|
});
|
|
2735
2718
|
if (match) {
|
|
2736
2719
|
const hookRes = await hook.handler({
|
|
2737
2720
|
...context,
|
|
2738
2721
|
context: {
|
|
2739
|
-
...
|
|
2722
|
+
...c,
|
|
2740
2723
|
...context.context
|
|
2741
2724
|
}
|
|
2742
2725
|
});
|
|
2743
2726
|
if (hookRes && "context" in hookRes) {
|
|
2744
|
-
|
|
2745
|
-
...
|
|
2727
|
+
c = {
|
|
2728
|
+
...c,
|
|
2746
2729
|
...hookRes.context
|
|
2747
2730
|
};
|
|
2748
2731
|
}
|
|
@@ -2753,7 +2736,7 @@ function getEndpoints(ctx, options) {
|
|
|
2753
2736
|
const endpointRes = await value({
|
|
2754
2737
|
...context,
|
|
2755
2738
|
context: {
|
|
2756
|
-
...
|
|
2739
|
+
...c,
|
|
2757
2740
|
...context.context
|
|
2758
2741
|
}
|
|
2759
2742
|
});
|
|
@@ -3278,7 +3261,7 @@ async function getMigrations(config2) {
|
|
|
3278
3261
|
}
|
|
3279
3262
|
let toBeAddedFields = {};
|
|
3280
3263
|
for (const [fieldName, field] of Object.entries(value.fields)) {
|
|
3281
|
-
const column = table.columns.find((
|
|
3264
|
+
const column = table.columns.find((c) => c.name === fieldName);
|
|
3282
3265
|
if (!column) {
|
|
3283
3266
|
toBeAddedFields[fieldName] = field;
|
|
3284
3267
|
continue;
|
|
@@ -3605,11 +3588,11 @@ function constantTimeEqual(a, b) {
|
|
|
3605
3588
|
if (aBuffer.length !== bBuffer.length) {
|
|
3606
3589
|
return false;
|
|
3607
3590
|
}
|
|
3608
|
-
let
|
|
3591
|
+
let c = 0;
|
|
3609
3592
|
for (let i = 0; i < aBuffer.length; i++) {
|
|
3610
|
-
|
|
3593
|
+
c |= aBuffer[i] ^ bBuffer[i];
|
|
3611
3594
|
}
|
|
3612
|
-
return
|
|
3595
|
+
return c === 0;
|
|
3613
3596
|
}
|
|
3614
3597
|
|
|
3615
3598
|
// src/crypto/password.ts
|
package/dist/plugins.js
CHANGED
|
@@ -995,39 +995,39 @@ var signInOAuth = createAuthEndpoint(
|
|
|
995
995
|
}),
|
|
996
996
|
use: [redirectURLMiddleware]
|
|
997
997
|
},
|
|
998
|
-
async (
|
|
999
|
-
const provider =
|
|
1000
|
-
(p) => p.id ===
|
|
998
|
+
async (c) => {
|
|
999
|
+
const provider = c.context.socialProviders.find(
|
|
1000
|
+
(p) => p.id === c.body.provider
|
|
1001
1001
|
);
|
|
1002
1002
|
if (!provider) {
|
|
1003
|
-
|
|
1003
|
+
c.context.logger.error(
|
|
1004
1004
|
"Provider not found. Make sure to add the provider to your auth config",
|
|
1005
1005
|
{
|
|
1006
|
-
provider:
|
|
1006
|
+
provider: c.body.provider
|
|
1007
1007
|
}
|
|
1008
1008
|
);
|
|
1009
1009
|
throw new APIError4("NOT_FOUND", {
|
|
1010
1010
|
message: "Provider not found"
|
|
1011
1011
|
});
|
|
1012
1012
|
}
|
|
1013
|
-
const cookie =
|
|
1014
|
-
const currentURL =
|
|
1015
|
-
const callbackURL =
|
|
1013
|
+
const cookie = c.context.authCookies;
|
|
1014
|
+
const currentURL = c.query?.currentURL ? new URL(c.query?.currentURL) : null;
|
|
1015
|
+
const callbackURL = c.body.callbackURL?.startsWith("http") ? c.body.callbackURL : `${currentURL?.origin}${c.body.callbackURL || ""}`;
|
|
1016
1016
|
const state = generateState(
|
|
1017
|
-
callbackURL || currentURL?.origin ||
|
|
1018
|
-
|
|
1017
|
+
callbackURL || currentURL?.origin || c.context.baseURL,
|
|
1018
|
+
c.query?.currentURL
|
|
1019
1019
|
);
|
|
1020
|
-
await
|
|
1020
|
+
await c.setSignedCookie(
|
|
1021
1021
|
cookie.state.name,
|
|
1022
1022
|
state,
|
|
1023
|
-
|
|
1023
|
+
c.context.secret,
|
|
1024
1024
|
cookie.state.options
|
|
1025
1025
|
);
|
|
1026
1026
|
const codeVerifier = generateCodeVerifier();
|
|
1027
|
-
await
|
|
1027
|
+
await c.setSignedCookie(
|
|
1028
1028
|
cookie.pkCodeVerifier.name,
|
|
1029
1029
|
codeVerifier,
|
|
1030
|
-
|
|
1030
|
+
c.context.secret,
|
|
1031
1031
|
cookie.pkCodeVerifier.options
|
|
1032
1032
|
);
|
|
1033
1033
|
const url = await provider.createAuthorizationURL({
|
|
@@ -1036,9 +1036,9 @@ var signInOAuth = createAuthEndpoint(
|
|
|
1036
1036
|
});
|
|
1037
1037
|
url.searchParams.set(
|
|
1038
1038
|
"redirect_uri",
|
|
1039
|
-
`${
|
|
1039
|
+
`${c.context.baseURL}/callback/${c.body.provider}`
|
|
1040
1040
|
);
|
|
1041
|
-
return
|
|
1041
|
+
return c.json({
|
|
1042
1042
|
url: url.toString(),
|
|
1043
1043
|
state,
|
|
1044
1044
|
codeVerifier,
|
|
@@ -1071,12 +1071,6 @@ var signInEmail = createAuthEndpoint(
|
|
|
1071
1071
|
message: "Email and password is not enabled"
|
|
1072
1072
|
});
|
|
1073
1073
|
}
|
|
1074
|
-
const currentSession = await getSessionFromCtx(ctx);
|
|
1075
|
-
if (currentSession) {
|
|
1076
|
-
await ctx.context.internalAdapter.deleteSession(
|
|
1077
|
-
currentSession.session.id
|
|
1078
|
-
);
|
|
1079
|
-
}
|
|
1080
1074
|
const { email, password } = ctx.body;
|
|
1081
1075
|
const checkEmail = z3.string().email().safeParse(email);
|
|
1082
1076
|
if (!checkEmail.success) {
|
|
@@ -1140,17 +1134,6 @@ var signInEmail = createAuthEndpoint(
|
|
|
1140
1134
|
});
|
|
1141
1135
|
}
|
|
1142
1136
|
);
|
|
1143
|
-
var c = (o) => {
|
|
1144
|
-
};
|
|
1145
|
-
c({
|
|
1146
|
-
additional: {
|
|
1147
|
-
name: "string"
|
|
1148
|
-
},
|
|
1149
|
-
hooks: {
|
|
1150
|
-
create(user) {
|
|
1151
|
-
}
|
|
1152
|
-
}
|
|
1153
|
-
});
|
|
1154
1137
|
|
|
1155
1138
|
// src/api/routes/callback.ts
|
|
1156
1139
|
import { APIError as APIError5 } from "better-call";
|
|
@@ -1321,63 +1304,63 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1321
1304
|
}),
|
|
1322
1305
|
metadata: HIDE_METADATA
|
|
1323
1306
|
},
|
|
1324
|
-
async (
|
|
1325
|
-
if (
|
|
1326
|
-
const parsedState2 = parseState(
|
|
1327
|
-
const callbackURL2 = parsedState2.data?.callbackURL || `${
|
|
1328
|
-
|
|
1329
|
-
throw
|
|
1330
|
-
`${callbackURL2}?error=${
|
|
1307
|
+
async (c) => {
|
|
1308
|
+
if (c.query.error || !c.query.code) {
|
|
1309
|
+
const parsedState2 = parseState(c.query.state);
|
|
1310
|
+
const callbackURL2 = parsedState2.data?.callbackURL || `${c.context.baseURL}/error`;
|
|
1311
|
+
c.context.logger.error(c.query.error, c.params.id);
|
|
1312
|
+
throw c.redirect(
|
|
1313
|
+
`${callbackURL2}?error=${c.query.error || "oAuth_code_missing"}`
|
|
1331
1314
|
);
|
|
1332
1315
|
}
|
|
1333
|
-
const provider =
|
|
1334
|
-
(p) => p.id ===
|
|
1316
|
+
const provider = c.context.socialProviders.find(
|
|
1317
|
+
(p) => p.id === c.params.id
|
|
1335
1318
|
);
|
|
1336
1319
|
if (!provider) {
|
|
1337
|
-
|
|
1320
|
+
c.context.logger.error(
|
|
1338
1321
|
"Oauth provider with id",
|
|
1339
|
-
|
|
1322
|
+
c.params.id,
|
|
1340
1323
|
"not found"
|
|
1341
1324
|
);
|
|
1342
|
-
throw
|
|
1343
|
-
`${
|
|
1325
|
+
throw c.redirect(
|
|
1326
|
+
`${c.context.baseURL}/error?error=oauth_provider_not_found`
|
|
1344
1327
|
);
|
|
1345
1328
|
}
|
|
1346
|
-
const parsedState = parseState(
|
|
1329
|
+
const parsedState = parseState(c.query.state);
|
|
1347
1330
|
if (!parsedState.success) {
|
|
1348
|
-
|
|
1349
|
-
throw
|
|
1350
|
-
`${
|
|
1331
|
+
c.context.logger.error("Unable to parse state");
|
|
1332
|
+
throw c.redirect(
|
|
1333
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1351
1334
|
);
|
|
1352
1335
|
}
|
|
1353
1336
|
const {
|
|
1354
1337
|
data: { callbackURL, currentURL }
|
|
1355
1338
|
} = parsedState;
|
|
1356
|
-
const storedState = await
|
|
1357
|
-
|
|
1358
|
-
|
|
1339
|
+
const storedState = await c.getSignedCookie(
|
|
1340
|
+
c.context.authCookies.state.name,
|
|
1341
|
+
c.context.secret
|
|
1359
1342
|
);
|
|
1360
|
-
if (storedState !==
|
|
1343
|
+
if (storedState !== c.query.state) {
|
|
1361
1344
|
logger.error("OAuth state mismatch");
|
|
1362
|
-
throw
|
|
1363
|
-
`${
|
|
1345
|
+
throw c.redirect(
|
|
1346
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1364
1347
|
);
|
|
1365
1348
|
}
|
|
1366
|
-
const codeVerifier = await
|
|
1367
|
-
|
|
1368
|
-
|
|
1349
|
+
const codeVerifier = await c.getSignedCookie(
|
|
1350
|
+
c.context.authCookies.pkCodeVerifier.name,
|
|
1351
|
+
c.context.secret
|
|
1369
1352
|
);
|
|
1370
1353
|
let tokens;
|
|
1371
1354
|
try {
|
|
1372
1355
|
tokens = await provider.validateAuthorizationCode(
|
|
1373
|
-
|
|
1356
|
+
c.query.code,
|
|
1374
1357
|
codeVerifier,
|
|
1375
|
-
`${
|
|
1358
|
+
`${c.context.baseURL}/callback/${provider.id}`
|
|
1376
1359
|
);
|
|
1377
1360
|
} catch (e) {
|
|
1378
|
-
|
|
1379
|
-
throw
|
|
1380
|
-
`${
|
|
1361
|
+
c.context.logger.error(e);
|
|
1362
|
+
throw c.redirect(
|
|
1363
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1381
1364
|
);
|
|
1382
1365
|
}
|
|
1383
1366
|
const user = await provider.getUserInfo(tokens).then((res) => res?.user);
|
|
@@ -1388,24 +1371,24 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1388
1371
|
});
|
|
1389
1372
|
if (!user || data.success === false) {
|
|
1390
1373
|
logger.error("Unable to get user info", data.error);
|
|
1391
|
-
throw
|
|
1392
|
-
`${
|
|
1374
|
+
throw c.redirect(
|
|
1375
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1393
1376
|
);
|
|
1394
1377
|
}
|
|
1395
1378
|
if (!callbackURL) {
|
|
1396
|
-
throw
|
|
1397
|
-
`${
|
|
1379
|
+
throw c.redirect(
|
|
1380
|
+
`${c.context.baseURL}/error?error=please_restart_the_process`
|
|
1398
1381
|
);
|
|
1399
1382
|
}
|
|
1400
|
-
const dbUser = await
|
|
1383
|
+
const dbUser = await c.context.internalAdapter.findUserByEmail(user.email, {
|
|
1401
1384
|
includeAccounts: true
|
|
1402
1385
|
}).catch((e) => {
|
|
1403
1386
|
logger.error(
|
|
1404
1387
|
"Better auth was unable to query your database.\nError: ",
|
|
1405
1388
|
e
|
|
1406
1389
|
);
|
|
1407
|
-
throw
|
|
1408
|
-
`${
|
|
1390
|
+
throw c.redirect(
|
|
1391
|
+
`${c.context.baseURL}/error?error=internal_server_error`
|
|
1409
1392
|
);
|
|
1410
1393
|
});
|
|
1411
1394
|
const userId = dbUser?.user.id;
|
|
@@ -1413,7 +1396,7 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1413
1396
|
const hasBeenLinked = dbUser.accounts.find(
|
|
1414
1397
|
(a) => a.providerId === provider.id
|
|
1415
1398
|
);
|
|
1416
|
-
const trustedProviders =
|
|
1399
|
+
const trustedProviders = c.context.options.account?.accountLinking?.trustedProviders;
|
|
1417
1400
|
const isTrustedProvider = trustedProviders ? trustedProviders.includes(provider.id) : true;
|
|
1418
1401
|
if (!hasBeenLinked && (!user.emailVerified || !isTrustedProvider)) {
|
|
1419
1402
|
let url;
|
|
@@ -1421,15 +1404,15 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1421
1404
|
url = new URL(currentURL || callbackURL);
|
|
1422
1405
|
url.searchParams.set("error", "account_not_linked");
|
|
1423
1406
|
} catch (e) {
|
|
1424
|
-
throw
|
|
1425
|
-
`${
|
|
1407
|
+
throw c.redirect(
|
|
1408
|
+
`${c.context.baseURL}/error?error=account_not_linked`
|
|
1426
1409
|
);
|
|
1427
1410
|
}
|
|
1428
|
-
throw
|
|
1411
|
+
throw c.redirect(url.toString());
|
|
1429
1412
|
}
|
|
1430
1413
|
if (!hasBeenLinked) {
|
|
1431
1414
|
try {
|
|
1432
|
-
await
|
|
1415
|
+
await c.context.internalAdapter.linkAccount({
|
|
1433
1416
|
providerId: provider.id,
|
|
1434
1417
|
accountId: user.id.toString(),
|
|
1435
1418
|
id: `${provider.id}:${user.id}`,
|
|
@@ -1438,14 +1421,14 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1438
1421
|
});
|
|
1439
1422
|
} catch (e) {
|
|
1440
1423
|
console.log(e);
|
|
1441
|
-
throw
|
|
1442
|
-
`${
|
|
1424
|
+
throw c.redirect(
|
|
1425
|
+
`${c.context.baseURL}/error?error=failed_linking_account`
|
|
1443
1426
|
);
|
|
1444
1427
|
}
|
|
1445
1428
|
}
|
|
1446
1429
|
} else {
|
|
1447
1430
|
try {
|
|
1448
|
-
await
|
|
1431
|
+
await c.context.internalAdapter.createOAuthUser(data.data, {
|
|
1449
1432
|
...getAccountTokens(tokens),
|
|
1450
1433
|
id: `${provider.id}:${user.id}`,
|
|
1451
1434
|
providerId: provider.id,
|
|
@@ -1455,8 +1438,8 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1455
1438
|
} catch (e) {
|
|
1456
1439
|
const url = new URL(currentURL || callbackURL);
|
|
1457
1440
|
url.searchParams.set("error", "unable_to_create_user");
|
|
1458
|
-
|
|
1459
|
-
throw
|
|
1441
|
+
c.setHeader("Location", url.toString());
|
|
1442
|
+
throw c.redirect(url.toString());
|
|
1460
1443
|
}
|
|
1461
1444
|
}
|
|
1462
1445
|
if (!userId && !id)
|
|
@@ -1464,29 +1447,29 @@ var callbackOAuth = createAuthEndpoint(
|
|
|
1464
1447
|
message: "Unable to create user"
|
|
1465
1448
|
});
|
|
1466
1449
|
try {
|
|
1467
|
-
const session = await
|
|
1450
|
+
const session = await c.context.internalAdapter.createSession(
|
|
1468
1451
|
userId || id,
|
|
1469
|
-
|
|
1452
|
+
c.request
|
|
1470
1453
|
);
|
|
1471
1454
|
if (!session) {
|
|
1472
1455
|
const url = new URL(currentURL || callbackURL);
|
|
1473
1456
|
url.searchParams.set("error", "unable_to_create_session");
|
|
1474
|
-
throw
|
|
1457
|
+
throw c.redirect(url.toString());
|
|
1475
1458
|
}
|
|
1476
1459
|
try {
|
|
1477
|
-
await setSessionCookie(
|
|
1460
|
+
await setSessionCookie(c, session.id);
|
|
1478
1461
|
} catch (e) {
|
|
1479
|
-
|
|
1462
|
+
c.context.logger.error("Unable to set session cookie", e);
|
|
1480
1463
|
const url = new URL(currentURL || callbackURL);
|
|
1481
1464
|
url.searchParams.set("error", "unable_to_create_session");
|
|
1482
|
-
throw
|
|
1465
|
+
throw c.redirect(url.toString());
|
|
1483
1466
|
}
|
|
1484
1467
|
} catch {
|
|
1485
1468
|
const url = new URL(currentURL || callbackURL || "");
|
|
1486
1469
|
url.searchParams.set("error", "unable_to_create_session");
|
|
1487
|
-
throw
|
|
1470
|
+
throw c.redirect(url.toString());
|
|
1488
1471
|
}
|
|
1489
|
-
throw
|
|
1472
|
+
throw c.redirect(callbackURL);
|
|
1490
1473
|
}
|
|
1491
1474
|
);
|
|
1492
1475
|
|
|
@@ -2184,8 +2167,8 @@ var error = createAuthEndpoint(
|
|
|
2184
2167
|
method: "GET",
|
|
2185
2168
|
metadata: HIDE_METADATA
|
|
2186
2169
|
},
|
|
2187
|
-
async (
|
|
2188
|
-
const query = new URL(
|
|
2170
|
+
async (c) => {
|
|
2171
|
+
const query = new URL(c.request?.url || "").searchParams.get("error") || "Unknown";
|
|
2189
2172
|
return new Response(html(query), {
|
|
2190
2173
|
headers: {
|
|
2191
2174
|
"Content-Type": "text/html"
|
|
@@ -5636,8 +5619,8 @@ var bearer = () => {
|
|
|
5636
5619
|
context.request?.headers.get("authorization") || context.headers?.get("authorization")
|
|
5637
5620
|
);
|
|
5638
5621
|
},
|
|
5639
|
-
handler: async (
|
|
5640
|
-
const token =
|
|
5622
|
+
handler: async (c) => {
|
|
5623
|
+
const token = c.request?.headers.get("authorization")?.replace("Bearer ", "") || c.headers?.get("authorization")?.replace("Bearer ", "");
|
|
5641
5624
|
if (!token) {
|
|
5642
5625
|
return;
|
|
5643
5626
|
}
|
|
@@ -5645,22 +5628,22 @@ var bearer = () => {
|
|
|
5645
5628
|
if (token.includes(".")) {
|
|
5646
5629
|
signedToken = token;
|
|
5647
5630
|
} else {
|
|
5648
|
-
signedToken = await serializeSigned("", token,
|
|
5631
|
+
signedToken = await serializeSigned("", token, c.context.secret);
|
|
5649
5632
|
}
|
|
5650
|
-
if (
|
|
5651
|
-
|
|
5633
|
+
if (c.request) {
|
|
5634
|
+
c.request.headers.set(
|
|
5652
5635
|
"cookie",
|
|
5653
|
-
`${
|
|
5636
|
+
`${c.context.authCookies.sessionToken.name}=${signedToken.replace("=", "")}`
|
|
5654
5637
|
);
|
|
5655
5638
|
}
|
|
5656
|
-
if (
|
|
5657
|
-
|
|
5639
|
+
if (c.headers) {
|
|
5640
|
+
c.headers.set(
|
|
5658
5641
|
"cookie",
|
|
5659
|
-
`${
|
|
5642
|
+
`${c.context.authCookies.sessionToken.name}=${signedToken.replace("=", "")}`
|
|
5660
5643
|
);
|
|
5661
5644
|
}
|
|
5662
5645
|
return {
|
|
5663
|
-
context:
|
|
5646
|
+
context: c
|
|
5664
5647
|
};
|
|
5665
5648
|
}
|
|
5666
5649
|
}
|
|
@@ -6708,7 +6691,7 @@ var genericOAuth = (options) => {
|
|
|
6708
6691
|
async (ctx) => {
|
|
6709
6692
|
const { providerId } = ctx.body;
|
|
6710
6693
|
const config = options.config.find(
|
|
6711
|
-
(
|
|
6694
|
+
(c) => c.providerId === providerId
|
|
6712
6695
|
);
|
|
6713
6696
|
if (!config) {
|
|
6714
6697
|
throw new APIError28("BAD_REQUEST", {
|