diff --git a/packages/admin-next/dashboard/src/hooks/api/customers.tsx b/packages/admin-next/dashboard/src/hooks/api/customers.tsx index 66764887d0..adfbb1e372 100644 --- a/packages/admin-next/dashboard/src/hooks/api/customers.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/customers.tsx @@ -58,7 +58,7 @@ export const useCustomers = ( export const useCreateCustomer = ( options?: UseMutationOptions< - { customer: HttpTypes.AdminCustomer; token: string }, + { customer: HttpTypes.AdminCustomer }, Error, HttpTypes.AdminCreateCustomer > diff --git a/packages/core/js-sdk/src/admin/index.ts b/packages/core/js-sdk/src/admin/index.ts index 7f64ee1a96..d4fc596479 100644 --- a/packages/core/js-sdk/src/admin/index.ts +++ b/packages/core/js-sdk/src/admin/index.ts @@ -179,7 +179,6 @@ export class Admin { ) => { return this.client.fetch<{ customer: HttpTypes.AdminCustomer - token: string }>(`/admin/customers`, { method: "POST", headers, diff --git a/packages/core/js-sdk/src/store/index.ts b/packages/core/js-sdk/src/store/index.ts index c574887baf..40de5778c9 100644 --- a/packages/core/js-sdk/src/store/index.ts +++ b/packages/core/js-sdk/src/store/index.ts @@ -349,7 +349,6 @@ export class Store { ) => { return this.client.fetch<{ customer: HttpTypes.StoreCustomer - token: string }>(`/store/customers`, { method: "POST", headers, diff --git a/packages/core/utils/package.json b/packages/core/utils/package.json index c06e22deb0..a5ca5afa1d 100644 --- a/packages/core/utils/package.json +++ b/packages/core/utils/package.json @@ -37,6 +37,7 @@ "awilix": "^8.0.1", "bignumber.js": "^9.1.2", "dotenv": "^16.4.5", + "jsonwebtoken": "^9.0.2", "knex": "2.4.2", "ulid": "^2.3.0" }, diff --git a/packages/core/utils/src/auth/index.ts b/packages/core/utils/src/auth/index.ts index c176b062eb..3e777a29ba 100644 --- a/packages/core/utils/src/auth/index.ts +++ b/packages/core/utils/src/auth/index.ts @@ -1 +1,2 @@ export * from "./abstract-auth-provider" +export * from "./token" diff --git a/packages/medusa/src/api/utils/auth/token.ts b/packages/core/utils/src/auth/token.ts similarity index 57% rename from packages/medusa/src/api/utils/auth/token.ts rename to packages/core/utils/src/auth/token.ts index e1def705ec..88683b1e94 100644 --- a/packages/medusa/src/api/utils/auth/token.ts +++ b/packages/core/utils/src/auth/token.ts @@ -1,14 +1,13 @@ -import { AuthContext } from "../../../types/routing" import jwt from "jsonwebtoken" export const generateJwtToken = ( - authContext: AuthContext, + tokenPayload: Record, jwtConfig: { secret: string expiresIn: string } ) => { - return jwt.sign(authContext, jwtConfig.secret, { + return jwt.sign(tokenPayload, jwtConfig.secret, { expiresIn: jwtConfig.expiresIn, }) } diff --git a/packages/medusa/package.json b/packages/medusa/package.json index faf54a5279..da1601ba6f 100644 --- a/packages/medusa/package.json +++ b/packages/medusa/package.json @@ -69,7 +69,6 @@ "glob": "^7.1.6", "ioredis": "^5.2.5", "ioredis-mock": "8.4.0", - "jsonwebtoken": "^9.0.0", "lodash": "^4.17.21", "medusa-telemetry": "^0.0.17", "morgan": "^1.9.1", diff --git a/packages/medusa/src/api/admin/users/route.ts b/packages/medusa/src/api/admin/users/route.ts index 28c7b562ed..4e7c92bbda 100644 --- a/packages/medusa/src/api/admin/users/route.ts +++ b/packages/medusa/src/api/admin/users/route.ts @@ -10,7 +10,6 @@ import { MedusaResponse, } from "../../../types/routing" import { refetchUser } from "./helpers" -import { generateJwtToken } from "../../utils/auth/token" export const GET = async ( req: AuthenticatedMedusaRequest, @@ -57,32 +56,13 @@ export const POST = async ( const { result } = await createUserAccountWorkflow(req.scope).run(input) - const { http } = req.scope.resolve( - ContainerRegistrationKeys.CONFIG_MODULE - ).projectConfig - const { jwtSecret, jwtExpiresIn } = http - const token = generateJwtToken( - { - actor_id: result.id, - actor_type: "user", - auth_identity_id: req.auth_context.auth_identity_id, - app_metadata: { - user_id: result.id, - }, - }, - { - secret: jwtSecret, - expiresIn: jwtExpiresIn, - } - ) - const user = await refetchUser( result.id, req.scope, req.remoteQueryConfig.fields ) - res.status(200).json({ user, token }) + res.status(200).json({ user }) } export const AUTHENTICATE = false diff --git a/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/callback/route.ts b/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/callback/route.ts index f6bb2e9372..b1dce24baa 100644 --- a/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/callback/route.ts +++ b/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/callback/route.ts @@ -4,9 +4,12 @@ import { IAuthModuleService, ConfigModule, } from "@medusajs/types" -import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils" +import { + ContainerRegistrationKeys, + MedusaError, + generateJwtToken, +} from "@medusajs/utils" import { MedusaRequest, MedusaResponse } from "../../../../../types/routing" -import { generateJwtToken } from "../../../../utils/auth/token" export const GET = async (req: MedusaRequest, res: MedusaResponse) => { const { actor_type, auth_provider } = req.params diff --git a/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/route.ts b/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/route.ts index 9bb8b6b681..b651a522ba 100644 --- a/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/route.ts +++ b/packages/medusa/src/api/auth/[actor_type]/[auth_provider]/route.ts @@ -4,9 +4,12 @@ import { IAuthModuleService, ConfigModule, } from "@medusajs/types" -import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils" +import { + ContainerRegistrationKeys, + MedusaError, + generateJwtToken, +} from "@medusajs/utils" import { MedusaRequest, MedusaResponse } from "../../../../types/routing" -import { generateJwtToken } from "../../../utils/auth/token" export const GET = async (req: MedusaRequest, res: MedusaResponse) => { const { actor_type, auth_provider } = req.params diff --git a/packages/medusa/src/api/store/customers/route.ts b/packages/medusa/src/api/store/customers/route.ts index 1f466accf5..f0dcf57ef5 100644 --- a/packages/medusa/src/api/store/customers/route.ts +++ b/packages/medusa/src/api/store/customers/route.ts @@ -2,12 +2,11 @@ import { AuthenticatedMedusaRequest, MedusaResponse, } from "../../../types/routing" -import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils" +import { MedusaError } from "@medusajs/utils" import { createCustomerAccountWorkflow } from "@medusajs/core-flows" import { refetchCustomer } from "./helpers" import { StoreCreateCustomerType } from "./validators" -import { generateJwtToken } from "../../utils/auth/token" export const POST = async ( req: AuthenticatedMedusaRequest, @@ -28,30 +27,11 @@ export const POST = async ( input: { customersData, authIdentityId: req.auth_context.auth_identity_id }, }) - const { http } = req.scope.resolve( - ContainerRegistrationKeys.CONFIG_MODULE - ).projectConfig - const { jwtSecret, jwtExpiresIn } = http - const token = generateJwtToken( - { - actor_id: result.id, - actor_type: "customer", - auth_identity_id: req.auth_context.auth_identity_id, - app_metadata: { - customer_id: result.id, - }, - }, - { - secret: jwtSecret, - expiresIn: jwtExpiresIn, - } - ) - const customer = await refetchCustomer( result.id, req.scope, req.remoteQueryConfig.fields ) - res.status(200).json({ customer, token }) + res.status(200).json({ customer }) } diff --git a/packages/medusa/src/loaders/helpers/routing/__fixtures__/server/index.ts b/packages/medusa/src/loaders/helpers/routing/__fixtures__/server/index.ts index eeadd7aee2..ff7cdcfcbe 100644 --- a/packages/medusa/src/loaders/helpers/routing/__fixtures__/server/index.ts +++ b/packages/medusa/src/loaders/helpers/routing/__fixtures__/server/index.ts @@ -6,10 +6,10 @@ import { import { ContainerRegistrationKeys, createMedusaContainer, + generateJwtToken, } from "@medusajs/utils" import { asValue } from "awilix" import express from "express" -import jwt from "jsonwebtoken" import querystring from "querystring" import supertest from "supertest" import apiLoader from "../../../../api" @@ -121,7 +121,7 @@ export const createServer = async (rootDir) => { ) headers.Cookie = headers.Cookie || "" if (opts.adminSession) { - const token = jwt.sign( + const token = generateJwtToken( { actor_id: opts.adminSession.userId || opts.adminSession.jwt?.userId, actor_type: "user", @@ -130,14 +130,17 @@ export const createServer = async (rootDir) => { opts.adminSession.userId || opts.adminSession.jwt?.userId, }, }, - config.projectConfig.http.jwtSecret! + { + secret: config.projectConfig.http.jwtSecret!, + expiresIn: "1d", + } ) headers.Authorization = `Bearer ${token}` } if (opts.clientSession) { - const token = jwt.sign( + const token = generateJwtToken( { actor_id: opts.clientSession.customer_id || @@ -149,7 +152,7 @@ export const createServer = async (rootDir) => { opts.clientSession.jwt?.customer_id, }, }, - config.projectConfig.http.jwtSecret! + { secret: config.projectConfig.http.jwtSecret!, expiresIn: "1d" } ) headers.Authorization = `Bearer ${token}` diff --git a/packages/modules/providers/auth-google/integration-tests/__tests__/services.spec.ts b/packages/modules/providers/auth-google/integration-tests/__tests__/services.spec.ts index c33b0f7e4c..da68a18677 100644 --- a/packages/modules/providers/auth-google/integration-tests/__tests__/services.spec.ts +++ b/packages/modules/providers/auth-google/integration-tests/__tests__/services.spec.ts @@ -1,9 +1,8 @@ -import { MedusaError } from "@medusajs/utils" +import { generateJwtToken, MedusaError } from "@medusajs/utils" import { GoogleAuthService } from "../../src/services/google" jest.setTimeout(100000) import { http, HttpResponse } from "msw" import { setupServer } from "msw/node" -import jwt from "jsonwebtoken" const sampleIdPayload = { iss: "https://accounts.google.com", @@ -20,10 +19,12 @@ const sampleIdPayload = { given_name: "Test", family_name: "Admin", iat: 1716891837, - exp: 1716895437, } -const encodedIdToken = jwt.sign(sampleIdPayload, "test") +const encodedIdToken = generateJwtToken(sampleIdPayload, { + secret: "test", + expiresIn: "1d", +}) const baseUrl = "https://someurl.com" diff --git a/yarn.lock b/yarn.lock index ea55323708..b611a89165 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5572,7 +5572,6 @@ __metadata: ioredis: ^5.2.5 ioredis-mock: 8.4.0 jest: ^25.5.4 - jsonwebtoken: ^9.0.0 lodash: ^4.17.21 medusa-telemetry: ^0.0.17 morgan: ^1.9.1 @@ -6197,6 +6196,7 @@ __metadata: dotenv: ^16.4.5 express: ^4.18.2 jest: ^29.6.3 + jsonwebtoken: ^9.0.2 knex: 2.4.2 rimraf: ^5.0.1 ts-jest: ^29.1.1