将 jsonwebtoken 与 angular-cli 应用程序一起使用时出错 [英] Error using jsonwebtoken with angular-cli application

查看:30
本文介绍了将 jsonwebtoken 与 angular-cli 应用程序一起使用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 angular2 应用程序,它使用 angular-cli 作为脚手架和其他任务,但现在我不能使用 jsonwebtoken 在我的 angular2 应用程序中.

I have an angular2 application that uses the angular-cli for the scaffold and other tasks, but now I can't use jsonwebtoken on my angular2 application.

我添加了依赖

yarn add --save jsonwebtoken

我实际上可以在 node_modules 处看到这个库,我做了

and I actually can see the library at node_modules, I do the

import { jwt } from 'jsonwebtoken'

在我的 TypeScript 代码中,IDE 没有给我任何警告,因此它可以正确找到依赖项.

in my TypeScript code and the IDE gives me no warning about it, so it can find the dependency correctly.

但是当我执行 npm startyarn start 时,我收到以下错误消息

But when I do npm start or yarn start I have the following error message

ERROR in ./~/isemail/lib/index.js
Module not found: Error: Can't resolve 'dns' in '/home/$MY_USER_NAME_PLACE_HOLDER/dir/to/my/app/node_modules/isemail/lib'
 @ ./~/isemail/lib/index.js 5:12-26
 @ ./~/joi/lib/string.js
 @ ./~/joi/lib/index.js
 @ ./~/jsonwebtoken/sign.js
 @ ./~/jsonwebtoken/index.js
 @ ./src/app/_auth/authentication.service.ts
 @ ./src/app/app.component.ts
 @ ./src/app/index.ts
 @ ./src/main.ts
 @ multi main

ERROR in [default] /home/$MY_USER_NAME_PLACE_HOLDER/dir/to/my/appsrc/app/_auth/authentication.service.ts:41:24 
Property 'validate' does not exist on type 'typeof "/home/$MY_USER_NAME_PLACE_HOLDER/dir/to/my/app/node_modules/@types/jsonwebtoken/index"'.

当我在互联网上搜索错误时,我关注了一系列对话和相关项目.我发现:

I follow a serie of conversations and related projects when I searched for the error on internet. And I found that:

  • 有一个类似的问题,但没有详细 它没有回答.

react-validation-mixinisemailjoi 引导我到这个 one,当他们描述类似的问题时,导入依赖于 dnsnet 或来自 node 的其他一些本机模块的库,并使我认为在将此类库与 webpack 一起使用时存在一些问题(我没有专业知识可以确定这一点,如果我错了,请纠正),解决方法是添加以下几行到 webpack.config

Some related errors in react-validation-mixin, isemail and joi lead me to this one, when they describe similar problems doing import of libraries that depends on dns, net or some other native modules from node and makes me think that there is some problems when using this kind of libraries exclusively with webpack (I have no the expertise to be sure of that, please correct if I am wrong) and the workaround is to add the following lines to the webpack.config

node: {
  net: 'empty',
  tls: 'empty',
  dns: 'empty'
}

但我的问题是,使用 angular-cli 有 no覆盖 webpack 配置的方法,并且无意改变它.

But my problem here is, with angular-cli there is no way to override webpack config and there is no intention to change that.

所以我不知道在这里做什么,您知道如何在我的 angular-cli 应用程序中使用 jsonwebtoken 吗?

So I don't know what to do here, have you any idea of how can I use jsonwebtoken with my angular-cli application ?

推荐答案

我在尝试使用 jsonwebtoken 客户端时遇到了同样的问题(jsonwebtoken 在我的 Express 应用程序中在服务器端工作得很好).

I ran into the same problem when attempting to use jsonwebtoken client-side (jsonwebtoken works just fine server-side in my Express app).

简短的回答是可能无法使用 https://github.com/Hendrixer/jwt-decode jsonwebtoke 在你的 Angular 应用中.如果您只想解码 JWT 的客户端以执行诸如验证之类的操作,那么该库有点重,而该模块实际上是为在 node.js 中的服务器端使用而设计的.

The short answer is that it may not be possible to use https://github.com/Hendrixer/jwt-decode jsonwebtoke in your angular app. The library is a bit heavy if all you're wanting to do is decode JWT's client-side for doing things like validation, and the module is really intended for server-side use in node.js.

但是,有许多替代方案可以在 angular 2(包括 angular-cli、webpack)中在客户端工作.例如,Jwt-decode 将解码 JWT.

However, there are a number of alternatives available that DO work client side in angular 2 (including in angular-cli, webpack). Jwt-decode will decode JWTs, for example.

然而,我发现最好的选择是重构我的代码以使用 Angular2-JWT 的 AuthHttp 类负责为您在 http 标头中传递令牌.如果令牌不存在或未通过验证,它也会抛出错误,您可以随意处理.

However, I found that the best alternative was to refactor my code to use Angular2-JWT's AuthHttp class which takes care of passing the token in the http header for you. It will also throw an error if the token doesn't exist or doesn't pass validation, which you can handle as you like.

这篇关于将 jsonwebtoken 与 angular-cli 应用程序一起使用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆