* 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
63 lines
1.5 KiB
TypeScript
63 lines
1.5 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",
|
|
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,
|
|
},
|
|
"test"
|
|
)
|
|
|
|
adminHeaders.headers["authorization"] = `Bearer ${token}`
|
|
}
|