connectbase-client 0.1.3 → 0.1.5

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.mjs CHANGED
@@ -1993,109 +1993,88 @@ var OAuthAPI = class {
1993
1993
  return this.http.get("/v1/public/oauth/providers");
1994
1994
  }
1995
1995
  /**
1996
- * OAuth 인증 URL 조회
1997
- * 사용자를 소셜 로그인 페이지로 리다이렉트할 URL을 반환합니다.
1996
+ * 소셜 로그인 (중앙 콜백 방식) - 권장
1997
+ * Google Cloud Console에 별도로 redirect_uri를 등록할 필요가 없습니다.
1998
+ * OAuth 완료 후 지정한 콜백 URL로 토큰과 함께 리다이렉트됩니다.
1998
1999
  *
1999
2000
  * @param provider - OAuth 프로바이더 (google, naver, github, discord)
2000
- * @param options - redirect_uri와 선택적 state 파라미터
2001
- * @returns 인증 URL
2001
+ * @param callbackUrl - OAuth 완료 리다이렉트될 앱의 URL
2002
+ * @param state - 선택적 state 파라미터 (CSRF 방지 등)
2002
2003
  *
2003
2004
  * @example
2004
2005
  * ```typescript
2005
- * const { authorization_url } = await cb.oauth.getAuthorizationURL('google', {
2006
- * redirect_uri: 'https://myapp.com/auth/callback'
2007
- * })
2008
- *
2009
- * // 사용자를 OAuth 페이지로 리다이렉트
2010
- * window.location.href = authorization_url
2006
+ * // 로그인 버튼 클릭
2007
+ * await cb.oauth.signIn('google', 'https://myapp.com/oauth/callback')
2008
+ * // Google 로그인 후 https://myapp.com/oauth/callback?access_token=...&refresh_token=... 로 리다이렉트됨
2011
2009
  * ```
2012
- */
2013
- async getAuthorizationURL(provider, options) {
2014
- const params = new URLSearchParams({
2015
- redirect_uri: options.redirect_uri
2016
- });
2017
- if (options.state) {
2018
- params.append("state", options.state);
2019
- }
2020
- return this.http.get(
2021
- `/v1/public/oauth/${provider}/authorize?${params.toString()}`
2022
- );
2023
- }
2024
- /**
2025
- * OAuth 콜백 처리
2026
- * 소셜 로그인 후 콜백으로 받은 code를 사용하여 로그인을 완료합니다.
2027
- * 성공 시 자동으로 토큰이 저장됩니다.
2028
- *
2029
- * @param provider - OAuth 프로바이더
2030
- * @param data - code와 redirect_uri
2031
- * @returns 로그인 결과 (member_id, 토큰, 신규 회원 여부)
2032
2010
  *
2033
2011
  * @example
2034
2012
  * ```typescript
2035
- * // 콜백 페이지에서 URL 파라미터로 code 추출
2036
- * const urlParams = new URLSearchParams(window.location.search)
2037
- * const code = urlParams.get('code')
2038
- *
2039
- * const result = await cb.oauth.handleCallback('google', {
2040
- * code: code,
2041
- * redirect_uri: 'https://myapp.com/auth/callback'
2042
- * })
2043
- *
2044
- * if (result.is_new_member) {
2045
- * console.log('신규 회원입니다!')
2013
+ * // callback 페이지에서
2014
+ * const result = cb.oauth.getCallbackResult()
2015
+ * if (result) {
2016
+ * if (result.error) {
2017
+ * console.error('로그인 실패:', result.error)
2018
+ * } else {
2019
+ * console.log('로그인 성공:', result.member_id)
2020
+ * // 메인 페이지로 이동
2021
+ * window.location.href = '/'
2022
+ * }
2046
2023
  * }
2047
2024
  * ```
2048
2025
  */
2049
- async handleCallback(provider, data) {
2050
- const response = await this.http.post(
2051
- `/v1/public/oauth/${provider}/callback`,
2052
- data
2026
+ async signIn(provider, callbackUrl, state) {
2027
+ const params = new URLSearchParams({ app_callback: callbackUrl });
2028
+ if (state) {
2029
+ params.append("state", state);
2030
+ }
2031
+ const response = await this.http.get(
2032
+ `/v1/public/oauth/${provider}/authorize/central?${params.toString()}`
2053
2033
  );
2054
- this.http.setTokens(response.access_token, response.refresh_token);
2055
- return response;
2034
+ window.location.href = response.authorization_url;
2056
2035
  }
2057
2036
  /**
2058
- * 소셜 로그인 전체 플로우 (팝업 방식)
2059
- * 창에서 소셜 로그인을 처리하고 결과를 Promise로 반환합니다.
2037
+ * 소셜 로그인 (팝업 방식)
2038
+ * 팝업 창에서 소셜 로그인을 처리하고 결과를 Promise로 반환합니다.
2039
+ * Google Cloud Console에 별도 등록 불필요.
2060
2040
  *
2061
2041
  * @param provider - OAuth 프로바이더
2062
- * @param redirectUri - 콜백 URL (이 페이지에서 postMessage로 결과 전달 필요)
2042
+ * @param callbackUrl - OAuth 완료 리다이렉트될 팝업의 URL
2063
2043
  * @returns 로그인 결과
2064
2044
  *
2065
2045
  * @example
2066
2046
  * ```typescript
2067
- * // 로그인 버튼 클릭
2068
- * const result = await cb.oauth.signInWithPopup('google', 'https://myapp.com/auth/popup-callback')
2047
+ * const result = await cb.oauth.signInWithPopup('google', 'https://myapp.com/oauth/popup-callback')
2069
2048
  * console.log('로그인 성공:', result.member_id)
2070
2049
  * ```
2071
2050
  *
2072
2051
  * @example
2073
2052
  * ```html
2074
2053
  * <!-- popup-callback.html -->
2054
+ * <script src="https://unpkg.com/connectbase-client"></script>
2075
2055
  * <script>
2076
- * const urlParams = new URLSearchParams(window.location.search)
2077
- * const code = urlParams.get('code')
2078
- * const error = urlParams.get('error')
2056
+ * const cb = new ConnectBase({ apiKey: 'YOUR_API_KEY' })
2057
+ * const result = cb.oauth.getCallbackResult()
2079
2058
  *
2080
2059
  * window.opener.postMessage({
2081
2060
  * type: 'oauth-callback',
2082
- * code,
2083
- * error
2061
+ * ...result
2084
2062
  * }, '*')
2085
2063
  * window.close()
2086
2064
  * </script>
2087
2065
  * ```
2088
2066
  */
2089
- async signInWithPopup(provider, redirectUri) {
2090
- const { authorization_url } = await this.getAuthorizationURL(provider, {
2091
- redirect_uri: redirectUri
2092
- });
2067
+ async signInWithPopup(provider, callbackUrl) {
2068
+ const params = new URLSearchParams({ app_callback: callbackUrl });
2069
+ const response = await this.http.get(
2070
+ `/v1/public/oauth/${provider}/authorize/central?${params.toString()}`
2071
+ );
2093
2072
  const width = 500;
2094
2073
  const height = 600;
2095
2074
  const left = window.screenX + (window.outerWidth - width) / 2;
2096
2075
  const top = window.screenY + (window.outerHeight - height) / 2;
2097
2076
  const popup = window.open(
2098
- authorization_url,
2077
+ response.authorization_url,
2099
2078
  "oauth-popup",
2100
2079
  `width=${width},height=${height},left=${left},top=${top}`
2101
2080
  );
@@ -2110,19 +2089,14 @@ var OAuthAPI = class {
2110
2089
  reject(new Error(event.data.error));
2111
2090
  return;
2112
2091
  }
2113
- if (!event.data.code) {
2114
- reject(new Error("\uC778\uC99D \uCF54\uB4DC\uAC00 \uC5C6\uC2B5\uB2C8\uB2E4."));
2115
- return;
2116
- }
2117
- try {
2118
- const result = await this.handleCallback(provider, {
2119
- code: event.data.code,
2120
- redirect_uri: redirectUri
2121
- });
2122
- resolve(result);
2123
- } catch (error) {
2124
- reject(error);
2125
- }
2092
+ const result = {
2093
+ member_id: event.data.member_id,
2094
+ access_token: event.data.access_token,
2095
+ refresh_token: event.data.refresh_token,
2096
+ is_new_member: event.data.is_new_member === "true" || event.data.is_new_member === true
2097
+ };
2098
+ this.http.setTokens(result.access_token, result.refresh_token);
2099
+ resolve(result);
2126
2100
  };
2127
2101
  window.addEventListener("message", handleMessage);
2128
2102
  const checkClosed = setInterval(() => {
@@ -2135,41 +2109,46 @@ var OAuthAPI = class {
2135
2109
  });
2136
2110
  }
2137
2111
  /**
2138
- * 소셜 로그인 (리다이렉트 방식)
2139
- * 현재 페이지를 소셜 로그인 페이지로 리다이렉트합니다.
2140
- * 콜백 URL에서 handleCallback()을 호출하여 로그인을 완료해야 합니다.
2112
+ * 콜백 URL에서 OAuth 결과 추출
2113
+ * 중앙 콜백 방식에서 리다이렉트 URL 파라미터에서 결과를 추출합니다.
2114
+ * 토큰이 있으면 자동으로 저장됩니다.
2141
2115
  *
2142
- * @param provider - OAuth 프로바이더
2143
- * @param redirectUri - 콜백 URL
2144
- *
2145
- * @example
2146
- * ```typescript
2147
- * // 로그인 버튼 클릭 시
2148
- * await cb.oauth.signInWithRedirect('google', 'https://myapp.com/auth/callback')
2149
- * // 페이지가 Google 로그인으로 리다이렉트됨
2150
- * ```
2116
+ * @returns OAuth 결과 (토큰, member_id 등) 또는 null
2151
2117
  *
2152
2118
  * @example
2153
2119
  * ```typescript
2154
2120
  * // callback 페이지에서
2155
- * const urlParams = new URLSearchParams(window.location.search)
2156
- * const code = urlParams.get('code')
2157
- *
2158
- * if (code) {
2159
- * const result = await cb.oauth.handleCallback('google', {
2160
- * code,
2161
- * redirect_uri: 'https://myapp.com/auth/callback'
2162
- * })
2163
- * // 로그인 완료, 메인 페이지로 이동
2164
- * window.location.href = '/'
2121
+ * const result = cb.oauth.getCallbackResult()
2122
+ * if (result) {
2123
+ * if (result.error) {
2124
+ * alert('로그인 실패: ' + result.error)
2125
+ * } else {
2126
+ * console.log('로그인 성공:', result.member_id)
2127
+ * window.location.href = '/'
2128
+ * }
2165
2129
  * }
2166
2130
  * ```
2167
2131
  */
2168
- async signInWithRedirect(provider, redirectUri) {
2169
- const { authorization_url } = await this.getAuthorizationURL(provider, {
2170
- redirect_uri: redirectUri
2171
- });
2172
- window.location.href = authorization_url;
2132
+ getCallbackResult() {
2133
+ const params = new URLSearchParams(window.location.search);
2134
+ const error = params.get("error");
2135
+ if (error) {
2136
+ return { error };
2137
+ }
2138
+ const accessToken = params.get("access_token");
2139
+ const refreshToken = params.get("refresh_token");
2140
+ const memberId = params.get("member_id");
2141
+ if (!accessToken || !refreshToken || !memberId) {
2142
+ return null;
2143
+ }
2144
+ this.http.setTokens(accessToken, refreshToken);
2145
+ return {
2146
+ access_token: accessToken,
2147
+ refresh_token: refreshToken,
2148
+ member_id: memberId,
2149
+ is_new_member: params.get("is_new_member") === "true",
2150
+ state: params.get("state") || void 0
2151
+ };
2173
2152
  }
2174
2153
  };
2175
2154
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "connectbase-client",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Connect Base JavaScript/TypeScript SDK for browser and Node.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,7 +31,8 @@
31
31
  "scripts": {
32
32
  "build": "tsup",
33
33
  "dev": "tsup --watch",
34
- "typecheck": "tsc --noEmit"
34
+ "typecheck": "tsc --noEmit",
35
+ "publish": "pnpm run build && npm publish --access public"
35
36
  },
36
37
  "keywords": [
37
38
  "connect-base",