feat(customer): Add create and retrieve customer from store side (#6267)

**What**
- GET /store/customers/me
- POST /store/customers
- Workflow for customer account creation
- Authentication middleware on customer routes
This commit is contained in:
Sebastian Rindom
2024-01-31 11:58:29 +00:00
committed by GitHub
parent f41877ef61
commit 7903a15e0f
33 changed files with 421 additions and 16 deletions
+1
View File
@@ -0,0 +1 @@
export * from "./steps"
@@ -0,0 +1 @@
export * from "./set-auth-app-metadata"
@@ -0,0 +1,59 @@
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { IAuthModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { isDefined } from "@medusajs/utils"
type StepInput = {
authUserId: string
key: string
value: string
}
export const setAuthAppMetadataStepId = "set-auth-app-metadata"
export const setAuthAppMetadataStep = createStep(
setAuthAppMetadataStepId,
async (data: StepInput, { container }) => {
const service = container.resolve<IAuthModuleService>(
ModuleRegistrationName.AUTH
)
const authUser = await service.retrieveAuthUser(data.authUserId)
const appMetadata = authUser.app_metadata || {}
if (isDefined(appMetadata[data.key])) {
throw new Error(`Key ${data.key} already exists in app metadata`)
}
appMetadata[data.key] = data.value
await service.updateAuthUser({
id: authUser.id,
app_metadata: appMetadata,
})
return new StepResponse(authUser, { id: authUser.id, key: data.key })
},
async (idAndKey, { container }) => {
if (!idAndKey) {
return
}
const { id, key } = idAndKey
const service = container.resolve<IAuthModuleService>(
ModuleRegistrationName.AUTH
)
const authUser = await service.retrieveAuthUser(id)
const appMetadata = authUser.app_metadata || {}
if (isDefined(appMetadata[key])) {
delete appMetadata[key]
}
await service.updateAuthUser({
id: authUser.id,
app_metadata: appMetadata,
})
}
)