@syncello/auth 3.4.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3717,7 +3717,7 @@ var resetPasswordRoute = createRoute7({
3717
3717
  });
3718
3718
  var resetPasswordHandler = async (c) => {
3719
3719
  const { token, password, turnstileToken } = c.req.valid("json");
3720
- const database = c.get("db");
3720
+ const { db: database, schema } = getAuthContext(c);
3721
3721
  const env = c.env;
3722
3722
  const turnstileValid = await verifyTurnstileToken(
3723
3723
  turnstileToken,
@@ -3732,7 +3732,18 @@ var resetPasswordHandler = async (c) => {
3732
3732
  if (!passwordValidation.valid) {
3733
3733
  return problems.badRequest(c, passwordValidation.error || "Invalid password");
3734
3734
  }
3735
- const result = await resetPassword(database, token, password, env.PASSWORD_PEPPER_V1, "v1");
3735
+ const result = await resetPassword(
3736
+ database,
3737
+ {
3738
+ users: schema.users,
3739
+ passwordResetTokens: schema.passwordResetTokens,
3740
+ sessions: schema.sessions
3741
+ },
3742
+ token,
3743
+ password,
3744
+ env.PASSWORD_PEPPER_V1,
3745
+ "v1"
3746
+ );
3736
3747
  if (!result.success) {
3737
3748
  return problems.badRequest(c, result.error || "Invalid or expired reset token");
3738
3749
  }
@@ -5335,9 +5346,10 @@ var PROVIDERS = {
5335
5346
  authorizeUrl: "https://accounts.google.com/o/oauth2/v2/auth",
5336
5347
  tokenUrl: "https://oauth2.googleapis.com/token",
5337
5348
  userinfoUrl: "https://www.googleapis.com/oauth2/v2/userinfo",
5338
- scopes: ["email"],
5349
+ scopes: ["email", "profile"],
5339
5350
  emailExtractor: (data) => data.email || null,
5340
- userIdExtractor: (data) => data.id || null
5351
+ userIdExtractor: (data) => data.id || null,
5352
+ nameExtractor: (data) => data.name || null
5341
5353
  },
5342
5354
  microsoft: {
5343
5355
  authorizeUrl: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
@@ -5345,7 +5357,8 @@ var PROVIDERS = {
5345
5357
  userinfoUrl: "https://graph.microsoft.com/v1.0/me",
5346
5358
  scopes: ["openid", "email", "User.Read"],
5347
5359
  emailExtractor: (data) => data.mail || data.userPrincipalName || null,
5348
- userIdExtractor: (data) => data.id || null
5360
+ userIdExtractor: (data) => data.id || null,
5361
+ nameExtractor: (data) => data.displayName || null
5349
5362
  }
5350
5363
  };
5351
5364
  function getProviderCredentials(provider, env) {
@@ -5582,6 +5595,7 @@ var callbackHandler = async (c) => {
5582
5595
  const userInfo = await userinfoResponse.json();
5583
5596
  const email = providerConfig.emailExtractor(userInfo)?.toLowerCase();
5584
5597
  const providerUserId = providerConfig.userIdExtractor(userInfo);
5598
+ const name = providerConfig.nameExtractor(userInfo);
5585
5599
  if (!email) {
5586
5600
  logger_default.warn("No email returned from OAuth provider", { provider });
5587
5601
  return c.redirect(`${appUrl}/login?error=oauth_no_email`);
@@ -5592,53 +5606,49 @@ var callbackHandler = async (c) => {
5592
5606
  }
5593
5607
  const { db, schema } = getAuthContext(c);
5594
5608
  const [existingUser] = await db.select({ id: schema.users.id, email: schema.users.email }).from(schema.users).where(eq31(schema.users.email, email)).limit(1);
5595
- if (existingUser) {
5596
- const loginCode = crypto.randomUUID();
5597
- await c.env.OAUTH_STATES.put(
5598
- `oauth:login:${loginCode}`,
5599
- JSON.stringify({
5600
- userId: existingUser.id,
5601
- email: existingUser.email,
5602
- provider,
5603
- providerUserId,
5604
- redirect: loginRedirect,
5605
- invitationToken
5606
- }),
5607
- { expirationTtl: 60 }
5608
- );
5609
- logSecurityEvent("oauth_login_initiated", "low", {
5610
- userId: existingUser.id,
5611
- email: existingUser.email,
5609
+ let authUser = existingUser;
5610
+ if (!authUser) {
5611
+ const [createdUser] = await db.insert(schema.users).values({
5612
+ email,
5613
+ name: name || null,
5614
+ hashedPassword: null,
5615
+ pepperKid: null,
5616
+ emailVerified: true,
5617
+ // Provider verified the email
5618
+ activatedAt: /* @__PURE__ */ new Date()
5619
+ }).returning({ id: schema.users.id, email: schema.users.email });
5620
+ authUser = createdUser;
5621
+ logSecurityEvent("user_signup_oauth", "low", {
5622
+ userId: createdUser.id,
5623
+ email: createdUser.email,
5612
5624
  provider
5613
5625
  });
5614
- if (mobileRedirectUri && isDeepLinkUri(mobileRedirectUri)) {
5615
- const mobileUrl = new URL(mobileRedirectUri);
5616
- mobileUrl.searchParams.set("code", loginCode);
5617
- mobileUrl.searchParams.set("action", "login");
5618
- return c.redirect(mobileUrl.toString());
5619
- }
5620
- return c.redirect(`${appUrl}/auth/callback?code=${loginCode}&action=login`);
5621
- } else {
5622
- const signupToken = crypto.randomUUID();
5623
- await c.env.OAUTH_STATES.put(
5624
- `oauth:signup:${signupToken}`,
5625
- JSON.stringify({ email, provider, providerUserId, invitationToken }),
5626
- { expirationTtl: 600 }
5627
- );
5628
- logger_default.info("OAuth signup initiated", { provider, email });
5629
- if (mobileRedirectUri && isDeepLinkUri(mobileRedirectUri)) {
5630
- const mobileUrl = new URL(mobileRedirectUri);
5631
- mobileUrl.searchParams.set("oauth_token", signupToken);
5632
- mobileUrl.searchParams.set("email", email);
5633
- if (invitationToken) {
5634
- mobileUrl.searchParams.set("invitation", invitationToken);
5635
- }
5636
- return c.redirect(mobileUrl.toString());
5637
- }
5638
- let signupUrl = `${appUrl}/signup?oauth_token=${signupToken}&email=${encodeURIComponent(email)}`;
5639
- if (invitationToken) signupUrl += `&invitation=${invitationToken}`;
5640
- return c.redirect(signupUrl);
5641
5626
  }
5627
+ const loginCode = crypto.randomUUID();
5628
+ await c.env.OAUTH_STATES.put(
5629
+ `oauth:login:${loginCode}`,
5630
+ JSON.stringify({
5631
+ userId: authUser.id,
5632
+ email: authUser.email,
5633
+ provider,
5634
+ providerUserId,
5635
+ redirect: loginRedirect,
5636
+ invitationToken
5637
+ }),
5638
+ { expirationTtl: 60 }
5639
+ );
5640
+ logSecurityEvent("oauth_login_initiated", "low", {
5641
+ userId: authUser.id,
5642
+ email: authUser.email,
5643
+ provider
5644
+ });
5645
+ if (mobileRedirectUri && isDeepLinkUri(mobileRedirectUri)) {
5646
+ const mobileUrl = new URL(mobileRedirectUri);
5647
+ mobileUrl.searchParams.set("code", loginCode);
5648
+ mobileUrl.searchParams.set("action", "login");
5649
+ return c.redirect(mobileUrl.toString());
5650
+ }
5651
+ return c.redirect(`${appUrl}/auth/callback?code=${loginCode}&action=login`);
5642
5652
  } catch (err) {
5643
5653
  logError(err, { context: "oauth_callback", provider });
5644
5654
  return c.redirect(`${appUrl}/login?error=oauth_failed`);