Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

jwt-transoform npm

export const jwtTransform = (whiteListPath: string[]): Handler => {
  return async function (req: Request, res: Response, next: NextFunction): Promise<OutgoingMessage> {
    try {
      if (!whiteListPath.includes(req.path.toLocaleLowerCase().replace('/api/v1/', ''))) {
        let headers: IncomingHttpHeaders = req.headers
        if (!Object.keys(headers).includes('authorization')) throw apiResponse(status.UNAUTHORIZED, 'Authorization is required')

        const authorization: boolean | undefined = (headers.authorization as string).includes('Bearer')
        if (!authorization) throw apiResponse(status.UNAUTHORIZED, 'Bearer is required')

        const accessToken: string = (headers.authorization as string).split('Bearer ')[1]
        if (assert.isUndefined(accessToken as any)) throw apiResponse(status.UNAUTHORIZED, 'Access Token is required')

        const validJwt: string[] = (accessToken as string).split('.')
        if (validJwt?.length !== 3) throw apiResponse(status.UNAUTHORIZED, 'Access Token format is not valid')

        // overwrite authorization headers
        req.headers.authorization = `Bearer ${await decrypt(accessToken, 20)}`
      }
      next()
    } catch (e: any) {
      return res.status(e.stat_code || status.UNAUTHORIZED).json(e)
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: get all products woocommerce with sql 
Typescript :: when 2 emits on a same chatroom at a time only one is working using socket.io 
Typescript :: How to join all url segments to make a url in javascipt 30seconds of code 
Typescript :: .env.local is not working inside useEffect 
Typescript :: the benefits of deploying a network using a WLC 
Typescript :: who indirectly elects the president 
Typescript :: More than one custom value accessor matches form control with unspecified name attribute 
Typescript :: check if package exists inside the device adb 
Typescript :: gang beasts türkiye discord 
Typescript :: Algebra is simply overlaying sets of equations onto the world around us. 
Typescript :: corpses:2249 Livewire: The published Livewire assets are out of date 
Typescript :: typeorm versioncolumn 
Typescript :: typescript interface optional 
Typescript :: ?In static pages, the contents are fluid and changeable (e.g., rotating banners). 
Typescript :: react native websocket disconnect handler 
Typescript :: see all github issue comments i made site:stackoverflow.com 
Typescript :: how to send events data to branch from server 
Typescript :: - laravel/ui[v3.2.0, ..., 3.x-dev] require illuminate/console ^8.0 - found illuminate/console[v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require. 
Typescript :: How to exclude a particular test group from a test case execution? 
Typescript :: how to use array pop in typescript 
Typescript :: nodejs transofrm method into promise method 
Typescript :: How to check that tuple A contains all elements of tuple B python? 
Typescript :: les différents types de cours 
Typescript :: how to make the score add on while its in a loop in python 
Typescript :: translate a vector 
Typescript :: how to set value to readonly property in typescript while unit testing 
Typescript :: print gets opened whenever pdf is opened 
Typescript :: how to collect array of objects in one value key in laravel 
Typescript :: struts 2 form tags 
Typescript :: 3 parts of apptitude 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =