feat(medusa-react,medusa,utils): add users/me endpoint + add missing specs (#6441)
**what:** - adds /me endpoint - adds fixes to routes - adds specs for auth endpoint - updates dotenv package versions Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
This commit is contained in:
co-authored by
Philip Korsholm
parent
883cb0dca7
commit
8dad2b51a2
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
"medusa-react": patch
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
"@medusajs/utils": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa-react,medusa,utils): fix login for medusa v2 admin next dashboard
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { IAuthModuleService, ICustomerModuleService } from "@medusajs/types"
|
||||||
|
import path from "path"
|
||||||
|
import Scrypt from "scrypt-kdf"
|
||||||
|
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||||
|
import { useApi } from "../../../../environment-helpers/use-api"
|
||||||
|
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||||
|
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||||
|
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||||
|
|
||||||
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
|
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||||
|
|
||||||
|
describe("POST /auth/emailpass", () => {
|
||||||
|
let dbConnection
|
||||||
|
let appContainer
|
||||||
|
let shutdownServer
|
||||||
|
let customerModuleService: ICustomerModuleService
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||||
|
dbConnection = await initDb({ cwd, env } as any)
|
||||||
|
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||||
|
appContainer = getContainer()
|
||||||
|
customerModuleService = appContainer.resolve(
|
||||||
|
ModuleRegistrationName.CUSTOMER
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.shutdown()
|
||||||
|
await shutdownServer()
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await adminSeeder(dbConnection)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.teardown()
|
||||||
|
})
|
||||||
|
|
||||||
|
const password = "supersecret"
|
||||||
|
const email = "test@test.com"
|
||||||
|
|
||||||
|
it("should return a token on successful login", async () => {
|
||||||
|
const passwordHash = (
|
||||||
|
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||||
|
).toString("base64")
|
||||||
|
const authService: IAuthModuleService = appContainer.resolve(
|
||||||
|
ModuleRegistrationName.AUTH
|
||||||
|
)
|
||||||
|
|
||||||
|
await authService.create({
|
||||||
|
provider: "emailpass",
|
||||||
|
entity_id: email,
|
||||||
|
scope: "admin",
|
||||||
|
provider_metadata: {
|
||||||
|
password: passwordHash,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const api = useApi() as any
|
||||||
|
const response = await api
|
||||||
|
.post(`/auth/admin/emailpass`, {
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
})
|
||||||
|
.catch((e) => e)
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(response.data).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
token: expect.any(String),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should throw an error upon incorrect password", async () => {
|
||||||
|
const passwordHash = (
|
||||||
|
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||||
|
).toString("base64")
|
||||||
|
const authService: IAuthModuleService = appContainer.resolve(
|
||||||
|
ModuleRegistrationName.AUTH
|
||||||
|
)
|
||||||
|
|
||||||
|
await authService.create({
|
||||||
|
provider: "emailpass",
|
||||||
|
entity_id: email,
|
||||||
|
scope: "admin",
|
||||||
|
provider_metadata: {
|
||||||
|
password: passwordHash,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const api = useApi() as any
|
||||||
|
const error = await api
|
||||||
|
.post(`/auth/admin/emailpass`, {
|
||||||
|
email: email,
|
||||||
|
password: "incorrect-password",
|
||||||
|
})
|
||||||
|
.catch((e) => e)
|
||||||
|
|
||||||
|
expect(error.response.status).toEqual(401)
|
||||||
|
expect(error.response.data).toEqual({
|
||||||
|
type: "unauthorized",
|
||||||
|
message: "Invalid email or password",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it.skip("should throw an error upon logging in with a non existing auth user", async () => {
|
||||||
|
const passwordHash = (
|
||||||
|
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||||
|
).toString("base64")
|
||||||
|
|
||||||
|
const api = useApi() as any
|
||||||
|
const error = await api
|
||||||
|
.post(`/auth/admin/emailpass`, {
|
||||||
|
email: "should-not-exist",
|
||||||
|
password: "should-not-exist",
|
||||||
|
})
|
||||||
|
.catch((e) => e)
|
||||||
|
|
||||||
|
// TODO: This is creating a user with a scope of admin. The client consuming the auth service
|
||||||
|
// should reject this if its not being created by an admin user
|
||||||
|
expect(error.response.status).toEqual(401)
|
||||||
|
expect(error.response.data).toEqual({
|
||||||
|
type: "unauthorized",
|
||||||
|
message: "Invalid email or password",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { initDb, useDb } from "../../../environment-helpers/use-db"
|
||||||
|
|
||||||
|
import { AxiosInstance } from "axios"
|
||||||
|
import path from "path"
|
||||||
|
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
|
||||||
|
import { useApi } from "../../../environment-helpers/use-api"
|
||||||
|
import { createAdminUser } from "../../helpers/create-admin-user"
|
||||||
|
|
||||||
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
|
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||||
|
const adminHeaders = {
|
||||||
|
headers: { "x-medusa-access-token": "test_token" },
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("POST /admin/users/me", () => {
|
||||||
|
let dbConnection
|
||||||
|
let shutdownServer
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||||
|
dbConnection = await initDb({ cwd, env } as any)
|
||||||
|
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await createAdminUser(dbConnection, adminHeaders)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.shutdown()
|
||||||
|
await shutdownServer()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.teardown()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("gets the current user", async () => {
|
||||||
|
const api = useApi()! as AxiosInstance
|
||||||
|
|
||||||
|
const response = await api.get(`/admin/users/me`, adminHeaders)
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(response.data).toEqual({
|
||||||
|
user: expect.objectContaining({ id: "admin_user" }),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -5,6 +5,7 @@ const DB_PASSWORD = process.env.DB_PASSWORD
|
|||||||
const DB_NAME = process.env.DB_TEMP_NAME
|
const DB_NAME = process.env.DB_TEMP_NAME
|
||||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`
|
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`
|
||||||
process.env.POSTGRES_URL = DB_URL
|
process.env.POSTGRES_URL = DB_URL
|
||||||
|
process.env.LOG_LEVEL = "error"
|
||||||
|
|
||||||
const enableMedusaV2 = process.env.MEDUSA_FF_MEDUSA_V2 == "true"
|
const enableMedusaV2 = process.env.MEDUSA_FF_MEDUSA_V2 == "true"
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
"@rollup/plugin-replace": "5.0.2",
|
"@rollup/plugin-replace": "5.0.2",
|
||||||
"@rollup/plugin-virtual": "^3.0.1",
|
"@rollup/plugin-virtual": "^3.0.1",
|
||||||
"commander": "^10.0.0",
|
"commander": "^10.0.0",
|
||||||
"dotenv": "16.3.1",
|
"dotenv": "16.4.5",
|
||||||
"esbuild": "0.17.18",
|
"esbuild": "0.17.18",
|
||||||
"express": "4.18.2",
|
"express": "4.18.2",
|
||||||
"fs-extra": "11.1.0",
|
"fs-extra": "11.1.0",
|
||||||
|
|||||||
@@ -55,7 +55,7 @@
|
|||||||
"@mikro-orm/migrations": "5.9.7",
|
"@mikro-orm/migrations": "5.9.7",
|
||||||
"@mikro-orm/postgresql": "5.9.7",
|
"@mikro-orm/postgresql": "5.9.7",
|
||||||
"awilix": "^8.0.0",
|
"awilix": "^8.0.0",
|
||||||
"dotenv": "16.3.1",
|
"dotenv": "16.4.5",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"knex": "2.4.2",
|
"knex": "2.4.2",
|
||||||
"scrypt-kdf": "^2.0.1",
|
"scrypt-kdf": "^2.0.1",
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
MedusaError,
|
||||||
|
remoteQueryObjectFromString,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
import {
|
||||||
|
AuthenticatedMedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
} from "../../../../types/routing"
|
||||||
|
|
||||||
|
export const GET = async (
|
||||||
|
req: AuthenticatedMedusaRequest,
|
||||||
|
res: MedusaResponse
|
||||||
|
) => {
|
||||||
|
const id = req.auth.app_metadata.user_id
|
||||||
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
throw new MedusaError(MedusaError.Types.NOT_FOUND, `User ID not found`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const query = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "user",
|
||||||
|
variables: { id },
|
||||||
|
fields: req.retrieveConfig.select as string[],
|
||||||
|
})
|
||||||
|
|
||||||
|
const [user] = await remoteQuery(query)
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.NOT_FOUND,
|
||||||
|
`User with id: ${id} was not found`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({ user })
|
||||||
|
}
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
|
|
||||||
|
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||||
import {
|
import {
|
||||||
AdminCreateUserRequest,
|
AdminCreateUserRequest,
|
||||||
AdminGetUsersParams,
|
AdminGetUsersParams,
|
||||||
AdminGetUsersUserParams,
|
AdminGetUsersUserParams,
|
||||||
AdminUpdateUserRequest,
|
AdminUpdateUserRequest,
|
||||||
} from "./validators"
|
} from "./validators"
|
||||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
|
||||||
|
|
||||||
import { MiddlewareRoute } from "../../../types/middlewares"
|
import { MiddlewareRoute } from "../../../types/middlewares"
|
||||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||||
@@ -39,6 +39,16 @@ export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
method: ["GET"],
|
||||||
|
matcher: "/admin/users/me",
|
||||||
|
middlewares: [
|
||||||
|
transformQuery(
|
||||||
|
AdminGetUsersUserParams,
|
||||||
|
QueryConfig.retrieveTransformQueryConfig
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
method: ["POST"],
|
method: ["POST"],
|
||||||
matcher: "/admin/users/:id",
|
matcher: "/admin/users/:id",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import jwt from "jsonwebtoken"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
import { AuthenticationInput, IAuthModuleService } from "@medusajs/types"
|
import { AuthenticationInput, IAuthModuleService } from "@medusajs/types"
|
||||||
import { MedusaError } from "@medusajs/utils"
|
import { MedusaError } from "@medusajs/utils"
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import jwt from "jsonwebtoken"
|
||||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||||
|
|
||||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||||
@@ -23,6 +23,7 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
|||||||
const authResult = await service.authenticate(authProvider, authData)
|
const authResult = await service.authenticate(authProvider, authData)
|
||||||
|
|
||||||
const { success, error, authUser, location } = authResult
|
const { success, error, authUser, location } = authResult
|
||||||
|
|
||||||
if (location) {
|
if (location) {
|
||||||
res.redirect(location)
|
res.redirect(location)
|
||||||
return
|
return
|
||||||
@@ -30,7 +31,6 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
|||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
const { jwt_secret } = req.scope.resolve("configModule").projectConfig
|
const { jwt_secret } = req.scope.resolve("configModule").projectConfig
|
||||||
|
|
||||||
const token = jwt.sign(authUser, jwt_secret)
|
const token = jwt.sign(authUser, jwt_secret)
|
||||||
|
|
||||||
return res.status(200).json({ token })
|
return res.status(200).json({ token })
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import path from "path"
|
|
||||||
import { FeatureFlagUtils, FlagRouter } from "@medusajs/utils"
|
import { FeatureFlagUtils, FlagRouter } from "@medusajs/utils"
|
||||||
import { AwilixContainer } from "awilix"
|
import { AwilixContainer } from "awilix"
|
||||||
import bodyParser from "body-parser"
|
import bodyParser from "body-parser"
|
||||||
import { Express } from "express"
|
import { Express } from "express"
|
||||||
|
import path from "path"
|
||||||
import qs from "qs"
|
import qs from "qs"
|
||||||
import { RoutesLoader } from "./helpers/routing"
|
|
||||||
import routes from "../api"
|
import routes from "../api"
|
||||||
import { ConfigModule } from "../types/global"
|
import { ConfigModule } from "../types/global"
|
||||||
|
import { RoutesLoader } from "./helpers/routing"
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
app: Express
|
app: Express
|
||||||
|
|||||||
@@ -307,6 +307,7 @@ export class RoutesLoader {
|
|||||||
shouldRequireAdminAuth: false,
|
shouldRequireAdminAuth: false,
|
||||||
shouldRequireCustomerAuth: false,
|
shouldRequireCustomerAuth: false,
|
||||||
shouldAppendCustomer: false,
|
shouldAppendCustomer: false,
|
||||||
|
shouldAppendAuthCors: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -343,6 +344,10 @@ export class RoutesLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (route.startsWith("/auth") && shouldAddCors) {
|
||||||
|
config.shouldAppendAuthCors = true
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldRequireAuth && route.startsWith("/store/me")) {
|
if (shouldRequireAuth && route.startsWith("/store/me")) {
|
||||||
config.shouldRequireCustomerAuth = shouldRequireAuth
|
config.shouldRequireCustomerAuth = shouldRequireAuth
|
||||||
}
|
}
|
||||||
@@ -612,6 +617,21 @@ export class RoutesLoader {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (descriptor.config.shouldAppendAuthCors) {
|
||||||
|
/**
|
||||||
|
* Apply the auth cors
|
||||||
|
*/
|
||||||
|
this.router.use(
|
||||||
|
descriptor.route,
|
||||||
|
cors({
|
||||||
|
origin: parseCorsOrigins(
|
||||||
|
this.configModule.projectConfig.auth_cors || ""
|
||||||
|
),
|
||||||
|
credentials: true,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (descriptor.config.shouldAppendStoreCors) {
|
if (descriptor.config.shouldAppendStoreCors) {
|
||||||
/**
|
/**
|
||||||
* Apply the store cors
|
* Apply the store cors
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ export type RouteConfig = {
|
|||||||
shouldAppendCustomer?: boolean
|
shouldAppendCustomer?: boolean
|
||||||
shouldAppendAdminCors?: boolean
|
shouldAppendAdminCors?: boolean
|
||||||
shouldAppendStoreCors?: boolean
|
shouldAppendStoreCors?: boolean
|
||||||
|
shouldAppendAuthCors?: boolean
|
||||||
routes?: RouteImplementation[]
|
routes?: RouteImplementation[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,9 @@ import {
|
|||||||
ModulesDefinition,
|
ModulesDefinition,
|
||||||
} from "@medusajs/modules-sdk"
|
} from "@medusajs/modules-sdk"
|
||||||
import { MODULE_RESOURCE_TYPE } from "@medusajs/types"
|
import { MODULE_RESOURCE_TYPE } from "@medusajs/types"
|
||||||
import { Express, NextFunction, Request, Response } from "express"
|
|
||||||
|
|
||||||
import databaseLoader, { dataSource } from "./database"
|
|
||||||
import pluginsLoader, { registerPluginModels } from "./plugins"
|
|
||||||
|
|
||||||
import { ContainerRegistrationKeys, isString } from "@medusajs/utils"
|
import { ContainerRegistrationKeys, isString } from "@medusajs/utils"
|
||||||
import { asValue } from "awilix"
|
import { asValue } from "awilix"
|
||||||
|
import { Express, NextFunction, Request, Response } from "express"
|
||||||
import { createMedusaContainer } from "medusa-core-utils"
|
import { createMedusaContainer } from "medusa-core-utils"
|
||||||
import { track } from "medusa-telemetry"
|
import { track } from "medusa-telemetry"
|
||||||
import { EOL } from "os"
|
import { EOL } from "os"
|
||||||
@@ -19,6 +15,7 @@ import { v4 } from "uuid"
|
|||||||
import { MedusaContainer } from "../types/global"
|
import { MedusaContainer } from "../types/global"
|
||||||
import apiLoader from "./api"
|
import apiLoader from "./api"
|
||||||
import loadConfig from "./config"
|
import loadConfig from "./config"
|
||||||
|
import databaseLoader, { dataSource } from "./database"
|
||||||
import defaultsLoader from "./defaults"
|
import defaultsLoader from "./defaults"
|
||||||
import expressLoader from "./express"
|
import expressLoader from "./express"
|
||||||
import featureFlagsLoader from "./feature-flags"
|
import featureFlagsLoader from "./feature-flags"
|
||||||
@@ -27,6 +24,7 @@ import loadMedusaApp, { mergeDefaultModules } from "./medusa-app"
|
|||||||
import modelsLoader from "./models"
|
import modelsLoader from "./models"
|
||||||
import passportLoader from "./passport"
|
import passportLoader from "./passport"
|
||||||
import pgConnectionLoader from "./pg-connection"
|
import pgConnectionLoader from "./pg-connection"
|
||||||
|
import pluginsLoader, { registerPluginModels } from "./plugins"
|
||||||
import redisLoader from "./redis"
|
import redisLoader from "./redis"
|
||||||
import repositoriesLoader from "./repositories"
|
import repositoriesLoader from "./repositories"
|
||||||
import searchIndexLoader from "./search-index"
|
import searchIndexLoader from "./search-index"
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
import { AuthUserDTO } from "@medusajs/types"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { ApiKeyDTO, AuthUserDTO, IApiKeyModuleService } from "@medusajs/types"
|
||||||
|
import { stringEqualsOrRegexMatch } from "@medusajs/utils"
|
||||||
|
import { NextFunction, RequestHandler } from "express"
|
||||||
|
import jwt, { JwtPayload } from "jsonwebtoken"
|
||||||
import {
|
import {
|
||||||
AuthenticatedMedusaRequest,
|
AuthenticatedMedusaRequest,
|
||||||
MedusaRequest,
|
MedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../types/routing"
|
} from "../types/routing"
|
||||||
import { NextFunction, RequestHandler } from "express"
|
|
||||||
import jwt, { JwtPayload } from "jsonwebtoken"
|
|
||||||
|
|
||||||
import { stringEqualsOrRegexMatch } from "@medusajs/utils"
|
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
|
||||||
import { IApiKeyModuleService } from "@medusajs/types"
|
|
||||||
import { ApiKeyDTO } from "@medusajs/types"
|
|
||||||
|
|
||||||
const SESSION_AUTH = "session"
|
const SESSION_AUTH = "session"
|
||||||
const BEARER_AUTH = "bearer"
|
const BEARER_AUTH = "bearer"
|
||||||
@@ -68,6 +65,7 @@ export const authenticate = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isMedusaScope = isAdminScope(authScope) || isStoreScope(authScope)
|
const isMedusaScope = isAdminScope(authScope) || isStoreScope(authScope)
|
||||||
|
|
||||||
const isRegistered =
|
const isRegistered =
|
||||||
!isMedusaScope ||
|
!isMedusaScope ||
|
||||||
(authUser?.app_metadata?.user_id &&
|
(authUser?.app_metadata?.user_id &&
|
||||||
@@ -85,6 +83,7 @@ export const authenticate = (
|
|||||||
app_metadata: authUser.app_metadata,
|
app_metadata: authUser.app_metadata,
|
||||||
scope: authUser.scope,
|
scope: authUser.scope,
|
||||||
}
|
}
|
||||||
|
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,7 @@
|
|||||||
"@mikro-orm/migrations": "5.9.7",
|
"@mikro-orm/migrations": "5.9.7",
|
||||||
"@mikro-orm/postgresql": "5.9.7",
|
"@mikro-orm/postgresql": "5.9.7",
|
||||||
"awilix": "^8.0.0",
|
"awilix": "^8.0.0",
|
||||||
"dotenv": "^16.1.4",
|
"dotenv": "16.4.5",
|
||||||
"knex": "2.4.2"
|
"knex": "2.4.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@
|
|||||||
"@mikro-orm/migrations": "5.9.7",
|
"@mikro-orm/migrations": "5.9.7",
|
||||||
"@mikro-orm/postgresql": "5.9.7",
|
"@mikro-orm/postgresql": "5.9.7",
|
||||||
"awilix": "^8.0.0",
|
"awilix": "^8.0.0",
|
||||||
"dotenv": "^16.1.4",
|
"dotenv": "16.4.5",
|
||||||
"knex": "2.4.2",
|
"knex": "2.4.2",
|
||||||
"lodash": "^4.17.21"
|
"lodash": "^4.17.21"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,12 +50,12 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@medusajs/modules-sdk": "^1.12.5",
|
"@medusajs/modules-sdk": "^1.12.5",
|
||||||
"@medusajs/types": "^1.11.9",
|
"@medusajs/types": "^1.11.9",
|
||||||
"@medusajs/utils": "^1.11.2",
|
"@medusajs/utils": "1.11.6",
|
||||||
"@mikro-orm/core": "5.9.7",
|
"@mikro-orm/core": "5.9.7",
|
||||||
"@mikro-orm/migrations": "5.9.7",
|
"@mikro-orm/migrations": "5.9.7",
|
||||||
"@mikro-orm/postgresql": "5.9.7",
|
"@mikro-orm/postgresql": "5.9.7",
|
||||||
"awilix": "^8.0.0",
|
"awilix": "^8.0.0",
|
||||||
"dotenv": "^16.1.4",
|
"dotenv": "16.3.1",
|
||||||
"knex": "2.4.2"
|
"knex": "2.4.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { RedisOptions } from "ioredis"
|
|
||||||
import { LoggerOptions } from "typeorm"
|
|
||||||
import {
|
import {
|
||||||
ExternalModuleDeclaration,
|
ExternalModuleDeclaration,
|
||||||
InternalModuleDeclaration,
|
InternalModuleDeclaration,
|
||||||
} from "../modules-sdk"
|
} from "../modules-sdk"
|
||||||
|
|
||||||
|
import { LoggerOptions } from "typeorm"
|
||||||
|
import { RedisOptions } from "ioredis"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @interface
|
* @interface
|
||||||
*
|
*
|
||||||
@@ -174,6 +175,7 @@ export type ProjectConfigOptions = {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
admin_cors?: string
|
admin_cors?: string
|
||||||
|
auth_cors?: string
|
||||||
/**
|
/**
|
||||||
* A random string used to create cookie tokens. Although this configuration option is not required, it’s highly recommended to set it for better security.
|
* A random string used to create cookie tokens. Although this configuration option is not required, it’s highly recommended to set it for better security.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -55,7 +55,7 @@
|
|||||||
"@mikro-orm/migrations": "5.9.7",
|
"@mikro-orm/migrations": "5.9.7",
|
||||||
"@mikro-orm/postgresql": "5.9.7",
|
"@mikro-orm/postgresql": "5.9.7",
|
||||||
"awilix": "^8.0.0",
|
"awilix": "^8.0.0",
|
||||||
"dotenv": "16.3.1",
|
"dotenv": "16.4.5",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"knex": "2.4.2"
|
"knex": "2.4.2"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7918,7 +7918,7 @@ __metadata:
|
|||||||
"@rollup/plugin-virtual": ^3.0.1
|
"@rollup/plugin-virtual": ^3.0.1
|
||||||
"@types/express": ^4.17.13
|
"@types/express": ^4.17.13
|
||||||
commander: ^10.0.0
|
commander: ^10.0.0
|
||||||
dotenv: 16.3.1
|
dotenv: 16.4.5
|
||||||
esbuild: 0.17.18
|
esbuild: 0.17.18
|
||||||
express: 4.18.2
|
express: 4.18.2
|
||||||
fs-extra: 11.1.0
|
fs-extra: 11.1.0
|
||||||
@@ -7975,7 +7975,7 @@ __metadata:
|
|||||||
"@mikro-orm/postgresql": 5.9.7
|
"@mikro-orm/postgresql": 5.9.7
|
||||||
awilix: ^8.0.0
|
awilix: ^8.0.0
|
||||||
cross-env: ^5.2.1
|
cross-env: ^5.2.1
|
||||||
dotenv: 16.3.1
|
dotenv: 16.4.5
|
||||||
jest: ^29.6.3
|
jest: ^29.6.3
|
||||||
jsonwebtoken: ^9.0.2
|
jsonwebtoken: ^9.0.2
|
||||||
knex: 2.4.2
|
knex: 2.4.2
|
||||||
@@ -8675,7 +8675,7 @@ __metadata:
|
|||||||
"@mikro-orm/postgresql": 5.9.7
|
"@mikro-orm/postgresql": 5.9.7
|
||||||
awilix: ^8.0.0
|
awilix: ^8.0.0
|
||||||
cross-env: ^5.2.1
|
cross-env: ^5.2.1
|
||||||
dotenv: ^16.1.4
|
dotenv: 16.4.5
|
||||||
jest: ^29.6.3
|
jest: ^29.6.3
|
||||||
knex: 2.4.2
|
knex: 2.4.2
|
||||||
medusa-test-utils: ^1.1.41
|
medusa-test-utils: ^1.1.41
|
||||||
@@ -8702,7 +8702,7 @@ __metadata:
|
|||||||
"@mikro-orm/postgresql": 5.9.7
|
"@mikro-orm/postgresql": 5.9.7
|
||||||
awilix: ^8.0.0
|
awilix: ^8.0.0
|
||||||
cross-env: ^5.2.1
|
cross-env: ^5.2.1
|
||||||
dotenv: ^16.1.4
|
dotenv: 16.4.5
|
||||||
faker: ^6.6.6
|
faker: ^6.6.6
|
||||||
jest: ^29.6.3
|
jest: ^29.6.3
|
||||||
knex: 2.4.2
|
knex: 2.4.2
|
||||||
@@ -8725,14 +8725,14 @@ __metadata:
|
|||||||
dependencies:
|
dependencies:
|
||||||
"@medusajs/modules-sdk": ^1.12.5
|
"@medusajs/modules-sdk": ^1.12.5
|
||||||
"@medusajs/types": ^1.11.9
|
"@medusajs/types": ^1.11.9
|
||||||
"@medusajs/utils": ^1.11.2
|
"@medusajs/utils": 1.11.6
|
||||||
"@mikro-orm/cli": 5.9.7
|
"@mikro-orm/cli": 5.9.7
|
||||||
"@mikro-orm/core": 5.9.7
|
"@mikro-orm/core": 5.9.7
|
||||||
"@mikro-orm/migrations": 5.9.7
|
"@mikro-orm/migrations": 5.9.7
|
||||||
"@mikro-orm/postgresql": 5.9.7
|
"@mikro-orm/postgresql": 5.9.7
|
||||||
awilix: ^8.0.0
|
awilix: ^8.0.0
|
||||||
cross-env: ^5.2.1
|
cross-env: ^5.2.1
|
||||||
dotenv: ^16.1.4
|
dotenv: 16.3.1
|
||||||
jest: ^29.6.3
|
jest: ^29.6.3
|
||||||
knex: 2.4.2
|
knex: 2.4.2
|
||||||
medusa-test-utils: ^1.1.40
|
medusa-test-utils: ^1.1.40
|
||||||
@@ -9014,7 +9014,7 @@ __metadata:
|
|||||||
"@mikro-orm/postgresql": 5.9.7
|
"@mikro-orm/postgresql": 5.9.7
|
||||||
awilix: ^8.0.0
|
awilix: ^8.0.0
|
||||||
cross-env: ^5.2.1
|
cross-env: ^5.2.1
|
||||||
dotenv: 16.3.1
|
dotenv: 16.4.5
|
||||||
jest: ^29.6.3
|
jest: ^29.6.3
|
||||||
jsonwebtoken: ^9.0.2
|
jsonwebtoken: ^9.0.2
|
||||||
knex: 2.4.2
|
knex: 2.4.2
|
||||||
@@ -9029,7 +9029,7 @@ __metadata:
|
|||||||
languageName: unknown
|
languageName: unknown
|
||||||
linkType: soft
|
linkType: soft
|
||||||
|
|
||||||
"@medusajs/utils@^1.1.41, @medusajs/utils@^1.10.5, @medusajs/utils@^1.11.1, @medusajs/utils@^1.11.2, @medusajs/utils@^1.11.3, @medusajs/utils@^1.11.4, @medusajs/utils@^1.11.5, @medusajs/utils@^1.11.6, @medusajs/utils@^1.9.4, @medusajs/utils@workspace:^, @medusajs/utils@workspace:packages/utils":
|
"@medusajs/utils@1.11.6, @medusajs/utils@^1.1.41, @medusajs/utils@^1.10.5, @medusajs/utils@^1.11.1, @medusajs/utils@^1.11.2, @medusajs/utils@^1.11.3, @medusajs/utils@^1.11.4, @medusajs/utils@^1.11.5, @medusajs/utils@^1.11.6, @medusajs/utils@^1.9.4, @medusajs/utils@workspace:^, @medusajs/utils@workspace:packages/utils":
|
||||||
version: 0.0.0-use.local
|
version: 0.0.0-use.local
|
||||||
resolution: "@medusajs/utils@workspace:packages/utils"
|
resolution: "@medusajs/utils@workspace:packages/utils"
|
||||||
dependencies:
|
dependencies:
|
||||||
@@ -25802,6 +25802,13 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"dotenv@npm:16.4.5":
|
||||||
|
version: 16.4.5
|
||||||
|
resolution: "dotenv@npm:16.4.5"
|
||||||
|
checksum: 48d92870076832af0418b13acd6e5a5a3e83bb00df690d9812e94b24aff62b88ade955ac99a05501305b8dc8f1b0ee7638b18493deb6fe93d680e5220936292f
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"dotenv@npm:^7.0.0":
|
"dotenv@npm:^7.0.0":
|
||||||
version: 7.0.0
|
version: 7.0.0
|
||||||
resolution: "dotenv@npm:7.0.0"
|
resolution: "dotenv@npm:7.0.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user