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:
Riqwan Thamir
2024-03-04 09:07:47 +00:00
committed by GitHub
co-authored by Philip Korsholm
parent 883cb0dca7
commit 8dad2b51a2
20 changed files with 305 additions and 36 deletions
+1 -1
View File
@@ -35,7 +35,7 @@
"@rollup/plugin-replace": "5.0.2",
"@rollup/plugin-virtual": "^3.0.1",
"commander": "^10.0.0",
"dotenv": "16.3.1",
"dotenv": "16.4.5",
"esbuild": "0.17.18",
"express": "4.18.2",
"fs-extra": "11.1.0",
+1 -1
View File
@@ -55,7 +55,7 @@
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "16.3.1",
"dotenv": "16.4.5",
"jsonwebtoken": "^9.0.2",
"knex": "2.4.2",
"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 { transformBody, transformQuery } from "../../../api/middlewares"
import {
AdminCreateUserRequest,
AdminGetUsersParams,
AdminGetUsersUserParams,
AdminUpdateUserRequest,
} from "./validators"
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../types/middlewares"
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"],
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 { MedusaError } from "@medusajs/utils"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import jwt from "jsonwebtoken"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
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 { success, error, authUser, location } = authResult
if (location) {
res.redirect(location)
return
@@ -30,7 +31,6 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
if (success) {
const { jwt_secret } = req.scope.resolve("configModule").projectConfig
const token = jwt.sign(authUser, jwt_secret)
return res.status(200).json({ token })
+2 -2
View File
@@ -1,12 +1,12 @@
import path from "path"
import { FeatureFlagUtils, FlagRouter } from "@medusajs/utils"
import { AwilixContainer } from "awilix"
import bodyParser from "body-parser"
import { Express } from "express"
import path from "path"
import qs from "qs"
import { RoutesLoader } from "./helpers/routing"
import routes from "../api"
import { ConfigModule } from "../types/global"
import { RoutesLoader } from "./helpers/routing"
type Options = {
app: Express
@@ -307,6 +307,7 @@ export class RoutesLoader {
shouldRequireAdminAuth: false,
shouldRequireCustomerAuth: 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")) {
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) {
/**
* Apply the store cors
@@ -41,6 +41,7 @@ export type RouteConfig = {
shouldAppendCustomer?: boolean
shouldAppendAdminCors?: boolean
shouldAppendStoreCors?: boolean
shouldAppendAuthCors?: boolean
routes?: RouteImplementation[]
}
+3 -5
View File
@@ -3,13 +3,9 @@ import {
ModulesDefinition,
} from "@medusajs/modules-sdk"
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 { asValue } from "awilix"
import { Express, NextFunction, Request, Response } from "express"
import { createMedusaContainer } from "medusa-core-utils"
import { track } from "medusa-telemetry"
import { EOL } from "os"
@@ -19,6 +15,7 @@ import { v4 } from "uuid"
import { MedusaContainer } from "../types/global"
import apiLoader from "./api"
import loadConfig from "./config"
import databaseLoader, { dataSource } from "./database"
import defaultsLoader from "./defaults"
import expressLoader from "./express"
import featureFlagsLoader from "./feature-flags"
@@ -27,6 +24,7 @@ import loadMedusaApp, { mergeDefaultModules } from "./medusa-app"
import modelsLoader from "./models"
import passportLoader from "./passport"
import pgConnectionLoader from "./pg-connection"
import pluginsLoader, { registerPluginModels } from "./plugins"
import redisLoader from "./redis"
import repositoriesLoader from "./repositories"
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 {
AuthenticatedMedusaRequest,
MedusaRequest,
MedusaResponse,
} 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 BEARER_AUTH = "bearer"
@@ -68,6 +65,7 @@ export const authenticate = (
}
const isMedusaScope = isAdminScope(authScope) || isStoreScope(authScope)
const isRegistered =
!isMedusaScope ||
(authUser?.app_metadata?.user_id &&
@@ -85,6 +83,7 @@ export const authenticate = (
app_metadata: authUser.app_metadata,
scope: authUser.scope,
}
return next()
}
+1 -1
View File
@@ -55,7 +55,7 @@
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "^16.1.4",
"dotenv": "16.4.5",
"knex": "2.4.2"
}
}
+1 -1
View File
@@ -57,7 +57,7 @@
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "^16.1.4",
"dotenv": "16.4.5",
"knex": "2.4.2",
"lodash": "^4.17.21"
}
+2 -2
View File
@@ -50,12 +50,12 @@
"dependencies": {
"@medusajs/modules-sdk": "^1.12.5",
"@medusajs/types": "^1.11.9",
"@medusajs/utils": "^1.11.2",
"@medusajs/utils": "1.11.6",
"@mikro-orm/core": "5.9.7",
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "^16.1.4",
"dotenv": "16.3.1",
"knex": "2.4.2"
}
}
+4 -2
View File
@@ -1,10 +1,11 @@
import { RedisOptions } from "ioredis"
import { LoggerOptions } from "typeorm"
import {
ExternalModuleDeclaration,
InternalModuleDeclaration,
} from "../modules-sdk"
import { LoggerOptions } from "typeorm"
import { RedisOptions } from "ioredis"
/**
* @interface
*
@@ -174,6 +175,7 @@ export type ProjectConfigOptions = {
* ```
*/
admin_cors?: string
auth_cors?: string
/**
* A random string used to create cookie tokens. Although this configuration option is not required, its highly recommended to set it for better security.
*
+1 -1
View File
@@ -55,7 +55,7 @@
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "16.3.1",
"dotenv": "16.4.5",
"jsonwebtoken": "^9.0.2",
"knex": "2.4.2"
}