feat(auth): Revamp authentication setup (#7387)
* chore: Clean up authentication middlewares * chore: Rename AuthUser to AuthIdentity * feat: Define link between user, customer, and auth identity * feat: Use links for auth, update auth context content * fix: Adjust user create command with new auth setup * fix: Make auth login more dynamic, review fixes * fix: Change test assertions for created by
This commit is contained in:
@@ -1 +0,0 @@
|
||||
export * from "./steps"
|
||||
@@ -1 +0,0 @@
|
||||
export * from "./set-auth-app-metadata"
|
||||
@@ -1,60 +0,0 @@
|
||||
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 = {
|
||||
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.retrieve(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.update({
|
||||
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.retrieve(id)
|
||||
|
||||
const appMetadata = authUser.app_metadata || {}
|
||||
if (isDefined(appMetadata[key])) {
|
||||
delete appMetadata[key]
|
||||
}
|
||||
|
||||
await service.update({
|
||||
id: authUser.id,
|
||||
app_metadata: appMetadata,
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -1,11 +1,12 @@
|
||||
import { CreateCustomerDTO, CustomerDTO } from "@medusajs/types"
|
||||
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
|
||||
import { createCustomersStep } from "../steps"
|
||||
import { setAuthAppMetadataStep } from "../../auth/steps"
|
||||
import { transform } from "@medusajs/workflows-sdk"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { createLinkStep } from "../../common"
|
||||
|
||||
type WorkflowInput = {
|
||||
authUserId: string
|
||||
authIdentityId: string
|
||||
customersData: CreateCustomerDTO
|
||||
}
|
||||
|
||||
@@ -20,12 +21,19 @@ export const createCustomerAccountWorkflow = createWorkflow(
|
||||
(customers: CustomerDTO[]) => customers[0]
|
||||
)
|
||||
|
||||
setAuthAppMetadataStep({
|
||||
authUserId: input.authUserId,
|
||||
key: "customer_id",
|
||||
value: customer.id,
|
||||
})
|
||||
const link = transform(
|
||||
{ customer, authIdentityId: input.authIdentityId },
|
||||
(data) => {
|
||||
return [
|
||||
{
|
||||
[Modules.CUSTOMER]: { customer_id: data.customer.id },
|
||||
[Modules.AUTH]: { auth_identity_id: data.authIdentityId },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
createLinkStep(link)
|
||||
return customer
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
export * from "./api-key"
|
||||
export * from "./auth"
|
||||
export * from "./common"
|
||||
export * from "./customer"
|
||||
export * from "./customer-group"
|
||||
|
||||
@@ -4,10 +4,11 @@ import {
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { setAuthAppMetadataStep } from "../../auth"
|
||||
import { createUsersStep } from "../../user"
|
||||
import { deleteInvitesStep } from "../steps"
|
||||
import { validateTokenStep } from "../steps/validate-token"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { createLinkStep } from "../../common"
|
||||
|
||||
export const acceptInviteWorkflowId = "accept-invite-workflow"
|
||||
export const acceptInviteWorkflow = createWorkflow(
|
||||
@@ -31,18 +32,20 @@ export const acceptInviteWorkflow = createWorkflow(
|
||||
|
||||
const users = createUsersStep(createUserInput)
|
||||
|
||||
const authUserInput = transform({ input, users }, ({ input, users }) => {
|
||||
const createdUser = users[0]
|
||||
|
||||
return {
|
||||
authUserId: input.auth_user_id,
|
||||
key: "user_id",
|
||||
value: createdUser.id,
|
||||
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 },
|
||||
},
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
setAuthAppMetadataStep(authUserInput)
|
||||
)
|
||||
|
||||
createLinkStep(link)
|
||||
deleteInvitesStep([invite.id])
|
||||
|
||||
return users
|
||||
|
||||
@@ -4,11 +4,12 @@ import {
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { setAuthAppMetadataStep } from "../../auth/steps"
|
||||
import { createUsersStep } from "../steps"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { createLinkStep } from "../../common"
|
||||
|
||||
type WorkflowInput = {
|
||||
authUserId: string
|
||||
authIdentityId: string
|
||||
userData: CreateUserDTO
|
||||
}
|
||||
|
||||
@@ -17,15 +18,21 @@ export const createUserAccountWorkflow = createWorkflow(
|
||||
createUserAccountWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<UserDTO> => {
|
||||
const users = createUsersStep([input.userData])
|
||||
|
||||
const user = transform(users, (users: UserDTO[]) => users[0])
|
||||
|
||||
setAuthAppMetadataStep({
|
||||
authUserId: input.authUserId,
|
||||
key: "user_id",
|
||||
value: user.id,
|
||||
})
|
||||
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)
|
||||
return user
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user