javascript - Electron Auth0Lock "Origin file://not allowed"

标签 javascript node.js electron auth0

试图让 auth0 与我的 Electron 应用程序一起工作。当我按照默认教程尝试使用用户名-密码-身份验证进行身份验证时,锁定失败并出现 403 错误并响应“不允许原始文件://”。

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

Auth0 Lock with console errors

Origin file:// is not allowed

编辑:

Electron 锁设置

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();
})

最佳答案

通过在我的 Electron 应用程序中使用内部快速服务器来处理服务页面,我能够让 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 锁按预期进行身份验证。

关于javascript - Electron Auth0Lock "Origin file://not allowed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48465130/

相关文章:

javascript:确保元素始终出现在顶部

mysql - 在 sequelize.js 中,是否可以取消嵌套包含嵌套的查询结果?

javascript - 如何链接 CSS 和 Jade?

json - Node.js 服务器 : JSON. 字符串化深层对象

javascript - Electron 应用程序中是否有可用的 GridView 模块/组件?

javascript - RadioButton 更改 jquery 方法在 DOM 更改后不起作用?

javascript - 如何使用scrollTop 滚动到页面末尾?

javascript - 如何在 Keystone.js 以外的 .js 文件中使用 .env 文件变量?

javascript - 在 Windows 上指定 Sequelize sqlite 路径

angular - 在 Angular 9 中记录整个桌面