【问题标题】:Auth0-Angular is not working fine with Angular SSR UniversalAuth0-Angular 不适用于 Angular SSR Universal
【发布时间】:2022-10-07 16:08:38
【问题描述】:

我有一个 Angular 13 应用程序,使用 @auth0/auth0-angular。 当我尝试实现 SSR 时,它会吐出未定义窗口的错误。 我将以下内容添加到server.ts

  const domino = require('domino');
  const fs = require('fs');
  const path = require('path');
  const template = fs.readFileSync(path.join(__dirname, '..', 'browser', 'index.html')).toString();
  const win = domino.createWindow(template);
  global['window'] = win;
  global['document'] = win.document;

这解决了一些问题,但是经过数小时的故障排除后,我知道剩下的错误是由 Auth0 吐出的,@auth0/auth0-angular

我在@auth0/auth0-angular 文档中阅读了this,在angular-universal 文档中阅读了this。 我现在理解了这个问题,并且有一个解决方案,这两个库都承认了,但是我发现很难实施。 我想知道是否有相同的示例实现,是否有人已经这样做了?

任何帮助表示赞赏。 谢谢大家的期待。

【问题讨论】:

    标签: angular auth0 angular-universal angular13


    【解决方案1】:

    我遇到了同样的问题,并花了一些时间来研究它,这是可能的解决方案。

    将这些更改应用到 app.server.module:

    import * as crypto from 'crypto';
    import { createWindow } from 'domino';
    import * as fs from 'fs';
    
    if (!global.window) {
      const template = fs.readFileSync('./src/index.html').toString();
      const win = createWindow(template);
      (global as any).window = win;
      (global as any).window.crypto = crypto;
      (global as any).window.isMobileOnServer = true;
      (global as any).document = win.document;
      (global as any).navigator = win.navigator;
      (global as any).location = win.location;
    }
    

    还添加一个提供者

    {provide: AuthService, useValue: {}
    

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 2019-08-26
      • 2018-12-03
      • 1970-01-01
      • 2023-02-15
      • 2018-07-04
      • 2019-12-26
      • 2020-12-16
      相关资源
      最近更新 更多