feat: Remove returning token from customer and user endpoints (#7523)

* chore: Move generateJwtToken to utils

* feat: Stop returning token on user and customer endpoints
This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-05-29 17:13:01 +02:00
1 parent e5e5eb6e18
commit aeda24265d
14 files changed
+32 -64

No files matched your search

@@ -58,7 +58,7 @@ export const useCustomers = (
export const useCreateCustomer = ( export const useCreateCustomer = (
options?: UseMutationOptions< options?: UseMutationOptions<
{ customer: HttpTypes.AdminCustomer; token: string }, { customer: HttpTypes.AdminCustomer },
Error, Error,
HttpTypes.AdminCreateCustomer HttpTypes.AdminCreateCustomer
> >
-1
View File
@@ -179,7 +179,6 @@ export class Admin {
) => { ) => {
return this.client.fetch<{ return this.client.fetch<{
customer: HttpTypes.AdminCustomer customer: HttpTypes.AdminCustomer
token: string
}>(`/admin/customers`, { }>(`/admin/customers`, {
method: "POST", method: "POST",
headers, headers,
-1
View File
@@ -349,7 +349,6 @@ export class Store {
) => { ) => {
return this.client.fetch<{ return this.client.fetch<{
customer: HttpTypes.StoreCustomer customer: HttpTypes.StoreCustomer
token: string
}>(`/store/customers`, { }>(`/store/customers`, {
method: "POST", method: "POST",
headers, headers,
+1
View File
@@ -37,6 +37,7 @@
"awilix": "^8.0.1", "awilix": "^8.0.1",
"bignumber.js": "^9.1.2", "bignumber.js": "^9.1.2",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"jsonwebtoken": "^9.0.2",
"knex": "2.4.2", "knex": "2.4.2",
"ulid": "^2.3.0" "ulid": "^2.3.0"
}, },
+1
View File
@@ -1 +1,2 @@
export * from "./abstract-auth-provider" export * from "./abstract-auth-provider"
export * from "./token"
@@ -1,14 +1,13 @@
import { AuthContext } from "../../../types/routing"
import jwt from "jsonwebtoken" import jwt from "jsonwebtoken"
export const generateJwtToken = ( export const generateJwtToken = (
authContext: AuthContext, tokenPayload: Record<string, unknown>,
jwtConfig: { jwtConfig: {
secret: string secret: string
expiresIn: string expiresIn: string
} }
) => { ) => {
return jwt.sign(authContext, jwtConfig.secret, { return jwt.sign(tokenPayload, jwtConfig.secret, {
expiresIn: jwtConfig.expiresIn, expiresIn: jwtConfig.expiresIn,
}) })
} }
-1
View File
@@ -69,7 +69,6 @@
"glob": "^7.1.6", "glob": "^7.1.6",
"ioredis": "^5.2.5", "ioredis": "^5.2.5",
"ioredis-mock": "8.4.0", "ioredis-mock": "8.4.0",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"medusa-telemetry": "^0.0.17", "medusa-telemetry": "^0.0.17",
"morgan": "^1.9.1", "morgan": "^1.9.1",
+1 -21
View File
@@ -10,7 +10,6 @@ import {
MedusaResponse, MedusaResponse,
} from "../../../types/routing" } from "../../../types/routing"
import { refetchUser } from "./helpers" import { refetchUser } from "./helpers"
import { generateJwtToken } from "../../utils/auth/token"
export const GET = async ( export const GET = async (
req: AuthenticatedMedusaRequest, req: AuthenticatedMedusaRequest,
@@ -57,32 +56,13 @@ export const POST = async (
const { result } = await createUserAccountWorkflow(req.scope).run(input) 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( const user = await refetchUser(
result.id, result.id,
req.scope, req.scope,
req.remoteQueryConfig.fields req.remoteQueryConfig.fields
) )
res.status(200).json({ user, token }) res.status(200).json({ user })
} }
export const AUTHENTICATE = false export const AUTHENTICATE = false
@@ -4,9 +4,12 @@ import {
IAuthModuleService, IAuthModuleService,
ConfigModule, ConfigModule,
} from "@medusajs/types" } from "@medusajs/types"
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils" import {
ContainerRegistrationKeys,
MedusaError,
generateJwtToken,
} from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing" import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { generateJwtToken } from "../../../../utils/auth/token"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const { actor_type, auth_provider } = req.params const { actor_type, auth_provider } = req.params
@@ -4,9 +4,12 @@ import {
IAuthModuleService, IAuthModuleService,
ConfigModule, ConfigModule,
} from "@medusajs/types" } from "@medusajs/types"
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils" import {
ContainerRegistrationKeys,
MedusaError,
generateJwtToken,
} from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing" import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { generateJwtToken } from "../../../utils/auth/token"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const { actor_type, auth_provider } = req.params const { actor_type, auth_provider } = req.params
@@ -2,12 +2,11 @@ import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaResponse, MedusaResponse,
} from "../../../types/routing" } from "../../../types/routing"
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils" import { MedusaError } from "@medusajs/utils"
import { createCustomerAccountWorkflow } from "@medusajs/core-flows" import { createCustomerAccountWorkflow } from "@medusajs/core-flows"
import { refetchCustomer } from "./helpers" import { refetchCustomer } from "./helpers"
import { StoreCreateCustomerType } from "./validators" import { StoreCreateCustomerType } from "./validators"
import { generateJwtToken } from "../../utils/auth/token"
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<StoreCreateCustomerType>, req: AuthenticatedMedusaRequest<StoreCreateCustomerType>,
@@ -28,30 +27,11 @@ export const POST = async (
input: { customersData, authIdentityId: req.auth_context.auth_identity_id }, 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( const customer = await refetchCustomer(
result.id, result.id,
req.scope, req.scope,
req.remoteQueryConfig.fields req.remoteQueryConfig.fields
) )
res.status(200).json({ customer, token }) res.status(200).json({ customer })
} }
@@ -6,10 +6,10 @@ import {
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
createMedusaContainer, createMedusaContainer,
generateJwtToken,
} from "@medusajs/utils" } from "@medusajs/utils"
import { asValue } from "awilix" import { asValue } from "awilix"
import express from "express" import express from "express"
import jwt from "jsonwebtoken"
import querystring from "querystring" import querystring from "querystring"
import supertest from "supertest" import supertest from "supertest"
import apiLoader from "../../../../api" import apiLoader from "../../../../api"
@@ -121,7 +121,7 @@ export const createServer = async (rootDir) => {
) )
headers.Cookie = headers.Cookie || "" headers.Cookie = headers.Cookie || ""
if (opts.adminSession) { if (opts.adminSession) {
const token = jwt.sign( const token = generateJwtToken(
{ {
actor_id: opts.adminSession.userId || opts.adminSession.jwt?.userId, actor_id: opts.adminSession.userId || opts.adminSession.jwt?.userId,
actor_type: "user", actor_type: "user",
@@ -130,14 +130,17 @@ export const createServer = async (rootDir) => {
opts.adminSession.userId || opts.adminSession.jwt?.userId, opts.adminSession.userId || opts.adminSession.jwt?.userId,
}, },
}, },
config.projectConfig.http.jwtSecret! {
secret: config.projectConfig.http.jwtSecret!,
expiresIn: "1d",
}
) )
headers.Authorization = `Bearer ${token}` headers.Authorization = `Bearer ${token}`
} }
if (opts.clientSession) { if (opts.clientSession) {
const token = jwt.sign( const token = generateJwtToken(
{ {
actor_id: actor_id:
opts.clientSession.customer_id || opts.clientSession.customer_id ||
@@ -149,7 +152,7 @@ export const createServer = async (rootDir) => {
opts.clientSession.jwt?.customer_id, opts.clientSession.jwt?.customer_id,
}, },
}, },
config.projectConfig.http.jwtSecret! { secret: config.projectConfig.http.jwtSecret!, expiresIn: "1d" }
) )
headers.Authorization = `Bearer ${token}` headers.Authorization = `Bearer ${token}`
@@ -1,9 +1,8 @@
import { MedusaError } from "@medusajs/utils" import { generateJwtToken, MedusaError } from "@medusajs/utils"
import { GoogleAuthService } from "../../src/services/google" import { GoogleAuthService } from "../../src/services/google"
jest.setTimeout(100000) jest.setTimeout(100000)
import { http, HttpResponse } from "msw" import { http, HttpResponse } from "msw"
import { setupServer } from "msw/node" import { setupServer } from "msw/node"
import jwt from "jsonwebtoken"
const sampleIdPayload = { const sampleIdPayload = {
iss: "https://accounts.google.com", iss: "https://accounts.google.com",
@@ -20,10 +19,12 @@ const sampleIdPayload = {
given_name: "Test", given_name: "Test",
family_name: "Admin", family_name: "Admin",
iat: 1716891837, iat: 1716891837,
exp: 1716895437,
} }
const encodedIdToken = jwt.sign(sampleIdPayload, "test") const encodedIdToken = generateJwtToken(sampleIdPayload, {
secret: "test",
expiresIn: "1d",
})
const baseUrl = "https://someurl.com" const baseUrl = "https://someurl.com"
+1 -1
View File
@@ -5572,7 +5572,6 @@ __metadata:
ioredis: ^5.2.5 ioredis: ^5.2.5
ioredis-mock: 8.4.0 ioredis-mock: 8.4.0
jest: ^25.5.4 jest: ^25.5.4
jsonwebtoken: ^9.0.0
lodash: ^4.17.21 lodash: ^4.17.21
medusa-telemetry: ^0.0.17 medusa-telemetry: ^0.0.17
morgan: ^1.9.1 morgan: ^1.9.1
@@ -6197,6 +6196,7 @@ __metadata:
dotenv: ^16.4.5 dotenv: ^16.4.5
express: ^4.18.2 express: ^4.18.2
jest: ^29.6.3 jest: ^29.6.3
jsonwebtoken: ^9.0.2
knex: 2.4.2 knex: 2.4.2
rimraf: ^5.0.1 rimraf: ^5.0.1
ts-jest: ^29.1.1 ts-jest: ^29.1.1