Files
medusa-store/integration-tests/modules/helpers/create-authenticated-customer.ts
Stevche Radevski 8a070d5d85 Revamp the authentication setup (#7419)
* feat: Add email pass authentication provider package

* feat: Revamp auth module and remove concept of scope

* feat: Revamp the auth module to be more standardized in how providers are loaded

* feat: Switch from scope to actor type for authentication

* feat: Add support for per-actor auth methods

* feat: Add emailpass auth provider by default

* fix: Add back app_metadata in auth module
2024-05-23 20:56:40 +02:00

53 lines
1.4 KiB
TypeScript

import { CreateCustomerDTO, MedusaContainer } from "@medusajs/types"
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
import jwt from "jsonwebtoken"
import { ContainerRegistrationKeys } from "@medusajs/utils"
export const createAuthenticatedCustomer = async (
appContainer: MedusaContainer,
customerData: Partial<CreateCustomerDTO> = {}
) => {
const { http } = appContainer.resolve("configModule").projectConfig
const authService = appContainer.resolve(ModuleRegistrationName.AUTH)
const customerModuleService = appContainer.resolve(
ModuleRegistrationName.CUSTOMER
)
const remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const customer = await customerModuleService.create({
first_name: "John",
last_name: "Doe",
email: "john@me.com",
...customerData,
})
const authIdentity = await authService.create({
entity_id: "store_user",
provider: "emailpass",
})
// Ideally we simulate a signup process than manually linking here.
await remoteLink.create([
{
[Modules.CUSTOMER]: {
customer_id: customer.id,
},
[Modules.AUTH]: {
auth_identity_id: authIdentity.id,
},
},
])
const token = jwt.sign(
{
actor_id: customer.id,
actor_type: "customer",
auth_identity_id: authIdentity.id,
},
http.jwtSecret
)
return { customer, authIdentity, jwt: token }
}