feat: Revert to using app_metadata for authentication (#7433)
This commit is contained in:
1 parent
6ec6e2c7b6
commit
135772b27b
24 files changed
+149
-265
No files matched your search
@@ -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
|
||||
}
|
||||
)
|
||||
@@ -22,6 +22,11 @@ export type AuthIdentityDTO = {
|
||||
*/
|
||||
entity_id: string
|
||||
|
||||
/**
|
||||
* Holds information related to the actor IDs tied to the auth identity.
|
||||
*/
|
||||
app_metadata?: Record<string, unknown>
|
||||
|
||||
/**
|
||||
* Holds custom data related to the provider in key-value pairs.
|
||||
*/
|
||||
@@ -56,6 +61,11 @@ export type CreateAuthIdentityDTO = {
|
||||
*/
|
||||
entity_id: string
|
||||
|
||||
/**
|
||||
* Holds information related to the actor IDs tied to the auth identity.
|
||||
*/
|
||||
app_metadata?: Record<string, unknown>
|
||||
|
||||
/**
|
||||
* Holds custom data related to the provider in key-value pairs.
|
||||
*/
|
||||
@@ -78,6 +88,10 @@ export type UpdateAuthIdentityDTO = {
|
||||
*/
|
||||
id: string
|
||||
|
||||
/**
|
||||
* Holds information related to the actor IDs tied to the auth identity.
|
||||
*/
|
||||
app_metadata?: Record<string, unknown>
|
||||
/**
|
||||
* Holds custom data related to the provider in key-value pairs.
|
||||
*/
|
||||
@@ -100,7 +114,7 @@ export interface FilterableAuthIdentityProps
|
||||
id?: string[]
|
||||
|
||||
/**
|
||||
* Filter the auth identitys by the ID of their auth provider.
|
||||
* Filter the auth identities by the ID of their auth provider.
|
||||
*/
|
||||
provider?: string[] | string
|
||||
}
|
||||
@@ -109,16 +109,16 @@ export interface IAuthModuleService extends IModuleService {
|
||||
): Promise<AuthIdentityDTO>
|
||||
|
||||
/**
|
||||
* This method retrieves a paginated list of auth identitys based on optional filters and configuration.
|
||||
* This method retrieves a paginated list of auth identities based on optional filters and configuration.
|
||||
*
|
||||
* @param {FilterableAuthIdentityProps} filters - The filters to apply on the retrieved auth identitys.
|
||||
* @param {FilterableAuthIdentityProps} filters - The filters to apply on the retrieved auth identities.
|
||||
* @param {FindConfig<AuthIdentityDTO>} config - The configurations determining how the auth identity is retrieved. Its properties, such as `select` or `relations`, accept the
|
||||
* attributes or relations associated with a auth identity.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<AuthIdentityDTO[]>} The list of auth identitys.
|
||||
* @returns {Promise<AuthIdentityDTO[]>} The list of auth identities.
|
||||
*
|
||||
* @example
|
||||
* To retrieve a list of auth identitys using their IDs:
|
||||
* To retrieve a list of auth identities using their IDs:
|
||||
*
|
||||
* ```ts
|
||||
* const authIdentities = await authModuleService.list({
|
||||
@@ -147,16 +147,16 @@ export interface IAuthModuleService extends IModuleService {
|
||||
): Promise<AuthIdentityDTO[]>
|
||||
|
||||
/**
|
||||
* This method retrieves a paginated list of auth identitys along with the total count of available auth identitys satisfying the provided filters.
|
||||
* This method retrieves a paginated list of auth identities along with the total count of available auth identities satisfying the provided filters.
|
||||
*
|
||||
* @param {FilterableAuthIdentityProps} filters - The filters to apply on the retrieved auth identitys.
|
||||
* @param {FilterableAuthIdentityProps} filters - The filters to apply on the retrieved auth identities.
|
||||
* @param {FindConfig<AuthIdentityDTO>} config - The configurations determining how the auth identity is retrieved. Its properties, such as `select` or `relations`, accept the
|
||||
* attributes or relations associated with a auth identity.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<[AuthIdentityDTO[], number]>} The list of auth identitys along with their total count.
|
||||
* @returns {Promise<[AuthIdentityDTO[], number]>} The list of auth identities along with their total count.
|
||||
*
|
||||
* @example
|
||||
* To retrieve a list of auth identitys using their IDs:
|
||||
* To retrieve a list of auth identities using their IDs:
|
||||
*
|
||||
* ```ts
|
||||
* const [authIdentities, count] =
|
||||
@@ -187,11 +187,11 @@ export interface IAuthModuleService extends IModuleService {
|
||||
): Promise<[AuthIdentityDTO[], number]>
|
||||
|
||||
/**
|
||||
* This method creates auth identitys.
|
||||
* This method creates auth identities.
|
||||
*
|
||||
* @param {CreateAuthIdentityDTO[]} data - The auth identitys to be created.
|
||||
* @param {CreateAuthIdentityDTO[]} data - The auth identities to be created.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<AuthIdentityDTO[]>} The created auth identitys.
|
||||
* @returns {Promise<AuthIdentityDTO[]>} The created auth identities.
|
||||
*
|
||||
* @example
|
||||
* const authIdentities = await authModuleService.create([
|
||||
@@ -231,7 +231,7 @@ export interface IAuthModuleService extends IModuleService {
|
||||
/**
|
||||
* This method updates existing auths.
|
||||
*
|
||||
* @param {UpdateAuthIdentityDTO[]} data - The attributes to update in the auth identitys.
|
||||
* @param {UpdateAuthIdentityDTO[]} data - The attributes to update in the auth identities.
|
||||
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
|
||||
* @returns {Promise<AuthIdentityDTO[]>} The updated auths.
|
||||
*
|
||||
|
||||
@@ -86,16 +86,4 @@ export const LINKS = {
|
||||
Modules.FULFILLMENT,
|
||||
"fulfillment_id"
|
||||
),
|
||||
UserAuth: composeLinkName(
|
||||
Modules.USER,
|
||||
"user_id",
|
||||
Modules.AUTH,
|
||||
"auth_identity_id"
|
||||
),
|
||||
CustomerAuth: composeLinkName(
|
||||
Modules.CUSTOMER,
|
||||
"customer_id",
|
||||
Modules.AUTH,
|
||||
"auth_identity_id"
|
||||
),
|
||||
}
|
||||
Reference in new issue
Block a user