import { OAuth2Client } from './oauth2client/oauth2client' import { AuthOptions, Auth } from './auth/apis' import { AUTH_API_PREFIX } from './auth/consts' import { Credentials } from './oauth2client/models' export { Auth } from './auth/apis' export { AuthError, AuthErrorCategory } from './auth/auth-error' export * as authModels from './auth/models' export type { ProviderProfile, UserInfo, ModifyUserBasicInfoRequest } from './auth/models' export type { Credentials, OAuth2ClientOptions, ResponseError, AuthClientRequestOptions } from './oauth2client/models' export type { AuthOptions } from './auth/apis' export { weAppJwtDecodeAll } from './utils/base64' export { AUTH_API_PREFIX, OAUTH_TYPE, DEFAULT_NODE_ACCESS_SCOPE, LOGIN_STATE_CHANGED_TYPE, EVENTS, AUTH_STATE_CHANGED_TYPE } from './auth/consts' export class CloudbaseOAuth { public oauth2client: OAuth2Client public authApi: Auth private detectSessionInUrl: boolean constructor(authOptions: AuthOptions) { // eslint-disable-next-line max-len const { apiOrigin, apiPath = AUTH_API_PREFIX, clientId, env, storage, request, baseRequest, anonymousSignInFunc, wxCloud, adapter, onCredentialsError, headers, i18n, useWxCloud, eventBus, detectSessionInUrl, debug, } = authOptions this.detectSessionInUrl = detectSessionInUrl ?? false this.oauth2client = new OAuth2Client({ apiOrigin, apiPath, clientId, env, storage, baseRequest: baseRequest || request, anonymousSignInFunc, wxCloud, onCredentialsError, headers: headers || {}, i18n, useWxCloud, eventBus, debug, }) this.authApi = new Auth({ credentialsClient: this.oauth2client, ...authOptions, // 兼容老逻辑,有值传入则不走Auth内的验证码请求逻辑 request: request ? this.oauth2client.request.bind(this.oauth2client) : undefined, adapter, }) // Set the getInitialSession callback after Auth is created // This allows Auth.getInitialSession to handle URL detection and OAuth verification if (detectSessionInUrl) { this.oauth2client.setGetInitialSession(this.authApi.getInitialSession.bind(this.authApi)) } else { // 在授权之前是hash地址的情况下,需要将hash地址替换到url上 try { const url = new URL(window.location.href) const code = url.searchParams.get('code') const state = url.searchParams.get('state') const cacheData = JSON.parse(sessionStorage.getItem(state) || 'null') if (code && state && cacheData?.hash) { url.hash = cacheData.hash window.history.replaceState(null, '', url.toString()) window.location.replace(url.toString()) } } catch (error) { // } } // Note: Do NOT auto-call initialize() here. // Upper layer (packages/auth) should call initializeSession() after setting up onInitialSessionObtained callback } /** * Setup the onInitialSessionObtained callback and trigger initialization. * This should be called by upper layer (packages/auth) after creating Auth instance. * @param onUserObtained Callback to handle user info storage after session is obtained */ public initializeSession(onUserObtained?: (data: { session: Credentials; user?: any; type?: string }, error?: any) => void | Promise): void { if (!this.detectSessionInUrl) { this.oauth2client.initialize(Promise.resolve({ error: null })) return } this.oauth2client.setOnInitialSessionObtained(onUserObtained) // Pass callback directly to initialize() to ensure it's set before initialization starts this.oauth2client.initialize() } }