Adds account support
This commit is contained in:
@@ -15,6 +15,8 @@ export default async (req, res) => {
|
||||
}
|
||||
|
||||
const authService = req.scope.resolve("authService")
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
|
||||
const result = await authService.authenticateCustomer(
|
||||
value.email,
|
||||
value.password
|
||||
@@ -26,12 +28,21 @@ export default async (req, res) => {
|
||||
|
||||
// Add JWT to cookie
|
||||
req.session.jwt = jwt.sign(
|
||||
{ customer_id: result.user._id },
|
||||
{ customer_id: result.customer._id },
|
||||
config.jwtSecret,
|
||||
{
|
||||
expiresIn: "30d",
|
||||
}
|
||||
)
|
||||
|
||||
res.json({ customer: result.customer })
|
||||
const data = await customerService.decorate(result.customer, [
|
||||
"_id",
|
||||
"email",
|
||||
"orders",
|
||||
"shipping_addresses",
|
||||
"first_name",
|
||||
"last_name",
|
||||
])
|
||||
|
||||
res.json({ customer: data })
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export default async (req, res) => {
|
||||
req.session.jwt = {}
|
||||
res.json({})
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const { email } = req.params
|
||||
|
||||
try {
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
const customer = await customerService.retrieveByEmail(email)
|
||||
res.status(200).json({ exists: !!customer.password_hash })
|
||||
} catch (err) {
|
||||
res.status(200).json({ exists: false })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
export default async (req, res) => {
|
||||
try {
|
||||
if (req.user && req.user.customer_id) {
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
const customer = await customerService.retrieve(req.user.customer_id)
|
||||
|
||||
const data = await customerService.decorate(
|
||||
customer,
|
||||
[
|
||||
"_id",
|
||||
"email",
|
||||
"orders",
|
||||
"shipping_addresses",
|
||||
"first_name",
|
||||
"last_name",
|
||||
],
|
||||
["orders"]
|
||||
)
|
||||
res.json({ customer: data })
|
||||
}
|
||||
|
||||
res.sendStatus(401)
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,15 @@ const route = Router()
|
||||
|
||||
export default app => {
|
||||
app.use("/auth", route)
|
||||
|
||||
route.get(
|
||||
"/",
|
||||
middlewares.authenticate(),
|
||||
middlewares.wrap(require("./get-session").default)
|
||||
)
|
||||
route.get("/:email", middlewares.wrap(require("./exists").default))
|
||||
route.delete("/", middlewares.wrap(require("./delete-session").default))
|
||||
route.post("/", middlewares.wrap(require("./create-session").default))
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import jwt from "jsonwebtoken"
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
import config from "../../../../config"
|
||||
|
||||
export default async (req, res) => {
|
||||
const schema = Validator.object().keys({
|
||||
@@ -17,7 +19,24 @@ export default async (req, res) => {
|
||||
try {
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
const customer = await customerService.create(value)
|
||||
const data = await customerService.decorate(customer, ["_id", "email"])
|
||||
|
||||
// Add JWT to cookie
|
||||
req.session.jwt = jwt.sign(
|
||||
{ customer_id: customer._id },
|
||||
config.jwtSecret,
|
||||
{
|
||||
expiresIn: "30d",
|
||||
}
|
||||
)
|
||||
|
||||
const data = await customerService.decorate(customer, [
|
||||
"_id",
|
||||
"email",
|
||||
"orders",
|
||||
"shipping_addresses",
|
||||
"first_name",
|
||||
"last_name",
|
||||
])
|
||||
res.status(201).json({ customer: data })
|
||||
} catch (err) {
|
||||
throw err
|
||||
|
||||
@@ -6,9 +6,6 @@ const route = Router()
|
||||
export default app => {
|
||||
app.use("/customers", route)
|
||||
|
||||
route.get("/", middlewares.wrap(require("./list-customers").default))
|
||||
route.get("/:id", middlewares.wrap(require("./get-customer").default))
|
||||
|
||||
route.post("/", middlewares.wrap(require("./create-customer").default))
|
||||
|
||||
route.post(
|
||||
@@ -26,6 +23,7 @@ export default app => {
|
||||
|
||||
route.param("id", middlewares.wrap(require("./authorize-customer").default))
|
||||
|
||||
route.get("/:id", middlewares.wrap(require("./get-customer").default))
|
||||
route.post("/:id", middlewares.wrap(require("./update-customer").default))
|
||||
route.post(
|
||||
"/:id/password",
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
export default async (req, res) => {
|
||||
const selector = {}
|
||||
|
||||
try {
|
||||
const customerService = req.scope.resolve("customerService")
|
||||
const customers = await customerService.list(selector)
|
||||
|
||||
res.json({ customers })
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Router } from "express"
|
||||
import cors from "cors"
|
||||
|
||||
import authRoutes from "./auth"
|
||||
import productRoutes from "./products"
|
||||
import cartRoutes from "./carts"
|
||||
import orderRoutes from "./orders"
|
||||
@@ -21,6 +22,7 @@ export default (app, container, config) => {
|
||||
})
|
||||
)
|
||||
|
||||
authRoutes(route)
|
||||
customerRoutes(route)
|
||||
productRoutes(route)
|
||||
orderRoutes(route)
|
||||
|
||||
@@ -36,6 +36,26 @@ export default async (req, res) => {
|
||||
|
||||
res.status(200).json({ order })
|
||||
} catch (err) {
|
||||
throw err
|
||||
// If something fails it might be because the order has already been created
|
||||
// if it has we find it from the cart id
|
||||
const orderService = req.scope.resolve("orderService")
|
||||
let order = await orderService.retrieveByCartId(value.cartId)
|
||||
order = await orderService.decorate(order, [
|
||||
"status",
|
||||
"fulfillment_status",
|
||||
"payment_status",
|
||||
"email",
|
||||
"billing_address",
|
||||
"shipping_address",
|
||||
"items",
|
||||
"region",
|
||||
"discounts",
|
||||
"customer_id",
|
||||
"payment_method",
|
||||
"shipping_methods",
|
||||
"metadata",
|
||||
])
|
||||
|
||||
res.status(200).json({ order })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user