@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.cjs CHANGED
@@ -5738,9 +5738,10 @@ var PROVIDERS = {
5738
5738
  authorizeUrl: "https://accounts.google.com/o/oauth2/v2/auth",
5739
5739
  tokenUrl: "https://oauth2.googleapis.com/token",
5740
5740
  userinfoUrl: "https://www.googleapis.com/oauth2/v2/userinfo",
5741
- scopes: ["email"],
5741
+ scopes: ["email", "profile"],
5742
5742
  emailExtractor: (data) => data.email || null,
5743
- userIdExtractor: (data) => data.id || null
5743
+ userIdExtractor: (data) => data.id || null,
5744
+ nameExtractor: (data) => data.name || null
5744
5745
  },
5745
5746
  microsoft: {
5746
5747
  authorizeUrl: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
@@ -5748,7 +5749,8 @@ var PROVIDERS = {
5748
5749
  userinfoUrl: "https://graph.microsoft.com/v1.0/me",
5749
5750
  scopes: ["openid", "email", "User.Read"],
5750
5751
  emailExtractor: (data) => data.mail || data.userPrincipalName || null,
5751
- userIdExtractor: (data) => data.id || null
5752
+ userIdExtractor: (data) => data.id || null,
5753
+ nameExtractor: (data) => data.displayName || null
5752
5754
  }
5753
5755
  };
5754
5756
  function getProviderCredentials(provider, env) {
@@ -5985,6 +5987,7 @@ var callbackHandler = async (c) => {
5985
5987
  const userInfo = await userinfoResponse.json();
5986
5988
  const email = providerConfig.emailExtractor(userInfo)?.toLowerCase();
5987
5989
  const providerUserId = providerConfig.userIdExtractor(userInfo);
5990
+ const name = providerConfig.nameExtractor(userInfo);
5988
5991
  if (!email) {
5989
5992
  logger_default.warn("No email returned from OAuth provider", { provider });
5990
5993
  return c.redirect(`${appUrl}/login?error=oauth_no_email`);
@@ -5995,53 +5998,49 @@ var callbackHandler = async (c) => {
5995
5998
  }
5996
5999
  const { db, schema } = getAuthContext(c);
5997
6000
  const [existingUser] = await db.select({ id: schema.users.id, email: schema.users.email }).from(schema.users).where((0, import_drizzle_orm32.eq)(schema.users.email, email)).limit(1);
5998
- if (existingUser) {
5999
- const loginCode = crypto.randomUUID();
6000
- await c.env.OAUTH_STATES.put(
6001
- `oauth:login:${loginCode}`,
6002
- JSON.stringify({
6003
- userId: existingUser.id,
6004
- email: existingUser.email,
6005
- provider,
6006
- providerUserId,
6007
- redirect: loginRedirect,
6008
- invitationToken
6009
- }),
6010
- { expirationTtl: 60 }
6011
- );
6012
- logSecurityEvent("oauth_login_initiated", "low", {
6013
- userId: existingUser.id,
6014
- email: existingUser.email,
6001
+ let authUser = existingUser;
6002
+ if (!authUser) {
6003
+ const [createdUser] = await db.insert(schema.users).values({
6004
+ email,
6005
+ name: name || null,
6006
+ hashedPassword: null,
6007
+ pepperKid: null,
6008
+ emailVerified: true,
6009
+ // Provider verified the email
6010
+ activatedAt: /* @__PURE__ */ new Date()
6011
+ }).returning({ id: schema.users.id, email: schema.users.email });
6012
+ authUser = createdUser;
6013
+ logSecurityEvent("user_signup_oauth", "low", {
6014
+ userId: createdUser.id,
6015
+ email: createdUser.email,
6015
6016
  provider
6016
6017
  });
6017
- if (mobileRedirectUri && isDeepLinkUri(mobileRedirectUri)) {
6018
- const mobileUrl = new URL(mobileRedirectUri);
6019
- mobileUrl.searchParams.set("code", loginCode);
6020
- mobileUrl.searchParams.set("action", "login");
6021
- return c.redirect(mobileUrl.toString());
6022
- }
6023
- return c.redirect(`${appUrl}/auth/callback?code=${loginCode}&action=login`);
6024
- } else {
6025
- const signupToken = crypto.randomUUID();
6026
- await c.env.OAUTH_STATES.put(
6027
- `oauth:signup:${signupToken}`,
6028
- JSON.stringify({ email, provider, providerUserId, invitationToken }),
6029
- { expirationTtl: 600 }
6030
- );
6031
- logger_default.info("OAuth signup initiated", { provider, email });
6032
- if (mobileRedirectUri && isDeepLinkUri(mobileRedirectUri)) {
6033
- const mobileUrl = new URL(mobileRedirectUri);
6034
- mobileUrl.searchParams.set("oauth_token", signupToken);
6035
- mobileUrl.searchParams.set("email", email);
6036
- if (invitationToken) {
6037
- mobileUrl.searchParams.set("invitation", invitationToken);
6038
- }
6039
- return c.redirect(mobileUrl.toString());
6040
- }
6041
- let signupUrl = `${appUrl}/signup?oauth_token=${signupToken}&email=${encodeURIComponent(email)}`;
6042
- if (invitationToken) signupUrl += `&invitation=${invitationToken}`;
6043
- return c.redirect(signupUrl);
6044
6018
  }
6019
+ const loginCode = crypto.randomUUID();
6020
+ await c.env.OAUTH_STATES.put(
6021
+ `oauth:login:${loginCode}`,
6022
+ JSON.stringify({
6023
+ userId: authUser.id,
6024
+ email: authUser.email,
6025
+ provider,
6026
+ providerUserId,
6027
+ redirect: loginRedirect,
6028
+ invitationToken
6029
+ }),
6030
+ { expirationTtl: 60 }
6031
+ );
6032
+ logSecurityEvent("oauth_login_initiated", "low", {
6033
+ userId: authUser.id,
6034
+ email: authUser.email,
6035
+ provider
6036
+ });
6037
+ if (mobileRedirectUri && isDeepLinkUri(mobileRedirectUri)) {
6038
+ const mobileUrl = new URL(mobileRedirectUri);
6039
+ mobileUrl.searchParams.set("code", loginCode);
6040
+ mobileUrl.searchParams.set("action", "login");
6041
+ return c.redirect(mobileUrl.toString());
6042
+ }
6043
+ return c.redirect(`${appUrl}/auth/callback?code=${loginCode}&action=login`);
6045
6044
  } catch (err) {
6046
6045
  logError(err, { context: "oauth_callback", provider });
6047
6046
  return c.redirect(`${appUrl}/login?error=oauth_failed`);