-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add route to return the retification config value
- Loading branch information
1 parent
6f85bfb
commit a37a729
Showing
5 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
/* | ||
Disabling because `status` and `body` are not defined in the `Context` type provided by VTEX. | ||
However, they are dynamically added by the middleware in the runtime, and using them works as expected. | ||
*/ | ||
export async function getEmailRetificationConfig( | ||
ctx: Context, | ||
next: () => Promise<any> | ||
) { | ||
try { | ||
const { storeUserAuthToken } = ctx.vtex | ||
const { account } = decodeToken(storeUserAuthToken ?? '') | ||
|
||
if (account !== ctx.vtex.account) { | ||
setForbiddenStatus(ctx) | ||
|
||
return | ||
} | ||
} catch (e) { | ||
setForbiddenStatus(ctx) | ||
|
||
return | ||
} | ||
|
||
// @ts-ignore | ||
ctx.status = 200 | ||
// @ts-ignore | ||
ctx.body = await ctx.clients.oms.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 | ||
// @ts-ignore | ||
ctx.body = 'Forbidden' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters