@syncello/auth 3.4.1 → 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
@@ -5346,9 +5346,10 @@ var PROVIDERS = {
5346
5346
  authorizeUrl: "https://accounts.google.com/o/oauth2/v2/auth",
5347
5347
  tokenUrl: "https://oauth2.googleapis.com/token",
5348
5348
  userinfoUrl: "https://www.googleapis.com/oauth2/v2/userinfo",
5349
- scopes: ["email"],
5349
+ scopes: ["email", "profile"],
5350
5350
  emailExtractor: (data) => data.email || null,
5351
- userIdExtractor: (data) => data.id || null
5351
+ userIdExtractor: (data) => data.id || null,
5352
+ nameExtractor: (data) => data.name || null
5352
5353
  },
5353
5354
  microsoft: {
5354
5355
  authorizeUrl: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
@@ -5356,7 +5357,8 @@ var PROVIDERS = {
5356
5357
  userinfoUrl: "https://graph.microsoft.com/v1.0/me",
5357
5358
  scopes: ["openid", "email", "User.Read"],
5358
5359
  emailExtractor: (data) => data.mail || data.userPrincipalName || null,
5359
- userIdExtractor: (data) => data.id || null
5360
+ userIdExtractor: (data) => data.id || null,
5361
+ nameExtractor: (data) => data.displayName || null
5360
5362
  }
5361
5363
  };
5362
5364
  function getProviderCredentials(provider, env) {
@@ -5593,6 +5595,7 @@ var callbackHandler = async (c) => {
5593
5595
  const userInfo = await userinfoResponse.json();
5594
5596
  const email = providerConfig.emailExtractor(userInfo)?.toLowerCase();
5595
5597
  const providerUserId = providerConfig.userIdExtractor(userInfo);
5598
+ const name = providerConfig.nameExtractor(userInfo);
5596
5599
  if (!email) {
5597
5600
  logger_default.warn("No email returned from OAuth provider", { provider });
5598
5601
  return c.redirect(`${appUrl}/login?error=oauth_no_email`);
@@ -5603,53 +5606,49 @@ var callbackHandler = async (c) => {
5603
5606
  }
5604
5607
  const { db, schema } = getAuthContext(c);
5605
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);
5606
- if (existingUser) {
5607
- const loginCode = crypto.randomUUID();
5608
- await c.env.OAUTH_STATES.put(
5609
- `oauth:login:${loginCode}`,
5610
- JSON.stringify({
5611
- userId: existingUser.id,
5612
- email: existingUser.email,
5613
- provider,
5614
- providerUserId,
5615
- redirect: loginRedirect,
5616
- invitationToken
5617
- }),
5618
- { expirationTtl: 60 }
5619
- );
5620
- logSecurityEvent("oauth_login_initiated", "low", {
5621
- userId: existingUser.id,
5622
- 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,
5623
5624
  provider
5624
5625
  });
5625
- if (mobileRedirectUri && isDeepLinkUri(mobileRedirectUri)) {
5626
- const mobileUrl = new URL(mobileRedirectUri);
5627
- mobileUrl.searchParams.set("code", loginCode);
5628
- mobileUrl.searchParams.set("action", "login");
5629
- return c.redirect(mobileUrl.toString());
5630
- }
5631
- return c.redirect(`${appUrl}/auth/callback?code=${loginCode}&action=login`);
5632
- } else {
5633
- const signupToken = crypto.randomUUID();
5634
- await c.env.OAUTH_STATES.put(
5635
- `oauth:signup:${signupToken}`,
5636
- JSON.stringify({ email, provider, providerUserId, invitationToken }),
5637
- { expirationTtl: 600 }
5638
- );
5639
- logger_default.info("OAuth signup initiated", { provider, email });
5640
- if (mobileRedirectUri && isDeepLinkUri(mobileRedirectUri)) {
5641
- const mobileUrl = new URL(mobileRedirectUri);
5642
- mobileUrl.searchParams.set("oauth_token", signupToken);
5643
- mobileUrl.searchParams.set("email", email);
5644
- if (invitationToken) {
5645
- mobileUrl.searchParams.set("invitation", invitationToken);
5646
- }
5647
- return c.redirect(mobileUrl.toString());
5648
- }
5649
- let signupUrl = `${appUrl}/signup?oauth_token=${signupToken}&email=${encodeURIComponent(email)}`;
5650
- if (invitationToken) signupUrl += `&invitation=${invitationToken}`;
5651
- return c.redirect(signupUrl);
5652
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`);
5653
5652
  } catch (err) {
5654
5653
  logError(err, { context: "oauth_callback", provider });
5655
5654
  return c.redirect(`${appUrl}/login?error=oauth_failed`);