试图让 auth0 与我的 Electron 应用程序一起工作。当我按照默认教程尝试使用用户名-密码-身份验证进行身份验证时,锁定失败并出现 403 错误并响应“不允许原始文件://”。
我还在 auth0 仪表板中的客户端设置的允许来源 (CORS) 部分添加了“file://*”。
Auth0 Lock with console errors
编辑:
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(<SOME_PORT>, (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/