Electron Auth0Lock“原始文件://不允许”

Law*_*nce 4 javascript node.js auth0 electron

尝试让 auth0 与我的电子应用程序一起使用。当我按照默认教程并尝试使用用户名-密码-身份验证进行身份验证时,锁定失败并显示 403 错误,并响应“不允许使用 Origin file://”。

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

Auth0 锁定并出现控制台错误

不允许原始文件://

编辑:

电子中的锁定设置

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();
})
Run Code Online (Sandbox Code Playgroud)

Law*_*nce 5

我能够通过在我的电子应用程序中使用内部 Express 服务器来处理服务页面来让 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 ...');
});
Run Code Online (Sandbox Code Playgroud)

然后在 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());
})
Run Code Online (Sandbox Code Playgroud)

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

  • @florin 我用这个 https://github.com/lawrencezahner/quark 创建了一个 npm 模块 (2认同)