Files
medusa-store/integration-tests/modules/helpers/create-authenticated-customer.ts
Stevche Radevski 5ede560f70 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
2024-05-22 10:27:32 +02:00

56 lines
1.5 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",
scope: "store",
})
// 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,
scope: "store",
app_metadata: {},
},
http.jwtSecret
)
return { customer, authIdentity, jwt: token }
}