Skip to content

Commit

Permalink
Feat(auth): Rename authentication to auth (#6229)
Browse files Browse the repository at this point in the history
**What**
- rename `authenticationModule` -> `authModule`
  • Loading branch information
pKorsholm authored Jan 29, 2024
1 parent a41aad4 commit 512b041
Show file tree
Hide file tree
Showing 70 changed files with 244 additions and 146 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @medusajs/authentication
# @medusajs/auth

## 0.0.2

Expand Down
3 changes: 3 additions & 0 deletions packages/auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Auth Module

The Auth Module is Medusa’s authentication engine engine. It provides functions to authenticate users through identity providers and store metadata about users that can be used for authorization purposes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { IAuthenticationModuleService } from "@medusajs/types"
import { IAuthModuleService } from "@medusajs/types"
import { MikroOrmWrapper } from "../../../utils"
import { Modules } from "@medusajs/modules-sdk"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
import { createAuthUsers } from "../../../__fixtures__/auth-user"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils/dist"
import { Modules } from "@medusajs/modules-sdk"
import { initModules } from "medusa-test-utils"

jest.setTimeout(30000)

describe("AuthenticationModuleService - AuthProvider", () => {
let service: IAuthenticationModuleService
describe("AuthModuleService - AuthProvider", () => {
let service: IAuthModuleService
let testManager: SqlEntityManager
let shutdownFunc: () => Promise<void>

Expand All @@ -19,7 +19,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {

const { medusaApp, shutdown } = await initModules(initModulesConfig)

service = medusaApp.modules[Modules.AUTHENTICATION]
service = medusaApp.modules[Modules.AUTH]

shutdownFunc = shutdown
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"

import { IAuthModuleService } from "@medusajs/types"
import { MikroOrmWrapper } from "../../../utils"
import { Modules } from "@medusajs/modules-sdk"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
import { createAuthUsers } from "../../../__fixtures__/auth-user"
import { IAuthenticationModuleService } from "@medusajs/types"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils/dist"
import { Modules } from "@medusajs/modules-sdk"
import { initModules } from "medusa-test-utils"

jest.setTimeout(30000)

describe("AuthenticationModuleService - AuthUser", () => {
let service: IAuthenticationModuleService
describe("AuthModuleService - AuthUser", () => {
let service: IAuthModuleService
let testManager: SqlEntityManager
let shutdownFunc: () => Promise<void>

Expand All @@ -20,7 +19,7 @@ describe("AuthenticationModuleService - AuthUser", () => {

const { medusaApp, shutdown } = await initModules(initModulesConfig)

service = medusaApp.modules[Modules.AUTHENTICATION]
service = medusaApp.modules[Modules.AUTH]

shutdownFunc = shutdown
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { MedusaModule, Modules } from "@medusajs/modules-sdk"

import { IAuthenticationModuleService } from "@medusajs/types"
import { IAuthModuleService } from "@medusajs/types"
import { MikroOrmWrapper } from "../../../utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils/dist"
import { initModules } from "medusa-test-utils"

jest.setTimeout(30000)

describe("AuthenticationModuleService - AuthProvider", () => {
let service: IAuthenticationModuleService
describe("AuthModuleService - AuthProvider", () => {
let service: IAuthModuleService
let testManager: SqlEntityManager
let shutdownFunc: () => Promise<void>

Expand All @@ -19,7 +19,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {

const { medusaApp, shutdown } = await initModules(initModulesConfig)

service = medusaApp.modules[Modules.AUTHENTICATION]
service = medusaApp.modules[Modules.AUTH]

shutdownFunc = shutdown
})
Expand Down Expand Up @@ -50,8 +50,8 @@ describe("AuthenticationModuleService - AuthProvider", () => {
expect(serialized).toEqual(
expect.arrayContaining([
expect.objectContaining({
provider: "usernamePassword",
name: "Username/Password Authentication",
provider: "emailpass",
name: "Email/Password Authentication",
}),
expect.objectContaining({
provider: "google",
Expand All @@ -71,7 +71,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
},
])

const { success, error } = await service.authenticate("notRegistered", {})
const { success, error } = await service.authenticate("notRegistered", {} as any)

expect(success).toBe(false)
expect(error).toEqual(
Expand All @@ -81,15 +81,15 @@ describe("AuthenticationModuleService - AuthProvider", () => {

it("fails to authenticate using a valid provider with an invalid scope", async () => {
const { success, error } = await service.authenticate(
"usernamePassword",
"emailpass",
{
scope: "non-existing",
}
} as any
)

expect(success).toBe(false)
expect(error).toEqual(
`Scope "non-existing" is not valid for provider usernamePassword`
`Scope "non-existing" is not valid for provider emailpass`
)
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AuthenticationInput, IAuthModuleService } from "@medusajs/types"
import { MedusaModule, Modules } from "@medusajs/modules-sdk"

import { IAuthenticationModuleService } from "@medusajs/types"
import { MikroOrmWrapper } from "../../../utils"
import Scrypt from "scrypt-kdf"
import { SqlEntityManager } from "@mikro-orm/postgresql"
Expand All @@ -15,8 +15,8 @@ const seedDefaultData = async (testManager) => {
await createAuthUsers(testManager)
}

describe("AuthenticationModuleService - AuthProvider", () => {
let service: IAuthenticationModuleService
describe("AuthModuleService - AuthProvider", () => {
let service: IAuthModuleService
let testManager: SqlEntityManager
let shutdownFunc: () => Promise<void>

Expand All @@ -25,7 +25,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {

const { medusaApp, shutdown } = await initModules(initModulesConfig)

service = medusaApp.modules[Modules.AUTHENTICATION]
service = medusaApp.modules[Modules.AUTH]

shutdownFunc = shutdown
})
Expand Down Expand Up @@ -60,15 +60,15 @@ describe("AuthenticationModuleService - AuthProvider", () => {
await createAuthUsers(testManager, [
// Add authenticated user
{
provider: "usernamePassword",
provider: "emailpass",
entity_id: email,
provider_metadata: {
password: passwordHash,
},
},
])

const res = await service.authenticate("usernamePassword", {
const res = await service.authenticate("emailpass", {
body: {
email: "[email protected]",
password: password,
Expand All @@ -90,7 +90,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {

await seedDefaultData(testManager)

const res = await service.authenticate("usernamePassword", {
const res = await service.authenticate("emailpass", {
body: { email: "[email protected]" },
scope: "store",
})
Expand All @@ -104,7 +104,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
it("fails when no email is given", async () => {
await seedDefaultData(testManager)

const res = await service.authenticate("usernamePassword", {
const res = await service.authenticate("emailpass", {
body: { password: "supersecret" },
scope: "store",
})
Expand All @@ -126,15 +126,15 @@ describe("AuthenticationModuleService - AuthProvider", () => {
await createAuthUsers(testManager, [
// Add authenticated user
{
provider: "usernamePassword",
provider: "emailpass",
entity_id: email,
provider_metadata: {
password_hash: passwordHash,
},
},
])

const res = await service.authenticate("usernamePassword", {
const res = await service.authenticate("emailpass", {
body: {
email: "[email protected]",
password: "password",
Expand Down
6 changes: 6 additions & 0 deletions packages/auth/integration-tests/setup-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if (typeof process.env.DB_TEMP_NAME === "undefined") {
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
process.env.DB_TEMP_NAME = `medusa-auth-integration-${tempName}`
}

process.env.MEDUSA_AUTH_DB_SCHEMA = "public"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { ModuleServiceInitializeOptions } from "@medusajs/types"

export const databaseOptions: ModuleServiceInitializeOptions["database"] = {
schema: "public",
clientUrl: "medusa-authentication-test",
clientUrl: "medusa-auth-test",
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TestDatabaseUtils } from "medusa-test-utils"
import * as AuthModels from "@models"

import * as AuthenticationModels from "@models"
import { TestDatabaseUtils } from "medusa-test-utils"

const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = AuthenticationModels as unknown as any[]
const mikroOrmEntities = AuthModels as unknown as any[]

export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
mikroOrmEntities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export function getInitModuleConfig() {
defaultAdapterOptions: {
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_AUTHENTICATION_DB_SCHEMA,
schema: process.env.MEDUSA_AUTH_DB_SCHEMA,
},
},
providers: [
{
name: "usernamePassword",
name: "emailpass",
scopes: {
admin: {},
store: {},
Expand All @@ -24,8 +24,8 @@ export function getInitModuleConfig() {
const injectedDependencies = {}

const modulesConfig_ = {
[Modules.AUTHENTICATION]: {
definition: ModulesDefinition[Modules.AUTHENTICATION],
[Modules.AUTH]: {
definition: ModulesDefinition[Modules.AUTH],
options: moduleOptions,
},
}
Expand All @@ -35,7 +35,7 @@ export function getInitModuleConfig() {
modulesConfig: modulesConfig_,
databaseConfig: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_AUTHENTICATION_DB_SCHEMA,
schema: process.env.MEDUSA_AUTH_DB_SCHEMA,
},
joinerConfig: [],
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import * as entities from "./src/models"
module.exports = {
entities: Object.values(entities),
schema: "public",
clientUrl: "postgres://postgres@localhost/medusa-authentication",
clientUrl: "postgres://postgres@localhost/medusa-auth",
type: "postgresql",
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@medusajs/authentication",
"name": "@medusajs/auth",
"version": "0.0.2",
"description": "Medusa Authentication module",
"description": "Medusa Auth module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand All @@ -11,12 +11,12 @@
"node": ">=16"
},
"bin": {
"medusa-authentication-seed": "dist/scripts/bin/run-seed.js"
"medusa-auth-seed": "dist/scripts/bin/run-seed.js"
},
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/authentication"
"directory": "packages/auth"
},
"publishConfig": {
"access": "public"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
MedusaModule,
Modules,
} from "@medusajs/modules-sdk"
import { IAuthenticationModuleService, ModulesSdkTypes } from "@medusajs/types"
import { IAuthModuleService, ModulesSdkTypes } from "@medusajs/types"

import { InitializeModuleInjectableDependencies } from "../types"
import { moduleDefinition } from "../module-definition"
Expand All @@ -16,16 +16,16 @@ export const initialize = async (
| ModulesSdkTypes.ModuleServiceInitializeOptions
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions,
injectedDependencies?: InitializeModuleInjectableDependencies
): Promise<IAuthenticationModuleService> => {
const loaded = await MedusaModule.bootstrap<IAuthenticationModuleService>({
moduleKey: Modules.AUTHENTICATION,
defaultPath: MODULE_PACKAGE_NAMES[Modules.AUTHENTICATION],
): Promise<IAuthModuleService> => {
const loaded = await MedusaModule.bootstrap<IAuthModuleService>({
moduleKey: Modules.AUTH,
defaultPath: MODULE_PACKAGE_NAMES[Modules.AUTH],
declaration: options as
| InternalModuleDeclaration
| ExternalModuleDeclaration, // TODO: Add provider configuration
injectedDependencies,
moduleExports: moduleDefinition,
})

return loaded[Modules.AUTHENTICATION]
return loaded[Modules.AUTH]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"
import { AuthUser } from "@models"
import { MapToConfig } from "@medusajs/utils"
import { ModuleJoinerConfig } from "@medusajs/types"
import { Modules } from "@medusajs/modules-sdk"

export const LinkableKeys = {
auth_user_id: AuthUser.name,
Expand All @@ -19,7 +19,7 @@ Object.entries(LinkableKeys).forEach(([key, value]) => {
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap

export const joinerConfig: ModuleJoinerConfig = {
serviceName: Modules.AUTHENTICATION,
serviceName: Modules.AUTH,
primaryKeys: ["id"],
linkableKeys: LinkableKeys,
alias: {
Expand Down
Loading

0 comments on commit 512b041

Please sign in to comment.