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,7 +1,8 @@
|
||||
import { CreateCustomerDTO, MedusaContainer } from "@medusajs/types"
|
||||
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
|
||||
import jwt from "jsonwebtoken"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
|
||||
export const createAuthenticatedCustomer = async (
|
||||
appContainer: MedusaContainer,
|
||||
@@ -12,6 +13,7 @@ export const createAuthenticatedCustomer = async (
|
||||
const customerModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
const remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const customer = await customerModuleService.create({
|
||||
first_name: "John",
|
||||
@@ -20,14 +22,34 @@ export const createAuthenticatedCustomer = async (
|
||||
...customerData,
|
||||
})
|
||||
|
||||
const authUser = await authService.create({
|
||||
const authIdentity = await authService.create({
|
||||
entity_id: "store_user",
|
||||
provider: "emailpass",
|
||||
scope: "store",
|
||||
app_metadata: { customer_id: customer.id },
|
||||
})
|
||||
|
||||
const token = jwt.sign(authUser, http.jwtSecret)
|
||||
// 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,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
return { customer, authUser, jwt: token }
|
||||
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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user