From e4748414d04734ec16e2190cb283ba8b3eb44633 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Tue, 5 Nov 2024 20:38:48 +0800 Subject: [PATCH] fix: web client login --- .../backend/server/src/plugins/oauth/controller.ts | 3 +-- .../core/src/desktop/pages/auth/oauth-callback.tsx | 11 ++++------- .../frontend/core/src/modules/cloud/services/auth.ts | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/backend/server/src/plugins/oauth/controller.ts b/packages/backend/server/src/plugins/oauth/controller.ts index 814d7a00fae36..5605ac88577e6 100644 --- a/packages/backend/server/src/plugins/oauth/controller.ts +++ b/packages/backend/server/src/plugins/oauth/controller.ts @@ -220,7 +220,6 @@ export class OAuthController { // we only cache the code and access token in server side const authState = await this.oauth.getOAuthState(oAuthToken); if (!authState || authState.state !== inAppState || !authState.code) { - console.log('authState', authState, 'inAppState', inAppState); throw new OauthStateExpired(); } @@ -238,7 +237,7 @@ export class OAuthController { // NOTE: in web client, we don't need to exchange token // and provide the auth code directly - const tokens = await provider.getToken(code || authState.code); + const tokens = await provider.getToken(authState.code); const externAccount = await provider.getUser(tokens.accessToken); const user = await this.loginFromOauth( authState.provider, diff --git a/packages/frontend/core/src/desktop/pages/auth/oauth-callback.tsx b/packages/frontend/core/src/desktop/pages/auth/oauth-callback.tsx index 416685134ca6b..ad9481504fbc2 100644 --- a/packages/frontend/core/src/desktop/pages/auth/oauth-callback.tsx +++ b/packages/frontend/core/src/desktop/pages/auth/oauth-callback.tsx @@ -21,7 +21,7 @@ const LoaderData = z.object({ const ParsedState = z.object({ payload: LoaderData, - client: supportedClient, + client: supportedClient.optional(), }); type LoaderData = z.infer; @@ -38,16 +38,13 @@ async function parseState(url: string): Promise { return ParsedState.parse({ payload: { state, code, provider }, client }); } catch {} // new client behavior - const { - token: state, - provider, - client, - } = await fetch('/api/oauth/exchangeToken', { + const { token, provider, client } = await fetch('/api/oauth/exchangeToken', { method: 'POST', body: JSON.stringify({ code, state: stateStr }), headers: { 'content-type': 'application/json' }, }).then(r => r.json()); - return ParsedState.parse({ payload: { state, provider }, client }); + const payload = client ? { token } : { code, state: stateStr }; + return ParsedState.parse({ payload: { ...payload, provider }, client }); } export const loader: LoaderFunction = async args => { diff --git a/packages/frontend/core/src/modules/cloud/services/auth.ts b/packages/frontend/core/src/modules/cloud/services/auth.ts index 649bc7ab8a6da..40f825ab8495c 100644 --- a/packages/frontend/core/src/modules/cloud/services/auth.ts +++ b/packages/frontend/core/src/modules/cloud/services/auth.ts @@ -209,7 +209,7 @@ export class AuthService extends Service { try { const res = await this.fetchService.fetch('/api/oauth/callback', { method: 'POST', - body: JSON.stringify({ code, state, secret: this.state }), + body: JSON.stringify({ code, state }), headers: { 'content-type': 'application/json', },