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,8 +1,8 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
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 adminSeeder from "./admin-seeder"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
|
||||
export const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
@@ -13,26 +13,53 @@ export const createAdminUser = async (
|
||||
adminHeaders,
|
||||
container?
|
||||
) => {
|
||||
const { password_hash } = await adminSeeder(dbConnection)
|
||||
const appContainer = container ?? getContainer()!
|
||||
|
||||
const userModule: IUserModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.USER
|
||||
)
|
||||
const authModule: IAuthModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
if (authModule) {
|
||||
const authUser = await authModule.create({
|
||||
provider: "emailpass",
|
||||
entity_id: "admin@medusa.js",
|
||||
scope: "admin",
|
||||
app_metadata: {
|
||||
user_id: "admin_user",
|
||||
},
|
||||
provider_metadata: {
|
||||
password: password_hash,
|
||||
},
|
||||
})
|
||||
const remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const token = jwt.sign(authUser, "test")
|
||||
adminHeaders.headers["authorization"] = `Bearer ${token}`
|
||||
}
|
||||
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}`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user