feat(medusa): Improve config loading (#1290)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import _ from "lodash"
|
||||
import jwt from "jsonwebtoken"
|
||||
import config from "../../../../config"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { IsEmail, IsNotEmpty, IsString } from "class-validator"
|
||||
import AuthService from "../../../../services/auth"
|
||||
@@ -28,10 +27,11 @@ import { MedusaError } from "medusa-core-utils"
|
||||
* $ref: "#/components/schemas/user"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
if (!config.jwtSecret) {
|
||||
const { projectConfig: { jwt_secret } } = req.scope.resolve('configModule')
|
||||
if (!jwt_secret) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
"Please configure jwtSecret in your environment"
|
||||
"Please configure jwt_secret in your environment"
|
||||
)
|
||||
}
|
||||
const validated = await validator(AdminPostAuthReq, req.body)
|
||||
@@ -44,7 +44,7 @@ export default async (req, res) => {
|
||||
|
||||
if (result.success && result.user) {
|
||||
// Add JWT to cookie
|
||||
req.session.jwt = jwt.sign({ userId: result.user.id }, config.jwtSecret, {
|
||||
req.session.jwt = jwt.sign({ userId: result.user.id }, jwt_secret, {
|
||||
expiresIn: "24h",
|
||||
})
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { IsEmail, IsNotEmpty } from "class-validator"
|
||||
import jwt from "jsonwebtoken"
|
||||
import config from "../../../../config"
|
||||
import AuthService from "../../../../services/auth"
|
||||
import CustomerService from "../../../../services/customer"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
@@ -29,8 +28,6 @@ export default async (req, res) => {
|
||||
const validated = await validator(StorePostAuthReq, req.body)
|
||||
|
||||
const authService: AuthService = req.scope.resolve("authService")
|
||||
const customerService: CustomerService = req.scope.resolve("customerService")
|
||||
|
||||
const result = await authService.authenticateCustomer(
|
||||
validated.email,
|
||||
validated.password
|
||||
@@ -41,14 +38,14 @@ export default async (req, res) => {
|
||||
}
|
||||
|
||||
// Add JWT to cookie
|
||||
req.session.jwt = jwt.sign(
|
||||
{ customer_id: result.customer?.id },
|
||||
config.jwtSecret as string,
|
||||
{
|
||||
expiresIn: "30d",
|
||||
}
|
||||
)
|
||||
const {
|
||||
projectConfig: { jwt_secret },
|
||||
} = req.scope.resolve("configModule")
|
||||
req.session.jwt = jwt.sign({ customer_id: result.customer?.id }, jwt_secret!, {
|
||||
expiresIn: "30d",
|
||||
})
|
||||
|
||||
const customerService: CustomerService = req.scope.resolve("customerService")
|
||||
const customer = await customerService.retrieve(result.customer?.id || "", {
|
||||
relations: ["orders", "orders.items"],
|
||||
})
|
||||
|
||||
@@ -2,7 +2,6 @@ import { IsEmail, IsOptional, IsString } from "class-validator"
|
||||
import jwt from "jsonwebtoken"
|
||||
import { defaultStoreCustomersFields, defaultStoreCustomersRelations } from "."
|
||||
import { Customer } from "../../../.."
|
||||
import config from "../../../../config"
|
||||
import CustomerService from "../../../../services/customer"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
|
||||
@@ -36,7 +35,10 @@ export default async (req, res) => {
|
||||
let customer: Customer = await customerService.create(validated)
|
||||
|
||||
// Add JWT to cookie
|
||||
req.session.jwt = jwt.sign({ customer_id: customer.id }, config.jwtSecret!, {
|
||||
const {
|
||||
projectConfig: { jwt_secret },
|
||||
} = req.scope.resolve("configModule")
|
||||
req.session.jwt = jwt.sign({ customer_id: customer.id }, jwt_secret!, {
|
||||
expiresIn: "30d",
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user