bennymeg / nx-electron

Electron schematics for nrwl nx platform
Apache License 2.0
333 stars 85 forks source link

Having difficulties with proxying in the packaged app #153

Closed JulieMarie closed 2 years ago

JulieMarie commented 2 years ago

I've successfully built and served one of our Angular apps locally, but when I package it up using nx run <electron-app-name>:package, the proxying isn't working.

This is my proxy.conf.js:

const { refCount } = require("rxjs/operators");

let serverUrl = 'https://www.some-server.com/';
let localUrl = 'localhost:4200';
const localProto = 'http';

const PROXY_CONFIG = {
  '/api': {
    target: serverUrl,
    secure: false,
    changeOrigin: true,
  },
  '/start-login': {
    target: serverUrl,
    bypass: function (req, res, proxyOptions) {
      req.headers['X-Orig-Host'] = localUrl;
      req.headers['X-Orig-Proto'] = localProto;
    },
    pathRewrite: function (path) {
      return path.replace(/^(\/start-login)/, '') || '/';
    },
    secure: false,
    changeOrigin: true,
    logLevel: 'debug',
  },
  '/callback': {
    target: serverUrl,
    bypass: function (req, res, proxyOptions) {
      req.headers['X-Orig-Host'] = localUrl;
      req.headers['X-Orig-Proto'] = localProto;
    },
    pathRewrite: function (path) {
      return path.replace(/^(\/callback)/, '/_callback') || '/';
    },
    secure: false,
    changeOrigin: true,
    logLevel: 'debug',
  },
};

module.exports = PROXY_CONFIG;

When the app loads, it should redirect via /start-login to our authentication with the correct redirect_uri to get us back once we've logged in. What I'm ending up with is: Not allowed to load local resource: file:///start-login

I feel like it could be solved with registerFileLocal that I've read about in electron documentation, but then again, maybe that's supposed to be resolved by this?

  private static loadMainWindow() {
    // load the index.html of the app.
    if (!App.application.isPackaged) {
      App.mainWindow.loadURL(`http://localhost:${rendererAppPort}`);
    } else {
      App.mainWindow.loadURL(
        format({
          pathname: join(__dirname, '..', rendererAppName, 'index.html'),
          protocol: 'file:',
          slashes: true,
        })
      );
    }

Any help would be greatly appreciated. I feel like I'm almost there, but missing just one key piece.

bennymeg commented 2 years ago

Hi, I did not quite understood the usage of this code snippet, does this issue related to nx-electron or to electron in general? Anyway, you might need to bypass the file protocol CSP (but that is just a hunch).

JulieMarie commented 2 years ago

It's related to nx-electron, but before I ask any more questions, I'll look into the link you provided, thanks! I'll let you know what I find.

JulieMarie commented 2 years ago

Closing this as the issue seems to be more related to how we authenticate and not the electron app itself.