Files
medusa-store/integration-tests/helpers/create-admin-user.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

66 lines
1.6 KiB
TypeScript

import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
import { IAuthModuleService, IUserModuleService } from "@medusajs/types"
import jwt from "jsonwebtoken"
import { getContainer } from "../environment-helpers/use-container"
import { ContainerRegistrationKeys } from "@medusajs/utils"
export const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
export const createAdminUser = async (
dbConnection,
adminHeaders,
container?
) => {
const appContainer = container ?? getContainer()!
const userModule: IUserModuleService = appContainer.resolve(
ModuleRegistrationName.USER
)
const authModule: IAuthModuleService = appContainer.resolve(
ModuleRegistrationName.AUTH
)
const remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const user = await userModule.create({
first_name: "Admin",
last_name: "User",
email: "admin@medusa.js",
})
const authIdentity = await authModule.create({
provider: "emailpass",
entity_id: "admin@medusa.js",
scope: "admin",
provider_metadata: {
password: "somepassword",
},
})
// Ideally we simulate a signup process than manually linking here.
await remoteLink.create([
{
[Modules.USER]: {
user_id: user.id,
},
[Modules.AUTH]: {
auth_identity_id: authIdentity.id,
},
},
])
const token = jwt.sign(
{
actor_id: user.id,
actor_type: "user",
auth_identity_id: authIdentity.id,
scope: "admin",
app_metadata: {},
},
"test"
)
adminHeaders.headers["authorization"] = `Bearer ${token}`
}