Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Use AuthGuard with gRPC Metadata

import {Injectable, CanActivate, ExecutionContext} from '@nestjs/common';
import {Observable, throwError} from 'rxjs';
import {JwtService} from '@nestjs/jwt';

@Injectable()
export class GrpcAuthGuard implements CanActivate {

    constructor(private  jwtService: JwtService) {

    }

    canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
        const type = context.getType();
        const prefix = 'Bearer ';
        let header;
        if(type==='rpc') {
            const metadata = context.getArgByIndex(1); // metadata
            if (!metadata) {
                return false;
            }
            header = metadata.get('Authorization')[0];
        }

        if (!header || !header.includes(prefix)) {
            return false;
        }

        const token = header.slice(header.indexOf(' ') + 1);
        try {
            const valid = this.jwtService.verify(token);
            return true;
        } catch (e) {
            return false;
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript map interface 
Typescript :: if a directive called 10000 times screen getting struck 
Typescript :: res.write prints html tags as text in express 
Typescript :: how to get all arrangments python 
Typescript :: typescript Empty Types 
Typescript :: 2 counts of IllegalAnnotationExceptions 
Typescript :: firebase not found in envirorment.ts file angular 
Typescript :: function call in Angular using typescript creates infinite loop 
Typescript :: check if a user already exists firebase realtime database react native 
Typescript :: economic tracking portfolio construction 
Typescript :: Error: "Filesystem" plugin is not implemented on android 
Typescript :: typescript baseurl 
Typescript :: how many sets of 3 in 4 
Typescript :: change css to scss in angular online 
Typescript :: testing tools vs testing techniques 
Typescript :: fiber absorption loss measurement 
Typescript :: typescript set 
Typescript :: typescript dynamic type 
Typescript :: how to separate a string into 2 lists of numbers and letters python 
Typescript :: how to mark plots octave 
Cpp :: c++ clear console 
Cpp :: excel vba delete worksheet if exists 
Cpp :: c++ typedef array 
Cpp :: lpcwstr to string c++ 
Cpp :: c++ short if 
Cpp :: round all columns in R dataframe to 3 digits 
Cpp :: how to get a random number between two numbers in c++ 
Cpp :: how to calculate polar coordinates in c++ 
Cpp :: c++ console color some digits 
Cpp :: creator of C++ 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =