From 93cb39e5fc73b49f0ab4a179d5d4a2201645cbbb Mon Sep 17 00:00:00 2001 From: Stevche Radevski Date: Wed, 4 Sep 2024 10:29:58 +0200 Subject: [PATCH] feat: Change callback to always return a token, expect callbackUrl to point to FE --- packages/core/js-sdk/src/auth/index.ts | 27 +++++++++++++++++++ .../core/types/src/auth/common/provider.ts | 9 ------- .../core/types/src/auth/providers/github.ts | 1 - .../core/types/src/auth/providers/google.ts | 1 - packages/core/types/src/auth/service.ts | 6 ++--- .../[auth_provider]/callback/route.ts | 13 +++------ .../__tests__/services.spec.ts | 3 --- .../__tests__/services.spec.ts | 3 --- .../auth-google/src/services/google.ts | 1 - 9 files changed, 34 insertions(+), 30 deletions(-) diff --git a/packages/core/js-sdk/src/auth/index.ts b/packages/core/js-sdk/src/auth/index.ts index 07a45e5490cd2..b171db20a5479 100644 --- a/packages/core/js-sdk/src/auth/index.ts +++ b/packages/core/js-sdk/src/auth/index.ts @@ -55,6 +55,33 @@ export class Auth { return token } + // The callback expects all query parameters from the Oauth callback to be passed to the backend, and the provider is in charge of parsing and validating them + callback = async ( + actor: "customer" | "user", + method: "emailpass", + query?: Record + ) => { + const { token } = await this.client.fetch<{ token: string }>( + `/auth/${actor}/${method}/callback`, + { + method: "GET", + query, + } + ) + + // By default we just set the token in memory, if configured to use sessions we convert it into session storage instead. + if (this.config?.auth?.type === "session") { + await this.client.fetch("/auth/session", { + method: "POST", + headers: { Authorization: `Bearer ${token}` }, + }) + } else { + this.client.setToken(token) + } + + return token + } + logout = async () => { if (this.config?.auth?.type === "session") { await this.client.fetch("/auth/session", { diff --git a/packages/core/types/src/auth/common/provider.ts b/packages/core/types/src/auth/common/provider.ts index 87e3b103d3dd6..0cc313ffb6a01 100644 --- a/packages/core/types/src/auth/common/provider.ts +++ b/packages/core/types/src/auth/common/provider.ts @@ -33,15 +33,6 @@ export type AuthenticationResponse = { * specified location. */ location?: string - - /** - * Some authentication providers support redirecting to a specified URL on - * success. In those cases, the URL to redirect to is set in this field. - * - * So, if `success` is true, there's no `location` set, and this field - * is set, you can redirect to this URL. - */ - successRedirectUrl?: string } /** diff --git a/packages/core/types/src/auth/providers/github.ts b/packages/core/types/src/auth/providers/github.ts index 317989cc50144..3ed9e65b891d1 100644 --- a/packages/core/types/src/auth/providers/github.ts +++ b/packages/core/types/src/auth/providers/github.ts @@ -2,5 +2,4 @@ export interface GithubAuthProviderOptions { clientId: string clientSecret: string callbackUrl: string - successRedirectUrl?: string } diff --git a/packages/core/types/src/auth/providers/google.ts b/packages/core/types/src/auth/providers/google.ts index ecb7c0db98a9c..f484428310e7a 100644 --- a/packages/core/types/src/auth/providers/google.ts +++ b/packages/core/types/src/auth/providers/google.ts @@ -2,5 +2,4 @@ export interface GoogleAuthProviderOptions { clientId: string clientSecret: string callbackUrl: string - successRedirectUrl?: string } diff --git a/packages/core/types/src/auth/service.ts b/packages/core/types/src/auth/service.ts index 0dbeac7f5d62d..4deebbbe79d34 100644 --- a/packages/core/types/src/auth/service.ts +++ b/packages/core/types/src/auth/service.ts @@ -46,12 +46,12 @@ export interface IAuthModuleService extends IModuleService { */ authenticate( provider: string, - providerData: AuthenticationInput, + providerData: AuthenticationInput ): Promise register( provider: string, - providerData: AuthenticationInput, + providerData: AuthenticationInput ): Promise /** @@ -76,7 +76,7 @@ export interface IAuthModuleService extends IModuleService { * `req` is an instance of the `MedusaRequest` object: * * ```ts - * const { success, authIdentity, error, successRedirectUrl } = + * const { success, authIdentity, error } = * await authModuleService.validateCallback("google", { * url: req.url, * headers: req.headers, diff --git a/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/callback/route.ts b/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/callback/route.ts index 82e44d01c1254..e658d5761ca5d 100644 --- a/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/callback/route.ts +++ b/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/callback/route.ts @@ -41,8 +41,10 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => { protocol: req.protocol, } as AuthenticationInput - const { success, error, authIdentity, successRedirectUrl } = - await service.validateCallback(auth_provider, authData) + const { success, error, authIdentity } = await service.validateCallback( + auth_provider, + authData + ) const entityIdKey = `${actor_type}_id` const entityId = authIdentity?.app_metadata?.[entityIdKey] as @@ -71,13 +73,6 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => { } ) - if (successRedirectUrl) { - const url = new URL(successRedirectUrl!) - url.searchParams.append("access_token", token) - - return res.redirect(url.toString()) - } - return res.json({ token }) } diff --git a/packages/modules/providers/auth-github/integration-tests/__tests__/services.spec.ts b/packages/modules/providers/auth-github/integration-tests/__tests__/services.spec.ts index 8564df5690579..db09b443856fd 100644 --- a/packages/modules/providers/auth-github/integration-tests/__tests__/services.spec.ts +++ b/packages/modules/providers/auth-github/integration-tests/__tests__/services.spec.ts @@ -81,7 +81,6 @@ describe("Github auth provider", () => { { clientId: "test", clientSecret: "test", - successRedirectUrl: baseUrl, callbackUrl: `${baseUrl}/auth/github/callback`, } ) @@ -179,7 +178,6 @@ describe("Github auth provider", () => { expect(res).toEqual({ success: true, - successRedirectUrl: baseUrl, authIdentity: { provider_identities: [ { @@ -232,7 +230,6 @@ describe("Github auth provider", () => { expect(res).toEqual({ success: true, - successRedirectUrl: baseUrl, authIdentity: { provider_identities: [ { diff --git a/packages/modules/providers/auth-google/integration-tests/__tests__/services.spec.ts b/packages/modules/providers/auth-google/integration-tests/__tests__/services.spec.ts index 8859b2c457b71..613e1d9e3687a 100644 --- a/packages/modules/providers/auth-google/integration-tests/__tests__/services.spec.ts +++ b/packages/modules/providers/auth-google/integration-tests/__tests__/services.spec.ts @@ -80,7 +80,6 @@ describe("Google auth provider", () => { { clientId: "test", clientSecret: "test", - successRedirectUrl: baseUrl, callbackUrl: `${baseUrl}/auth/google/callback`, } ) @@ -178,7 +177,6 @@ describe("Google auth provider", () => { expect(res).toEqual({ success: true, - successRedirectUrl: baseUrl, authIdentity: { provider_identities: [ { @@ -221,7 +219,6 @@ describe("Google auth provider", () => { expect(res).toEqual({ success: true, - successRedirectUrl: baseUrl, authIdentity: { provider_identities: [ { diff --git a/packages/modules/providers/auth-google/src/services/google.ts b/packages/modules/providers/auth-google/src/services/google.ts index aacff750358f6..ecb4d672b30a8 100644 --- a/packages/modules/providers/auth-google/src/services/google.ts +++ b/packages/modules/providers/auth-google/src/services/google.ts @@ -109,7 +109,6 @@ export class GoogleAuthService extends AbstractAuthModuleProvider { return { success, authIdentity, - successRedirectUrl: this.config_.successRedirectUrl, } } catch (error) { return { success: false, error: error.message }