From 12b12d4aab5544bb834c5fcd92701efd412e119d 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 7b11ad34b54c7..3ff52fdf561cc 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 c2a706adaf1b0..003cf74856557 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 17e337c8ff53f..e6ccb248d0137 100644 --- a/packages/frontend/core/src/modules/cloud/services/auth.ts +++ b/packages/frontend/core/src/modules/cloud/services/auth.ts @@ -222,7 +222,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', },