【问题标题】:Electron Auth0Lock "Origin file:// not allowed"Electron Auth0Lock“原始文件://不允许”
【发布时间】:2018-07-06 00:22:51
【问题描述】:

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

【问题讨论】:

  • 尝试设置本地 http 服务器。 PHP、node 和 python 都内置了简单的 http 服务器。
  • 请使用 stackoverflow 图片上传器上传图片,因为这样可以消除图片与问题分离的可能性。另外,请将代码粘贴到代码块中,而不是上传代码图像,以帮助人们通过复制/粘贴重新创建您的问题(通过使每行代码缩进至少 4 个空格或 1 个制表符来创建代码块)
  • @Scoots 我使用问题编辑器顶部的图片按钮上传图片,您能指导我到正确的位置上传图片吗?
  • 抱歉,我一定是看错了网址。这是一种完全可以接受的包含图片的方式。
  • @Scoots Ahhh,您必须拥有 10 名声望才能在问题中包含图片。今天只创建我的帐户的缺点!

标签: javascript node.js electron auth0


【解决方案1】:

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

【讨论】:

  • 你能提供一个 git 例子吗?我有同样的问题,但我不确定我是否理解解决方案
  • @florin 我用这个github.com/lawrencezahner/quark创建了一个npm模块
猜你喜欢
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
  • 2020-05-25
  • 2016-03-02
  • 2019-07-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多