电子 Auth0Lock "Origin file:// not allowed"

Electron Auth0Lock "Origin file:// not allowed"

正在尝试让 auth0 与我的电子应用程序一起使用。当我按照默认教程尝试使用 Username-Password-Authentication 进行身份验证时,锁定失败并显示 403 错误并响应 "Origin file:// is not allowed"。

我还在 auth0 仪表板中的客户端设置的允许来源 (CORS) 部分添加了 "file://*"。

Auth0 Lock with console errors

Origin file:// is not allowed

编辑:

电子锁设置

var lock = new Auth0Lock(
   'McQ0ls5GmkJRC1slHwNQ0585MJknnK0L', 
   'lpsd.auth0.com', {
    auth: {
            redirect: false,
            sso: false
    }
});

document.getElementById('pill_login').addEventListener('click', function (e) {
    e.preventDefault();
    lock.show();
})

我能够通过在我的电子应用程序中使用内部快速服务器来处理服务页面来让 Auth0 工作。

首先,我在名为 http 的项目的单独文件夹中创建了一个基本的 Express 应用程序,这里将提供 Express 服务器代码和 html 文件。

const path = require('path');

const express = require('express');
const app = express();

app.use(express.static(process.env.P_DIR)); // Serve static files from the Parent Directory (Passed when child proccess is spawned).

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:<PORT>'); // Set this header to allow redirection from localhost to auth0
    next();
})


// Default page to serve electron app
app.get('/index', (req, res) => {
    res.sendFile(__dirname + '/index.html');
})

// Callback for Auth0
app.get('/auth/callback', (req, res) => {
    res.redirect('/index'); 
})

// Listen on some port
app.listen(&lt;SOME_PORT&gt;, (err) => {
    if (err) console.log(err);
    console.log('HTTP Server running on ...');
});

然后在 Electron 主进程中,我将 express 服务器作为子进程生成

const {spawn} = require('child_process');

const http = spawn('node', ['./dist/http/page-server.js'], {
    env: {
        P_DIR: __dirname // Pass the current dir to the child process as an env variable, this is for serving static files in the project
    }
});

// Log standard output
http.stdout.on('data', (data) => {
    console.log(data.toString());
})

// Log errors
http.stderr.on('data', (data) => {
    console.log(data.toString());
})

现在 auth0 锁按预期进行身份验证。