Skip to content

Commit

Permalink
Extract decode function
Browse files Browse the repository at this point in the history
  • Loading branch information
rerissondaniel committed Jan 30, 2025
1 parent d0973b9 commit c7331e5
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions node/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ export async function getEmailRetificationConfig(
) {
try {
const { storeUserAuthToken } = ctx.vtex
const userStoreToken = storeUserAuthToken?.includes('.')
? storeUserAuthToken.split('.')[1]
: ''

const { account } = JSON.parse(
Buffer.from(userStoreToken, 'base64').toString()
)
const { account } = decodeToken(storeUserAuthToken ?? '')

if (account !== ctx.vtex.account) {
setForbiddenStatus(ctx)
Expand All @@ -36,6 +30,17 @@ export async function getEmailRetificationConfig(
await next()
}

function decodeToken(token: string) {
if (!token || !token.includes('.')) {
throw new Error('Invalid token')
}

const [, encodedPayload] = token.split('.')
const payload = Buffer.from(encodedPayload, 'base64').toString()

return JSON.parse(payload)
}

function setForbiddenStatus(ctx: Context) {
// @ts-ignore
ctx.status = 403
Expand Down

0 comments on commit c7331e5

Please sign in to comment.