首页 > 解决方案 > 如何在 nativeScript 中使用 fusionAuth 库?

问题描述

我尝试按照 nativescript 文档使用 lib,但是当我尝试使用它时,除了一些诸如 net 和 tls 之类的模块之外,我无法解决一个错误,它无法自动找到它。使用 npm install 放置 tls 和 net 后我无法解决这个问题 'TypeError: Cannot read property' browser 'of undefined' 我不知道如何解决这个问题,测试 lib 是否正常工作我尝试使用ionic 4,它工作正常,但在 nativescript 中,我在 Angular 和 Vue 中都发现了这个问题。

Webpack compilation complete. Watching for file changes.
Webpack build done!
Successfully synced application org.nativescript.studyLogin on device 0aa24655.
Successfully transferred bundle.292d892ab2d871e6a00e.hot-update.js on device 0aa24655.
Successfully transferred 292d892ab2d871e6a00e.hot-update.json on device 0aa24655.
Successfully transferred bundle.js on device 0aa24655.
Successfully transferred runtime.js on device 0aa24655.
Successfully transferred vendor.js on device 0aa24655.
Restarting application on device 0aa24655...
Successfully synced application org.nativescript.studyLogin on device 0aa24655.
Successfully transferred bundle.e980eda364519dbb87a0.hot-update.js on device 0aa24655.
Successfully transferred e980eda364519dbb87a0.hot-update.json on device 0aa24655.
Refreshing application on device 0aa24655...
ActivityManager: Permission Denial: getCurrentUser() from pid=2971, uid=10187 requires android.permission.INTERACT_ACROSS_USERS
System.err: An uncaught Exception occurred on "main" thread.
System.err: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException: Error calling module function 
System.err: TypeError: Cannot read property 'browser' of undefined
System.err: File: (file:///node_modules/pbkdf2/lib/default-encoding.js:3:11)
System.err: StackTrace: 
System.err:     ../node_modules/pbkdf2/lib/default-encoding.js(file:///node_modules/pbkdf2/lib/default-encoding.js:3:11)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/pbkdf2/lib/sync.js(file:///node_modules/pbkdf2/lib/sync.js:14:22)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/pbkdf2/index.js(file:///node_modules/pbkdf2/index.js:24:23)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/crypto-browserify/index.js(file:///node_modules/crypto-browserify/index.js:14:8)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/request/lib/helpers.js(file:///node_modules/request/lib/helpers.js:4:13)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/request/index.js(file:///node_modules/request/index.js:19:14)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClient.js(file:///node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClient.js:19:14)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClientBuilder.js(file:///node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClientBuilder.js:18:28)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/src/FusionAuthClient.js(file:///node_modules/@fusionauth/typescript-client/build/src/FusionAuthClient.js:18:35)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/index.js(file:///node_modules/@fusionauth/typescript-client/build/index.js:21:9)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/lib/fusionAuthClientInstance.ts(file:///src/app/lib/fusionAuthClientInstance.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/item/items.component.ts(file:///src/app/item/items.component.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/app-routing.module.ts(file:///src/app/app-routing.module.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/app.module.ts(file:///src/app/app.module.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at (file:///data/data/org.nativescript.studyLogin/files/app/bundle.js:490:73)
System.err:     at ./main.ts(file:///data/data/org.nativescript.studyLogin/files/app/bundle.js:560:30)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at checkDeferredModules(file:///src/webpack/bootstrap:43:0)
System.err:     at webpackJsonpCallback(file:///src/webpack/bootstrap:30:0)
System.err:     at (file:///data/data/org.nativescript.studyLogin/files/app/bundle.js:2:57)
System.err:     at require(:1:266)
System.err: TypeError: Cannot read property 'browser' of undefined
System.err: StackTrace:
System.err: java.lang.RuntimeException: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException: Error calling module function 
System.err: TypeError: Cannot read property 'browser' of undefined
System.err: File: (file:///node_modules/pbkdf2/lib/default-encoding.js:3:11)
System.err: StackTrace: 
System.err:     ../node_modules/pbkdf2/lib/default-encoding.js(file:///node_modules/pbkdf2/lib/default-encoding.js:3:11)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/pbkdf2/lib/sync.js(file:///node_modules/pbkdf2/lib/sync.js:14:22)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/pbkdf2/index.js(file:///node_modules/pbkdf2/index.js:24:23)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/crypto-browserify/index.js(file:///node_modules/crypto-browserify/index.js:14:8)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/request/lib/helpers.js(file:///node_modules/request/lib/helpers.js:4:13)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/request/index.js(file:///node_modules/request/index.js:19:14)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClient.js(file:///node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClient.js:19:14)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClientBuilder.js(file:///node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClientBuilder.js:18:28)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/src/FusionAuthClient.js(file:///node_modules/@fusionauth/typescript-client/build/src/FusionAuthClient.js:18:35)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/index.js(file:///node_modules/@fusionauth/typescript-client/build/index.js:21:9)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/lib/fusionAuthClientInstance.ts(file:///src/app/lib/fusionAuthClientInstance.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/item/items.component.ts(file:///src/app/item/items.component.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/app-routing.module.ts(file:///src/app/app-routing.module.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/app.module.ts(file:///src/app/app.module.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at (file:///data/data/org.nativescript.studyLogin/files/app/bundle.js:490:73)
System.err:     at ./main.ts(file:///data/data/org.nativescript.studyLogin/files/app/bundle.js:560:30)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at checkDeferredModules(file:///src/webpack/bootstrap:43:0)
System.err:     at webpackJsonpCallback(file:///src/webpack/bootstrap:30:0)
System.err:     at (file:///data/data/org.nativescript.studyLogin/files/app/bundle.js:2:57)
System.err:     at require(:1:266)
System.err: TypeError: Cannot read property 'browser' of undefined
System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4705)
System.err:     at android.app.ActivityThread.access$1400(ActivityThread.java:168)
System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1389)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
System.err:     at android.os.Looper.loop(Looper.java:176)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:5493)
System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
System.err:     at java.lang.reflect.Method.invoke(Method.java:525)
System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
System.err:     at dalvik.system.NativeStart.main(Native Method)
System.err: Caused by: com.tns.NativeScriptException: Error calling module function 
System.err: TypeError: Cannot read property 'browser' of undefined
System.err: File: (file:///node_modules/pbkdf2/lib/default-encoding.js:3:11)
System.err: StackTrace: 
System.err:     ../node_modules/pbkdf2/lib/default-encoding.js(file:///node_modules/pbkdf2/lib/default-encoding.js:3:11)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/pbkdf2/lib/sync.js(file:///node_modules/pbkdf2/lib/sync.js:14:22)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/pbkdf2/index.js(file:///node_modules/pbkdf2/index.js:24:23)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/crypto-browserify/index.js(file:///node_modules/crypto-browserify/index.js:14:8)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/request/lib/helpers.js(file:///node_modules/request/lib/helpers.js:4:13)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/request/index.js(file:///node_modules/request/index.js:19:14)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClient.js(file:///node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClient.js:19:14)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClientBuilder.js(file:///node_modules/@fusionauth/typescript-client/build/src/DefaultRESTClientBuilder.js:18:28)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/src/FusionAuthClient.js(file:///node_modules/@fusionauth/typescript-client/build/src/FusionAuthClient.js:18:35)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ../node_modules/@fusionauth/typescript-client/build/index.js(file:///node_modules/@fusionauth/typescript-client/build/index.js:21:9)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/lib/fusionAuthClientInstance.ts(file:///src/app/lib/fusionAuthClientInstance.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/item/items.component.ts(file:///src/app/item/items.component.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/app-routing.module.ts(file:///src/app/app-routing.module.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at ./app/app.module.ts(file:///src/app/app.module.ts:1:0)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at fn(file:///src/webpack/bootstrap:120:0)
System.err:     at (file:///data/data/org.nativescript.studyLogin/files/app/bundle.js:490:73)
System.err:     at ./main.ts(file:///data/data/org.nativescript.studyLogin/files/app/bundle.js:560:30)
System.err:     at __webpack_require__(file:///src/webpack/bootstrap:750:0)
System.err:     at checkDeferredModules(file:///src/webpack/bootstrap:43:0)
System.err:     at webpackJsonpCallback(file:///src/webpack/bootstrap:30:0)
System.err:     at (file:///data/data/org.nativescript.studyLogin/files/app/bundle.js:2:57)
System.err:     at require(:1:266)
System.err: TypeError: Cannot read property 'browser' of undefined
System.err:     at com.tns.Runtime.runModule(Native Method)
System.err:     at com.tns.Runtime.runModule(Runtime.java:662)
System.err:     at com.tns.Runtime.run(Runtime.java:654)
System.err:     at com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
System.err:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4702)
System.err:     ... 10 more
ActivityManager: Process org.nativescript.studyLogin (pid 2971) (adj 0) has died.
Successfully synced application org.nativescript.studyLogin on device 0aa24655.

标签: typescriptnativescriptfusionauth

解决方案


很高兴看到人们使用 typescript 客户端。不幸的是,我们不直接支持 NativeScript,而且我们的 DefaultRESTClient 似乎导入了 NativeScript 不支持的东西。

要自行解决此问题,您需要使用 webpack 使用DefaultRESTClient与 NativeScript 兼容的自定义实现来覆盖,或者使用为 NativeScript提供api 的东西覆盖cross-fetch内部使用的包。DefaultRESTClientfetch

你也可以在我们的 Github 上为TypeScript开一张票,表明 NativeScript 是你想要支持的东西,如果它得到足够的支持或看起来像是我们应该支持的重要东西,我们将完成它。


推荐阅读