简体   繁体   English

使用Firebase和Electron

[英]Using Firebase with Electron

I'm trying to use Firebase with Electron . 我正在尝试使用Firebase和Electron When installing it just like I would on a web page it doesn't work because Electron pages are hosted locally and don't have a hostname . 当我在网页上安装它时,它不起作用,因为Electron页面是本地托管的,没有hostname This is the error I'm getting... 这是我得到的错误......

Uncaught Error: This domain is not authorized for OAuth operations for your Firebase project. Edit the list of authorized domains from the Firebase console.

I can't add an empty (or wildcard) authorized domain to the Firebase console so I'm therefore stuck. 我无法将空(或通配符)授权域添加到Firebase控制台,因此我卡住了。 Does anybody have any ideas of how to work around this? 有没有人对如何解决这个问题有任何想法?

edit: Here's the code I'm using, it's just the standard boilerplate, nothing extra... 编辑:这是我正在使用的代码,它只是标准的样板,没有什么额外的......

<script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script>
<script>
  var config = {
    apiKey: "AIzaSyBvmmPB0_Oddc-02cUj3Ntt3wi8jSxxxx",
    authDomain: "xxxxx-d24ad.firebaseapp.com",
    databaseURL: "https://xxxxx-d24ad.firebaseio.com",
    storageBucket: "",
  };
  firebase.initializeApp(config);
</script>

For now, you can suppress this error by removing the authDomain line from your config. 现在,您可以通过从配置中删除authDomain行来抑制此错误。 authDomain is needed for the Auth signInWithPopup/signInWithRedirect operations, but everything else should work. Auth signInWithPopup / signInWithRedirect操作需要authDomain,但其他一切都应该有效。

A version of the library that throws that error only when you actually try to do a signInWithPopup/Redirect is in the works. 只有在您实际尝试执行signInWithPopup / Redirect时才会抛出该错误的库版本正在运行中。

I don't know if this is the best solution but is a workaround. 我不知道这是否是最佳解决方案,但是是一种解决方法。

create a file server.js with a simple express server 使用简单的快速服务器创建文件server.js.

"server.js" “server.js”

var express = require('express');
var http = require('http');
var path = require('path');

var appServer = express();
appServer.use(express.static(path.join(__dirname, '')));

appServer.get('*', (req, res) => {
    res.sendFile(__dirname + 'index.html');
});

http.createServer(appServer).listen(3007, function() {
    console.log('Express server listening on port');
});

In your main.js(electron-main-js-file) 在你的main.js(electron-main-js文件)中

On the top of the main.js start the node server 在main.js的顶部启动节点服务器

require('./server');

and change the "win.loadURL" 并改变“win.loadURL”

win.loadURL('http://localhost:3007');

I've fork your project and implement, the error from firebase is gone but jQuery is not defined, I think you can fix that. 我已经分叉你的项目和实现,来自firebase的错误消失但jQuery没有定义,我想你可以修复它。

https://github.com/diegoddox/sad-electron-firebase-error https://github.com/diegoddox/sad-electron-firebase-error

You can use firebase auth's GitHub, Twitter, Facebook, Google Provider with electron by using the manual sign in flow and firebase auth signInWithCredential method. 您可以使用firebase auth的GitHub,Twitter,Facebook,Google Provider与电子使用手动登录流和signInWithCredential auth signInWithCredential方法。

https://firebase.google.com/docs/auth/web/github-auth#handle_the_sign-in_flow_manually https://firebase.google.com/docs/auth/web/github-auth#handle_the_sign-in_flow_manually

I created useful library for these case. 我为这些案例创建了有用的库。

https://github.com/mironal/electron-oauth-helper#firebase-auth-integration https://github.com/mironal/electron-oauth-helper#firebase-auth-integration

// Github manually flow example.

const { OAuth2Provider } = require("electron-oauth-helper")

const config = { /* oauth config. please see example/main/config.example.js.  */}
const provider = new OAuth2Provider(config)
provider.perform()
  .then(resp => {
    const query = querystring.parse(resp)
    const credential = firebase.auth.GithubAuthProvider.credential(query.access_token)
    firebase.auth().signInWithCredential(credential)
    .then(user => {
        console.log(user)
    })
    .catch(error => console.error(error))
  })
  .catch(error => console.error(error))

It's works in Electron 6.0.1 (Google, Facebook and Twitter Auth) through .signInWithPopup. 它可以通过.signInWithPopup在Electron 6.0.1(Google,Facebook和Twitter Auth)中使用。 Environment: 环境:

node 10.16.2 LTS
electron 6.0.1
vs2017-win2016
electron-forge 5.2.4

electron's package.json 电子的package.json

 "dependencies": {
 "@capacitor/electron": "^1.1.0",
 "electron-compile": "^6.4.4",
 "electron-squirrel-startup": "^1.0.0"
  },
  "devDependencies": {
   "babel-plugin-transform-async-to-generator": "^6.24.1",
   "babel-preset-env": "^1.7.0",
   "babel-preset-react": "^6.24.1",
   "electron-forge": "^5.2.4",
   "electron-prebuilt-compile": "4.0.0"
  }, 

Angular's package Angular的包

  "dependencies": {
"@angular/animations": "^8.2.0-next.1",
"@angular/cdk": "^8.0.0",
"@angular/common": "^8.2.0-next.1",
"@angular/core": "^8.2.0-next.1",
"@angular/fire": "^5.2.1",
"@angular/forms": "^8.2.0-next.1",
"@angular/material": "^8.0.0",
"@angular/platform-browser": "^8.2.0-next.1",
"@angular/platform-browser-dynamic": "^8.2.0-next.1",
"@angular/pwa": "^0.800.2",
"@angular/router": "^8.2.0-next.1",
"@angular/service-worker": "^8.2.0-next.1",
"@capacitor/core": "1.1.0",
"@ionic/angular": "^4.6.2",
"@ngx-loading-bar/core": "^4.2.0",
"@ngx-loading-bar/router": "^4.2.0",
"@ngxs/logger-plugin": "^3.3.2",
"@ngxs/store": "^3.3.2",
"angulartics2": "^7.5.2",
"echarts": "^4.2.1",
"firebase": "^6.1.1",
"hammerjs": "^2.0.8",
"immutable": "^4.0.0-rc.12",
"ngx-echarts": "^4.1.1",
"ngx-permissions": "^7.0.3",
"ngx-stars": "^1.3.0",
"rxjs": "6.5.2",
"zone.js": "~0.9.1"
},
"devDependencies": {
  "@angular-devkit/build-angular": "~0.802.0-next.0",
  "@angular/cli": "~8.2.0-next.0",
  "@angular/compiler": "^8.2.0-next.1",
  "@angular/compiler-cli": "~8.2.0-next.1",
  "@angular/language-service": "~8.2.0-next.1",
  "@capacitor/cli": "^1.0.0",
  "@compodoc/compodoc": "^1.1.10",
  "@ionic/angular-toolkit": "~2.0.0",
  "@ngxs/devtools-plugin": "^3.4.3",
  "@ngxs/schematics": "0.0.1-alpha.5",
  "@types/echarts": "^4.1.9",
  "@types/jasmine": "^3.3.12",
  "@types/jasminewd2": "~2.0.6",
  "@types/node": "~11.13.4",
  "ajv": "^6.9.1",
  "codelyzer": "~5.0.0",
  "html-minifier": "^4.0.0",
  "ionic": "^5.0.1",
  "jasmine-core": "~3.4.0",
  "jasmine-spec-reporter": "~4.2.1",
   "karma": "^4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.5",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"karma-mocha-reporter": "^2.2.5",
"protractor": "~5.4.0",
"sonarqube-scanner": "^2.4.0",
"ts-node": "~8.0.3",
"tslint": "~5.15.0",
"tslint-sonarts": "^1.9.0",
"typescript": "~3.4.3"
},

I've compiled for Windows with: 我用Windows编译了:

DEBUG='electron-forge:*' node_modules/.bin/electron-forge make

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM