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:
Stevche Radevski
2024-05-22 10:27:32 +02:00
committed by GitHub
parent b7df447682
commit 5ede560f70
88 changed files with 887 additions and 1014 deletions
@@ -1,4 +1,4 @@
import { createAuthUsers } from "../../../__fixtures__/auth-user"
import { createAuthIdentities } from "../../../__fixtures__/auth-identity"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { IAuthModuleService } from "@medusajs/types"
@@ -11,15 +11,15 @@ moduleIntegrationTestRunner({
MikroOrmWrapper,
service,
}: SuiteOptions<IAuthModuleService>) => {
describe("AuthUser Service", () => {
describe("AuthIdentity Service", () => {
beforeEach(async () => {
await createAuthUsers(MikroOrmWrapper.forkManager())
await createAuthIdentities(MikroOrmWrapper.forkManager())
})
describe("list", () => {
it("should list authUsers", async () => {
const authUsers = await service.list()
const serialized = JSON.parse(JSON.stringify(authUsers))
it("should list authIdentities", async () => {
const authIdentities = await service.list()
const serialized = JSON.parse(JSON.stringify(authIdentities))
expect(serialized).toEqual([
expect.objectContaining({
@@ -34,24 +34,24 @@ moduleIntegrationTestRunner({
])
})
it("should list authUsers by id", async () => {
const authUsers = await service.list({
it("should list authIdentities by id", async () => {
const authIdentities = await service.list({
id: ["test-id"],
})
expect(authUsers).toEqual([
expect(authIdentities).toEqual([
expect.objectContaining({
id: "test-id",
}),
])
})
it("should list authUsers by provider_id", async () => {
const authUsers = await service.list({
it("should list authIdentities by provider_id", async () => {
const authIdentities = await service.list({
provider: "manual",
})
const serialized = JSON.parse(JSON.stringify(authUsers))
const serialized = JSON.parse(JSON.stringify(authIdentities))
expect(serialized).toEqual([
expect.objectContaining({
@@ -65,9 +65,9 @@ moduleIntegrationTestRunner({
})
describe("listAndCount", () => {
it("should list authUsers", async () => {
const [authUsers, count] = await service.listAndCount()
const serialized = JSON.parse(JSON.stringify(authUsers))
it("should list authIdentities", async () => {
const [authIdentities, count] = await service.listAndCount()
const serialized = JSON.parse(JSON.stringify(authIdentities))
expect(count).toEqual(3)
expect(serialized).toEqual([
@@ -83,13 +83,13 @@ moduleIntegrationTestRunner({
])
})
it("should listAndCount authUsers by provider_id", async () => {
const [authUsers, count] = await service.listAndCount({
it("should listAndCount authIdentities by provider_id", async () => {
const [authIdentities, count] = await service.listAndCount({
provider: "manual",
})
expect(count).toEqual(2)
expect(authUsers).toEqual([
expect(authIdentities).toEqual([
expect.objectContaining({
id: "test-id",
}),
@@ -103,29 +103,29 @@ moduleIntegrationTestRunner({
describe("retrieve", () => {
const id = "test-id"
it("should return an authUser for the given id", async () => {
const authUser = await service.retrieve(id)
it("should return an authIdentity for the given id", async () => {
const authIdentity = await service.retrieve(id)
expect(authUser).toEqual(
expect(authIdentity).toEqual(
expect.objectContaining({
id,
})
)
})
it("should return authUser based on config select param", async () => {
const authUser = await service.retrieve(id, {
it("should return authIdentity based on config select param", async () => {
const authIdentity = await service.retrieve(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(authUser))
const serialized = JSON.parse(JSON.stringify(authIdentity))
expect(serialized).toEqual({
id,
})
})
it("should throw an error when an authUser with the given id does not exist", async () => {
it("should throw an error when an authIdentity with the given id does not exist", async () => {
let error
try {
@@ -135,11 +135,11 @@ moduleIntegrationTestRunner({
}
expect(error.message).toEqual(
"AuthUser with id: does-not-exist was not found"
"AuthIdentity with id: does-not-exist was not found"
)
})
it("should throw an error when a authUserId is not provided", async () => {
it("should throw an error when a authIdentityId is not provided", async () => {
let error
try {
@@ -148,21 +148,21 @@ moduleIntegrationTestRunner({
error = e
}
expect(error.message).toEqual("authUser - id must be defined")
expect(error.message).toEqual("authIdentity - id must be defined")
})
})
describe("delete", () => {
it("should delete the authUsers given an id successfully", async () => {
it("should delete the authIdentities given an id successfully", async () => {
const id = "test-id"
await service.delete([id])
const authUsers = await service.list({
const authIdentities = await service.list({
id: [id],
})
expect(authUsers).toHaveLength(0)
expect(authIdentities).toHaveLength(0)
})
})
@@ -181,11 +181,11 @@ moduleIntegrationTestRunner({
}
expect(error.message).toEqual(
'AuthUser with id "does-not-exist" not found'
'AuthIdentity with id "does-not-exist" not found'
)
})
it("should update authUser", async () => {
it("should update authIdentity", async () => {
const id = "test-id"
await service.update([
@@ -195,8 +195,8 @@ moduleIntegrationTestRunner({
},
])
const [authUser] = await service.list({ id: [id] })
expect(authUser).toEqual(
const [authIdentity] = await service.list({ id: [id] })
expect(authIdentity).toEqual(
expect.objectContaining({
provider_metadata: { email: "test@email.com" },
})
@@ -205,7 +205,7 @@ moduleIntegrationTestRunner({
})
describe("create", () => {
it("should create a authUser successfully", async () => {
it("should create a authIdentity successfully", async () => {
await service.create([
{
id: "test",
@@ -215,11 +215,11 @@ moduleIntegrationTestRunner({
},
])
const [authUser] = await service.list({
const [authIdentity] = await service.list({
id: ["test"],
})
expect(authUser).toEqual(
expect(authIdentity).toEqual(
expect.objectContaining({
id: "test",
})
@@ -1,6 +1,6 @@
import { IAuthModuleService } from "@medusajs/types"
import { Modules } from "@medusajs/modules-sdk"
import { createAuthUsers } from "../../../__fixtures__/auth-user"
import { createAuthIdentities } from "../../../__fixtures__/auth-identity"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
@@ -11,16 +11,16 @@ moduleIntegrationTestRunner({
MikroOrmWrapper,
service,
}: SuiteOptions<IAuthModuleService>) => {
describe("AuthModuleService - AuthUser", () => {
describe("AuthModuleService - AuthIdentity", () => {
beforeEach(async () => {
await createAuthUsers(MikroOrmWrapper.forkManager())
await createAuthIdentities(MikroOrmWrapper.forkManager())
})
describe("listAuthUsers", () => {
it("should list authUsers", async () => {
const authUsers = await service.list()
describe("listAuthIdentities", () => {
it("should list authIdentities", async () => {
const authIdentities = await service.list()
expect(authUsers).toEqual([
expect(authIdentities).toEqual([
expect.objectContaining({
provider: "store",
}),
@@ -33,24 +33,24 @@ moduleIntegrationTestRunner({
])
})
it("should list authUsers by id", async () => {
const authUsers = await service.list({
it("should list authIdentities by id", async () => {
const authIdentities = await service.list({
id: ["test-id"],
})
expect(authUsers).toEqual([
expect(authIdentities).toEqual([
expect.objectContaining({
id: "test-id",
}),
])
})
it("should list authUsers by provider", async () => {
const authUsers = await service.list({
it("should list authIdentities by provider", async () => {
const authIdentities = await service.list({
provider: "manual",
})
expect(authUsers).toEqual([
expect(authIdentities).toEqual([
expect.objectContaining({
id: "test-id",
}),
@@ -61,12 +61,12 @@ moduleIntegrationTestRunner({
})
})
describe("listAndCountAuthUsers", () => {
it("should list and count authUsers", async () => {
const [authUsers, count] = await service.listAndCount()
describe("listAndCountAuthIdentities", () => {
it("should list and count authIdentities", async () => {
const [authIdentities, count] = await service.listAndCount()
expect(count).toEqual(3)
expect(authUsers).toEqual([
expect(authIdentities).toEqual([
expect.objectContaining({
provider: "store",
}),
@@ -79,13 +79,13 @@ moduleIntegrationTestRunner({
])
})
it("should listAndCount authUsers by provider_id", async () => {
const [authUsers, count] = await service.listAndCount({
it("should listAndCount authIdentities by provider_id", async () => {
const [authIdentities, count] = await service.listAndCount({
provider: "manual",
})
expect(count).toEqual(2)
expect(authUsers).toEqual([
expect(authIdentities).toEqual([
expect.objectContaining({
id: "test-id",
}),
@@ -96,20 +96,20 @@ moduleIntegrationTestRunner({
})
})
describe("retrieveAuthUser", () => {
describe("retrieveAuthIdentity", () => {
const id = "test-id"
it("should return an authUser for the given id", async () => {
const authUser = await service.retrieve(id)
it("should return an authIdentity for the given id", async () => {
const authIdentity = await service.retrieve(id)
expect(authUser).toEqual(
expect(authIdentity).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when an authUser with the given id does not exist", async () => {
it("should throw an error when an authIdentity with the given id does not exist", async () => {
let error
try {
@@ -119,22 +119,22 @@ moduleIntegrationTestRunner({
}
expect(error.message).toEqual(
"AuthUser with id: does-not-exist was not found"
"AuthIdentity with id: does-not-exist was not found"
)
})
it("should not return an authUser with password hash", async () => {
const authUser = await service.retrieve("test-id-1")
it("should not return an authIdentity with password hash", async () => {
const authIdentity = await service.retrieve("test-id-1")
expect(authUser).toEqual(
expect(authIdentity).toEqual(
expect.objectContaining({
id: "test-id-1",
})
)
expect(authUser["password_hash"]).toEqual(undefined)
expect(authIdentity["password_hash"]).toEqual(undefined)
})
it("should throw an error when a authUserId is not provided", async () => {
it("should throw an error when a authIdentityId is not provided", async () => {
let error
try {
@@ -143,35 +143,35 @@ moduleIntegrationTestRunner({
error = e
}
expect(error.message).toEqual("authUser - id must be defined")
expect(error.message).toEqual("authIdentity - id must be defined")
})
it("should return authUser based on config select param", async () => {
const authUser = await service.retrieve(id, {
it("should return authIdentity based on config select param", async () => {
const authIdentity = await service.retrieve(id, {
select: ["id"],
})
expect(authUser).toEqual({
expect(authIdentity).toEqual({
id,
})
})
})
describe("deleteAuthUser", () => {
describe("deleteAuthIdentity", () => {
const id = "test-id"
it("should delete the authUsers given an id successfully", async () => {
it("should delete the authIdentities given an id successfully", async () => {
await service.delete([id])
const authUsers = await service.list({
const authIdentities = await service.list({
id: [id],
})
expect(authUsers).toHaveLength(0)
expect(authIdentities).toHaveLength(0)
})
})
describe("updateAuthUser", () => {
describe("updateAuthIdentity", () => {
const id = "test-id"
it("should throw an error when a id does not exist", async () => {
@@ -188,11 +188,11 @@ moduleIntegrationTestRunner({
}
expect(error.message).toEqual(
'AuthUser with id "does-not-exist" not found'
'AuthIdentity with id "does-not-exist" not found'
)
})
it("should update authUser", async () => {
it("should update authIdentity", async () => {
await service.update([
{
id,
@@ -200,8 +200,8 @@ moduleIntegrationTestRunner({
},
])
const [authUser] = await service.list({ id: [id] })
expect(authUser).toEqual(
const [authIdentity] = await service.list({ id: [id] })
expect(authIdentity).toEqual(
expect.objectContaining({
provider_metadata: { email: "test@email.com" },
})
@@ -209,8 +209,8 @@ moduleIntegrationTestRunner({
})
})
describe("createAuthUser", () => {
it("should create a authUser successfully", async () => {
describe("createAuthIdentity", () => {
it("should create a authIdentity successfully", async () => {
await service.create([
{
id: "test",
@@ -220,12 +220,12 @@ moduleIntegrationTestRunner({
},
])
const [authUser, count] = await service.listAndCount({
const [authIdentity, count] = await service.listAndCount({
id: ["test"],
})
expect(count).toEqual(1)
expect(authUser[0]).toEqual(
expect(authIdentity[0]).toEqual(
expect.objectContaining({
id: "test",
})
@@ -1,4 +1,4 @@
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
import { Modules } from "@medusajs/modules-sdk"
import { IAuthModuleService } from "@medusajs/types"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
@@ -2,12 +2,12 @@ import { MedusaModule, Modules } from "@medusajs/modules-sdk"
import { IAuthModuleService } from "@medusajs/types"
import Scrypt from "scrypt-kdf"
import { createAuthUsers } from "../../../__fixtures__/auth-user"
import { createAuthIdentities } from "../../../__fixtures__/auth-identity"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
const seedDefaultData = async (manager) => {
await createAuthUsers(manager)
await createAuthIdentities(manager)
}
moduleIntegrationTestRunner({
@@ -37,7 +37,7 @@ moduleIntegrationTestRunner({
).toString("base64")
await seedDefaultData(MikroOrmWrapper.forkManager())
await createAuthUsers(MikroOrmWrapper.forkManager(), [
await createAuthIdentities(MikroOrmWrapper.forkManager(), [
// Add authenticated user
{
provider: "emailpass",
@@ -59,7 +59,7 @@ moduleIntegrationTestRunner({
expect(res).toEqual({
success: true,
authUser: expect.objectContaining({
authIdentity: expect.objectContaining({
entity_id: email,
provider_metadata: {},
}),
@@ -102,7 +102,7 @@ moduleIntegrationTestRunner({
).toString("base64")
await seedDefaultData(MikroOrmWrapper.forkManager())
await createAuthUsers(MikroOrmWrapper.forkManager(), [
await createAuthIdentities(MikroOrmWrapper.forkManager(), [
// Add authenticated user
{
provider: "emailpass",