@spfn/auth 0.2.0-beta.12 → 0.2.0-beta.14

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/server.js CHANGED
@@ -1359,7 +1359,7 @@ var init_literal2 = __esm({
1359
1359
  });
1360
1360
 
1361
1361
  // ../../node_modules/.pnpm/@sinclair+typebox@0.34.41/node_modules/@sinclair/typebox/build/esm/type/boolean/boolean.mjs
1362
- function Boolean(options) {
1362
+ function Boolean2(options) {
1363
1363
  return CreateType({ [Kind]: "Boolean", type: "boolean" }, options);
1364
1364
  }
1365
1365
  var init_boolean = __esm({
@@ -1441,7 +1441,7 @@ var init_string2 = __esm({
1441
1441
  // ../../node_modules/.pnpm/@sinclair+typebox@0.34.41/node_modules/@sinclair/typebox/build/esm/type/template-literal/syntax.mjs
1442
1442
  function* FromUnion(syntax) {
1443
1443
  const trim = syntax.trim().replace(/"|'/g, "");
1444
- return trim === "boolean" ? yield Boolean() : trim === "number" ? yield Number2() : trim === "bigint" ? yield BigInt() : trim === "string" ? yield String2() : yield (() => {
1444
+ return trim === "boolean" ? yield Boolean2() : trim === "number" ? yield Number2() : trim === "bigint" ? yield BigInt() : trim === "string" ? yield String2() : yield (() => {
1445
1445
  const literals = trim.split("|").map((literal) => Literal(literal.trim()));
1446
1446
  return literals.length === 0 ? Never() : literals.length === 1 ? literals[0] : UnionEvaluated(literals);
1447
1447
  })();
@@ -4244,7 +4244,7 @@ __export(type_exports3, {
4244
4244
  AsyncIterator: () => AsyncIterator,
4245
4245
  Awaited: () => Awaited,
4246
4246
  BigInt: () => BigInt,
4247
- Boolean: () => Boolean,
4247
+ Boolean: () => Boolean2,
4248
4248
  Capitalize: () => Capitalize,
4249
4249
  Composite: () => Composite,
4250
4250
  Const: () => Const,
@@ -7679,13 +7679,21 @@ function getGoogleOAuthConfig() {
7679
7679
  redirectUri
7680
7680
  };
7681
7681
  }
7682
- function getGoogleAuthUrl(state, scopes = ["email", "profile"]) {
7682
+ function getDefaultScopes() {
7683
+ const envScopes = env5.SPFN_AUTH_GOOGLE_SCOPES;
7684
+ if (envScopes) {
7685
+ return envScopes.split(",").map((s) => s.trim()).filter(Boolean);
7686
+ }
7687
+ return ["email", "profile"];
7688
+ }
7689
+ function getGoogleAuthUrl(state, scopes) {
7690
+ const resolvedScopes = scopes ?? getDefaultScopes();
7683
7691
  const config = getGoogleOAuthConfig();
7684
7692
  const params = new URLSearchParams({
7685
7693
  client_id: config.clientId,
7686
7694
  redirect_uri: config.redirectUri,
7687
7695
  response_type: "code",
7688
- scope: scopes.join(" "),
7696
+ scope: resolvedScopes.join(" "),
7689
7697
  state,
7690
7698
  access_type: "offline",
7691
7699
  // refresh_token 받기 위해
@@ -7947,6 +7955,31 @@ function getEnabledOAuthProviders() {
7947
7955
  }
7948
7956
  return providers;
7949
7957
  }
7958
+ var TOKEN_EXPIRY_BUFFER_MS = 5 * 60 * 1e3;
7959
+ async function getGoogleAccessToken(userId) {
7960
+ const account = await socialAccountsRepository.findByUserIdAndProvider(userId, "google");
7961
+ if (!account) {
7962
+ throw new ValidationError2({
7963
+ message: "No Google account linked. User must sign in with Google first."
7964
+ });
7965
+ }
7966
+ const isExpired = !account.tokenExpiresAt || account.tokenExpiresAt.getTime() < Date.now() + TOKEN_EXPIRY_BUFFER_MS;
7967
+ if (!isExpired && account.accessToken) {
7968
+ return account.accessToken;
7969
+ }
7970
+ if (!account.refreshToken) {
7971
+ throw new ValidationError2({
7972
+ message: "Google refresh token not available. User must re-authenticate with Google."
7973
+ });
7974
+ }
7975
+ const tokens = await refreshAccessToken(account.refreshToken);
7976
+ await socialAccountsRepository.updateTokens(account.id, {
7977
+ accessToken: tokens.access_token,
7978
+ refreshToken: tokens.refresh_token ?? account.refreshToken,
7979
+ tokenExpiresAt: new Date(Date.now() + tokens.expires_in * 1e3)
7980
+ });
7981
+ return tokens.access_token;
7982
+ }
7950
7983
 
7951
7984
  // src/server/routes/auth/index.ts
7952
7985
  init_esm();
@@ -9077,6 +9110,7 @@ export {
9077
9110
  getAuthConfig,
9078
9111
  getAuthSessionService,
9079
9112
  getEnabledOAuthProviders,
9113
+ getGoogleAccessToken,
9080
9114
  getGoogleAuthUrl,
9081
9115
  getGoogleOAuthConfig,
9082
9116
  getGoogleUserInfo,