thiagobustamante / typescript-rest

This is a lightweight annotation-based expressjs extension for typescript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How we can get user inforamtion in controller method decoded from @Secourity decorator

surinder-enest opened this issue · comments

@thiagobustamante Thanks for your Awesome work :)
Can you please help me to get the decoded user id

I am done with integration everything is working fine.

    private configureAuthenticator() {
        const JWT_SECRET: string = process.env.ENCRYPT_PWD;
        const jwtConfig: StrategyOptions = {
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            secretOrKey: Buffer.from(JWT_SECRET)
        };
        const strategy = new Strategy(jwtConfig, (payload: any, done: (err: any, user: any) => void) => {
            done(null, payload);
        });
        const authenticator = new PassportAuthenticator(strategy, {
            deserializeUser: (user: string) => JSON.parse(user),
            serializeUser: (user: any) => {
                return JSON.stringify(user);
            }
        });
        Server.registerAuthenticator(authenticator);
        Server.registerAuthenticator(authenticator, TOKEN_KEY.JWT_AUTH);
    }
    @Tags('Auth')
    @Path("getCurrectUser")
    @POST
    @Security('*', TOKEN_KEY.JWT_AUTH)
    public async getCurrectUser(): Promise<any> {
        return "Working";
    }

Here i need to get user id from the deserializeUser (decoded from the jwt) in the getCurrectUser method.

One way would be to collect the request header from the context and then deserialize the token user.
Inside the controller:

@context
private context: ServiceContext;

Inside the function
this.context.request