Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nest.js jsencrypt does not work with error window is not defined #303

Open
ingyeoking13 opened this issue Dec 11, 2023 · 0 comments
Open

Comments

@ingyeoking13
Copy link

export class ClientDecryptAuthGuard implements CanActivate {
    constructor() {}
    async decrypt(req) {
        const JSEncrypt = (await import('jsencrypt')).default;
        const rsa = new JSEncrypt();
        rsa.setPrivateKey(keys.rsaPrivKey);
        const token = rsa.decrypt(req.token);
        const bodyOriginJson = CryptoJS.AES.decrypt(
            req.body['data'],
            token.toString(),
        ).toString(CryptoJS.enc.Utf8);
        return [bodyOriginJson, token];
    }

    async canActivate(context: ExecutionContext): Promise<boolean> {
        const req: Request = context.switchToHttp().getRequest();
        [req.body, req['token']] = await this.decrypt(req);
        const data = req.body;
        return true;
    }
}

ReferenceError: window is not defined
at Object. (/Users/gaelim/Project/violet-project/violet-backend/node_modules/jsencrypt/bin/jsencrypt.js:18:4)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Function.Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at require (node:internal/modules/cjs/helpers:110:18)
at /Users/gaelim/Project/violet-project/violet-backend/src/guards/clientDecryptAuth.guard.ts:15:28
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at ClientDecryptAuthGuard.decrypt (/Users/gaelim/Project/violet-project/violet-backend/src/guards/clientDecryptAuth.guard.ts:15:28)

using jsencrypt with nest js guard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant