move firebase credentials to a separate file

This commit is contained in:
or-else
2018-09-24 12:37:35 +03:00
parent 3ad8274ad8
commit c8a47efde2
6 changed files with 34 additions and 30 deletions
+12
View File
@@ -0,0 +1,12 @@
// Get these values for your project from the https://console.firebase.google.com/
// You can get most of this file at
// Project Settings -> General -> Your Apps -> [Add app] -> [Add Firebase to your Web app]
// The `messagingVapidKey` needs to be added separately.
const FIREBASE_INIT = {
// Project Settings -> Cloud Messaging, "Sender ID".
messagingSenderId: "114126160546",
// Project Settings -> Cloud Messaging -> Web configuration -> Web Push certificates.
// This value IS NOT included in the generated js config you can download from
// the firebase console. It needs to be added separately.
messagingVapidKey: "BOgQVPOMzIMXUpsYGpbVkZoEBc0ifKY_f2kSU5DNDGYI6i6CoKqqxDd7w7PJ3FaGRBgVGJffldETumOx831jl58"
};
+3 -2
View File
@@ -33,8 +33,8 @@
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script crossorigin src="https://unpkg.com/@babel/standalone/babel.min.js"></script> <script crossorigin src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- Firebase push notifications. --> <!-- Firebase push notifications. -->
<script crossorigin src="https://unpkg.com/firebase@5.5.0/firebase-app.js"></script> <script crossorigin src="https://unpkg.com/firebase@5.5.1/firebase-app.js"></script>
<script crossorigin src="https://unpkg.com/firebase@5.5.0/firebase-messaging.js"></script> <script crossorigin src="https://unpkg.com/firebase@5.5.1/firebase-messaging.js"></script>
</head> </head>
<body> <body>
@@ -47,6 +47,7 @@
<script src="src/drafty.js"></script> <script src="src/drafty.js"></script>
<script src="src/tinode.js"></script> <script src="src/tinode.js"></script>
<!-- the application --> <!-- the application -->
<script src="/firebase-init.js"></script>
<script type="text/babel" src="src/webapp.jsx"></script> <script type="text/babel" src="src/webapp.jsx"></script>
<script type="text/babel"> <script type="text/babel">
ReactDOM.render( ReactDOM.render(
+1
View File
@@ -26,6 +26,7 @@
</head> </head>
<body> <body>
<div id="mountPoint"></div> <div id="mountPoint"></div>
<script src="/firebase-init.js"></script>
<script src="umd/index.prod.js"></script> <script src="umd/index.prod.js"></script>
</body> </body>
</html> </html>
+1
View File
@@ -41,6 +41,7 @@
"index.min.js", "index.min.js",
"manifest.json", "manifest.json",
"service-worker.js", "service-worker.js",
"firebase-init.js",
"web-desktop-2.png", "web-desktop-2.png",
"web-mob-chat-1.png", "web-mob-chat-1.png",
"web-mob-contacts-1.png", "web-mob-contacts-1.png",
+5 -12
View File
@@ -1,16 +1,9 @@
// Must be located at the root. // Must be located at the root.
'use strict'; importScripts("https://unpkg.com/firebase@5.5.1/firebase-app.js");
importScripts("https://unpkg.com/firebase@5.5.1/firebase-messaging.js");
importScripts("/firebase-init.js")
importScripts("https://unpkg.com/firebase@5.5.0/firebase-app.js"); firebase.initializeApp(FIREBASE_INIT);
importScripts("https://unpkg.com/firebase@5.5.0/firebase-messaging.js");
const FIREBASE_PUBLIC_API_KEY = "AIzaSyD6X4ULR-RUsobvs1zZ2bHdJuPz39q2tbQ";
const FIREBASE_SENDER_ID = "114126160546";
firebase.initializeApp({
apiKey: FIREBASE_PUBLIC_API_KEY,
messagingSenderId: FIREBASE_SENDER_ID
});
firebase.messaging().setBackgroundMessageHandler(function(payload) { firebase.messaging().setBackgroundMessageHandler(function(payload) {
let title = payload.notification.title || "New message"; let title = payload.notification.title || "New message";
@@ -19,6 +12,6 @@ firebase.messaging().setBackgroundMessageHandler(function(payload) {
icon: '/img/logo96.png' icon: '/img/logo96.png'
}; };
console.log("background message", payload); console.log("background message", payload);
return self.registration.showNotification(title, options); return self.registration.showNotification(title, options);
}); });
+12 -16
View File
@@ -32,12 +32,6 @@ const POP_SOUND = new Audio('audio/msg.mp3');
// API key. Use https://github.com/tinode/chat/tree/master/keygen to generate your own // API key. Use https://github.com/tinode/chat/tree/master/keygen to generate your own
const API_KEY = "AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K"; const API_KEY = "AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K";
// Firebase is used for desktop alerts (push notifications).
// If you deploy your own server, you must change these FIREBASE_... values to your own.
const FIREBASE_VAPID_KEY = "BOgQVPOMzIMXUpsYGpbVkZoEBc0ifKY_f2kSU5DNDGYI6i6CoKqqxDd7w7PJ3FaGRBgVGJffldETumOx831jl58";
const FIREBASE_PUBLIC_API_KEY = "AIzaSyD6X4ULR-RUsobvs1zZ2bHdJuPz39q2tbQ";
const FIREBASE_SENDER_ID = "114126160546";
// Minimum time between two keypress notifications, milliseconds. // Minimum time between two keypress notifications, milliseconds.
const KEYPRESS_DELAY = 3*1000; const KEYPRESS_DELAY = 3*1000;
// Delay before sending a {note} for reciving a message, milliseconds. // Delay before sending a {note} for reciving a message, milliseconds.
@@ -4360,7 +4354,8 @@ class TinodeWeb extends React.Component {
messageSounds: !settings.messageSoundsOff, messageSounds: !settings.messageSoundsOff,
desktopAlerts: settings.desktopAlerts, desktopAlerts: settings.desktopAlerts,
desktopAlertsEnabled: (isSecureConnection() || isLocalHost()) && desktopAlertsEnabled: (isSecureConnection() || isLocalHost()) &&
(typeof firebase != 'undefined') && (typeof navigator != 'undefined'), (typeof firebase != 'undefined') && (typeof navigator != 'undefined') &&
(typeof FIREBASE_INIT != 'undefined'),
firebaseToken: localStorage.getObject("firebase-token"), firebaseToken: localStorage.getObject("firebase-token"),
errorText: '', errorText: '',
@@ -4416,11 +4411,8 @@ class TinodeWeb extends React.Component {
// Initialize desktop alerts. // Initialize desktop alerts.
if (this.state.desktopAlertsEnabled) { if (this.state.desktopAlertsEnabled) {
try { try {
this.fbPush = firebase.initializeApp({ this.fbPush = firebase.initializeApp(FIREBASE_INIT, APP_NAME).messaging();
apiKey: FIREBASE_PUBLIC_API_KEY, this.fbPush.usePublicVapidKey(FIREBASE_INIT.messagingVapidKey);
messagingSenderId: FIREBASE_SENDER_ID,
}, APP_NAME).messaging();
this.fbPush.usePublicVapidKey(FIREBASE_VAPID_KEY);
navigator.serviceWorker.register('/service-worker.js').then((reg) => { navigator.serviceWorker.register('/service-worker.js').then((reg) => {
this.fbPush.useServiceWorker(reg); this.fbPush.useServiceWorker(reg);
this.initDesktopAlerts(); this.initDesktopAlerts();
@@ -4988,6 +4980,7 @@ class TinodeWeb extends React.Component {
if (enabled) { if (enabled) {
if (!this.state.firebaseToken) { if (!this.state.firebaseToken) {
this.fbPush.requestPermission().then(() => { this.fbPush.requestPermission().then(() => {
console.log("got permission, requesting token");
this.requestPushToken(true); this.requestPushToken(true);
}).catch((err) => { }).catch((err) => {
this.handleError(err.message, "err"); this.handleError(err.message, "err");
@@ -5000,12 +4993,12 @@ class TinodeWeb extends React.Component {
localStorage.updateObject("settings", {desktopAlerts: true}); localStorage.updateObject("settings", {desktopAlerts: true});
} }
} else if (this.state.firebaseToken) { } else if (this.state.firebaseToken) {
this.fbPush.deleteToken(this.state.firebaseToken).then(() => { this.fbPush.deleteToken(this.state.firebaseToken).catch((err) => {
console.log("Unable to delete token.", err);
}).finally(() => {
localStorage.updateObject("settings", {desktopAlerts: false}); localStorage.updateObject("settings", {desktopAlerts: false});
localStorage.removeItem("firebase-token"); localStorage.removeItem("firebase-token");
this.setState({desktopAlerts: false, firebaseToken: null}); this.setState({desktopAlerts: false, firebaseToken: null});
}).catch((err) => {
console.log("Unable to delete token.", err);
}); });
} else { } else {
this.setState({desktopAlerts: false, firebaseToken: null}); this.setState({desktopAlerts: false, firebaseToken: null});
@@ -5014,7 +5007,9 @@ class TinodeWeb extends React.Component {
} }
requestPushToken(sendToServer) { requestPushToken(sendToServer) {
console.log("requestPushToken", sendToServer);
this.fbPush.getToken().then((refreshedToken) => { this.fbPush.getToken().then((refreshedToken) => {
console.log("got token", refreshedToken, this.state.firebaseToken);
if (refreshedToken != this.state.firebaseToken) { if (refreshedToken != this.state.firebaseToken) {
this.tinode.setDeviceToken(refreshedToken, sendToServer); this.tinode.setDeviceToken(refreshedToken, sendToServer);
localStorage.setObject("firebase-token", refreshedToken); localStorage.setObject("firebase-token", refreshedToken);
@@ -5123,10 +5118,11 @@ class TinodeWeb extends React.Component {
handleLogout() { handleLogout() {
localStorage.removeItem("auth-token"); localStorage.removeItem("auth-token");
this.setState(this.getBlankState());
if (this.tinode) { if (this.tinode) {
this.tinode.onDisconnect = undefined;
this.tinode.disconnect(); this.tinode.disconnect();
} }
this.setState(this.getBlankState());
this.tinode = TinodeWeb.tnSetup(this.state.serverAddress, this.state.transport); this.tinode = TinodeWeb.tnSetup(this.state.serverAddress, this.state.transport);
navigateTo(""); navigateTo("");
} }