1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Nest-js Headerデコレータのカスタム

Posted at
tags: nest-js
import { createParamDecorator, ExecutionContext,BadRequestException } from '@nestjs/common'
import * as admin from 'firebase-admin'

export const CustomHeaders =  createParamDecorator(
  (data: string, ctx: ExecutionContext) => {
    const req = ctx.switchToHttp().getRequest()
    const value = req.headers[data].split('Bearer ')[1]
    
    return admin.auth().verifySessionCookie(value, true).then((decodedClaims) => {
        return decodedClaims.user_id
    }).catch(err => {
        console.log("err:",err)
        throw new BadRequestException('Error:Validation failed. Is the authorization appropriate?');
    })
  }
)

@Post()
  async sample(@CustomHeaders('authorization') uid) {
    const result = await this.sampleService.sample(uid)
    return result
  }

解説

firebaseのセッションを受け取って(ヘッダーに添付),userIdを返すPipeを作ろうとしたが、既存のデコレータ(@Header())はPipeに互換性がなかったため、セッションを受け取ってuseridを返すデコレータを作った。

参照

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?