Auth0-Angular在Angular SSR Universal下不能正常工作



我有一个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文档和angular-universal文档中读到这一点。我现在明白了这个问题,并且有一个解决方案,这已经得到了两个库的承认,但我发现很难实现。我想知道是否有一些样例实现相同,如果有谁已经做到了这一点?

任何帮助都是感激的。衷心感谢大家。

我遇到了同样的问题,并花了一些时间深入研究,下面是可能的解决方案。

将这些更改应用到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;
}

同时添加一个provider

{provide: AuthService, useValue: {}

最新更新