This commit is contained in:
Vilfred Sikker
2021-08-26 13:00:45 +02:00
parent f77fd38369
commit ae192d844b
5 changed files with 164 additions and 1 deletions
@@ -1,6 +1,9 @@
import { Router } from "express"
import bodyParser from "body-parser"
import { Validator, MedusaError } from "medusa-core-utils"
import jwt from "jsonwebtoken"
const JWT_SECRET = process.env.JWT_SECRET || ""
export default () => {
const app = Router()
@@ -74,5 +77,34 @@ export default () => {
}
})
app.post("/:id/wishlist/share-token", bodyParser.json(), async (req, res) => {
try {
const customerService = req.scope.resolve("customerService")
let customer = await customerService.retrieve(req.params.id)
// check customer exists else throw 404
if (!customer?.id) {
throw new MedusaError(Medusa.Types.NOT_FOUND, "not found", 404)
}
// check customer has wishlist else throw 400 bad request
if (!customer?.metadata?.wishlist) {
throw new MedusaError(Medusa.Types.INVALID_DATA, "invalid data", 400)
}
const token = jwt.sign(
{
customer_id: customer.id,
},
JWT_SECRET
)
res.json({ share_token: token })
} catch (err) {
throw err
}
})
return app
}
@@ -0,0 +1,36 @@
import { Router } from "express"
import jwt from "jsonwebtoken"
import cors from "cors"
import { getConfigFile } from "medusa-core-utils"
export default () => {
const app = Router()
const JWT_SECRET = process.env.JWT_SECRET || ""
// const { configModule } = getConfigFile(rootDirectory, "medusa-config")
// const { projectConfig } = configModule
// const corsOptions = {
// origin: projectConfig.store_cors.split(","),
// credentials: true,
// }
// console.log(corsOptions)
// app.options("/wishlists/:token", cors(corsOptions))
app.get("/wishlists/:token", async (req, res) => {
const { token } = req.params
// decorde token with decode = jwt.decode(token, secret)
const decode = jwt.decode(token, JWT_SECRET)
console.log(decode)
// fetch customer.retrieve(decode.customer_id)
// get customer.metadata.wishlist
// respond with
// wishlist
// first_name
res.json("200")
})
return app
}