尝试让auth0使用我的电子应用程序。当我按照默认教程并尝试使用Username-Password-Authentication进行身份验证时,锁定失败并显示403错误,并以“Origin file:// is not allowed”响应。
我还在auth0信息中心的客户端设置的Allowed Origins(CORS)部分添加了“file:// *”。
Auth0 Lock with console errors
编辑:
锁定电子设置
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();
})
答案 0 :(得分:3)
通过在我的电子应用程序中使用内部快速服务器来处理服务页面,我能够让Auth0工作。
首先,我在我的项目名为http的单独文件夹中创建了一个基本快速应用程序,这里将是快速服务器代码和要提供的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主过程中,我将快速服务器作为子进程生成
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锁定按预期进行身份验证。