feat: Revert to using app_metadata for authentication (#7433)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export * from "./steps"
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./set-auth-app-metadata"
|
||||
@@ -0,0 +1,64 @@
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { isDefined } from "@medusajs/utils"
|
||||
|
||||
type StepInput = {
|
||||
authIdentityId: string
|
||||
actorType: 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 key = `${data.actorType}_id`
|
||||
const authIdentity = await service.retrieve(data.authIdentityId)
|
||||
|
||||
const appMetadata = authIdentity.app_metadata || {}
|
||||
if (isDefined(appMetadata[key])) {
|
||||
throw new Error(`Key ${key} already exists in app metadata`)
|
||||
}
|
||||
|
||||
appMetadata[key] = data.value
|
||||
|
||||
await service.update({
|
||||
id: authIdentity.id,
|
||||
app_metadata: appMetadata,
|
||||
})
|
||||
|
||||
return new StepResponse(authIdentity, {
|
||||
id: authIdentity.id,
|
||||
key: key,
|
||||
})
|
||||
},
|
||||
async (idAndKey, { container }) => {
|
||||
if (!idAndKey) {
|
||||
return
|
||||
}
|
||||
|
||||
const { id, key } = idAndKey
|
||||
|
||||
const service = container.resolve<IAuthModuleService>(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const authIdentity = await service.retrieve(id)
|
||||
|
||||
const appMetadata = authIdentity.app_metadata || {}
|
||||
if (isDefined(appMetadata[key])) {
|
||||
delete appMetadata[key]
|
||||
}
|
||||
|
||||
await service.update({
|
||||
id: authIdentity.id,
|
||||
app_metadata: appMetadata,
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -2,8 +2,7 @@ import { CreateCustomerDTO, CustomerDTO } from "@medusajs/types"
|
||||
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
|
||||
import { createCustomersStep } from "../steps"
|
||||
import { transform } from "@medusajs/workflows-sdk"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { createLinkStep } from "../../common"
|
||||
import { setAuthAppMetadataStep } from "../../auth"
|
||||
|
||||
type WorkflowInput = {
|
||||
authIdentityId: string
|
||||
@@ -21,19 +20,12 @@ export const createCustomerAccountWorkflow = createWorkflow(
|
||||
(customers: CustomerDTO[]) => customers[0]
|
||||
)
|
||||
|
||||
const link = transform(
|
||||
{ customer, authIdentityId: input.authIdentityId },
|
||||
(data) => {
|
||||
return [
|
||||
{
|
||||
[Modules.CUSTOMER]: { customer_id: data.customer.id },
|
||||
[Modules.AUTH]: { auth_identity_id: data.authIdentityId },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
setAuthAppMetadataStep({
|
||||
authIdentityId: input.authIdentityId,
|
||||
actorType: "customer",
|
||||
value: customer.id,
|
||||
})
|
||||
|
||||
createLinkStep(link)
|
||||
return customer
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./api-key"
|
||||
export * from "./auth"
|
||||
export * from "./common"
|
||||
export * from "./customer"
|
||||
export * from "./customer-group"
|
||||
|
||||
@@ -7,8 +7,7 @@ import {
|
||||
import { createUsersStep } from "../../user"
|
||||
import { deleteInvitesStep } from "../steps"
|
||||
import { validateTokenStep } from "../steps/validate-token"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { createLinkStep } from "../../common"
|
||||
import { setAuthAppMetadataStep } from "../../auth"
|
||||
|
||||
export const acceptInviteWorkflowId = "accept-invite-workflow"
|
||||
export const acceptInviteWorkflow = createWorkflow(
|
||||
@@ -32,20 +31,17 @@ export const acceptInviteWorkflow = createWorkflow(
|
||||
|
||||
const users = createUsersStep(createUserInput)
|
||||
|
||||
const link = transform(
|
||||
{ users, authIdentityId: input.auth_identity_id },
|
||||
(data) => {
|
||||
const user = data.users[0]
|
||||
return [
|
||||
{
|
||||
[Modules.USER]: { user_id: user.id },
|
||||
[Modules.AUTH]: { auth_identity_id: data.authIdentityId },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
const authUserInput = transform({ input, users }, ({ input, users }) => {
|
||||
const createdUser = users[0]
|
||||
|
||||
createLinkStep(link)
|
||||
return {
|
||||
authIdentityId: input.auth_identity_id,
|
||||
actorType: "user",
|
||||
value: createdUser.id,
|
||||
}
|
||||
})
|
||||
|
||||
setAuthAppMetadataStep(authUserInput)
|
||||
deleteInvitesStep([invite.id])
|
||||
|
||||
return users
|
||||
|
||||
@@ -5,8 +5,7 @@ import {
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createUsersStep } from "../steps"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { createLinkStep } from "../../common"
|
||||
import { setAuthAppMetadataStep } from "../../auth"
|
||||
|
||||
type WorkflowInput = {
|
||||
authIdentityId: string
|
||||
@@ -20,19 +19,11 @@ export const createUserAccountWorkflow = createWorkflow(
|
||||
const users = createUsersStep([input.userData])
|
||||
const user = transform(users, (users: UserDTO[]) => users[0])
|
||||
|
||||
const link = transform(
|
||||
{ user, authIdentityId: input.authIdentityId },
|
||||
(data) => {
|
||||
return [
|
||||
{
|
||||
[Modules.USER]: { user_id: data.user.id },
|
||||
[Modules.AUTH]: { auth_identity_id: data.authIdentityId },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
createLinkStep(link)
|
||||
setAuthAppMetadataStep({
|
||||
authIdentityId: input.authIdentityId,
|
||||
actorType: "user",
|
||||
value: user.id,
|
||||
})
|
||||
return user
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user