在Angular-Cli应用程序中使用jsonwebtoken时出错 [英] Error using jsonwebtoken with angular-cli application

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

问题描述

我有一个angular2应用程序,它使用 angular-cli 脚手架和其他任务,但现在我无法在我的angular2上使用 jsonwebtoken 应用程序.

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-mixin , isemail

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时否覆盖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在您的角度应用程序中.如果您只想解码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.

但是,有很多可用的替代方法可以在角度2(包括angular-cli,webpack)中运行客户端.例如, Jwt-解码将解码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.

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

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