Adds Oauth support to plugins

This commit is contained in:
Sebastian Rindom
2020-08-04 17:13:47 +02:00
parent e69c3aba01
commit 21bc096b2e
21 changed files with 688 additions and 685 deletions
@@ -0,0 +1,25 @@
import { MedusaError, Validator } from "medusa-core-utils"
export default async (req, res) => {
const schema = Validator.object().keys({
application_name: Validator.string().required(),
state: Validator.string().required(),
code: Validator.string().required(),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const oauthService = req.scope.resolve("oauthService")
const data = await oauthService.generateToken(
value.application_name,
value.code,
value.state
)
res.status(200).json({ apps: data })
} catch (err) {
throw err
}
}
@@ -0,0 +1,16 @@
import { Router } from "express"
import middlewares from "../../../middlewares"
const route = Router()
export default app => {
app.use("/apps", route)
route.get("/", middlewares.wrap(require("./list").default))
route.post(
"/authorizations",
middlewares.wrap(require("./authorize-app").default)
)
return app
}
@@ -0,0 +1,12 @@
import { MedusaError, Validator } from "medusa-core-utils"
export default async (req, res) => {
try {
const oauthService = req.scope.resolve("oauthService")
const data = await oauthService.list({})
res.status(200).json({ apps: data })
} catch (err) {
throw err
}
}
@@ -13,6 +13,7 @@ import orderRoutes from "./orders"
import storeRoutes from "./store"
import uploadRoutes from "./uploads"
import customerRoutes from "./customers"
import appRoutes from "./apps"
const route = Router()
@@ -40,6 +41,7 @@ export default (app, container, config) => {
// Calls all middleware that has been registered to run after authentication.
middlewareService.usePostAuthentication(app)
appRoutes(route)
productRoutes(route)
userRoutes(route)
regionRoutes(route)