Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Mar 24, 2021
1 parent 3694092 commit 563d722
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
radata
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const IRIS_PRIVATE_KEY = JSON.parse(process.env.IRIS_PRIVATE_KEY || 'null');
const PEERS = JSON.parse(process.env.PEERS || 'null') || 'https://iris.cx/gun';
const PEERS = process.env.PEERS && process.env.PEERS.split(',');
const INCOMING_HTTP_PORT = 3000;

// process.env.GOOGLE_APPLICATION_CREDENTIALS should be the file path of a JSON containing the service account key
Expand All @@ -11,14 +11,18 @@ var firebaseApp = admin.initializeApp({
});

const Gun = require('gun');
require('gun/sea');
const SEA = require('gun/sea');

const express = require('express');
const app = express();
const publicState = new Gun(PEERS);
publicState.user().auth(IRIS_PRIVATE_KEY);
const publicState = new Gun({peers:PEERS});
const user = publicState.user();
user.auth(IRIS_PRIVATE_KEY);

app.get('/auth', async (req, res) => {
if (!(IRIS_PRIVATE_KEY && PEERS)) {
return res.status(400).send({message: 'bad server config'});
}
const idToken = req.query.token;
const pub = req.query.pub;
if (idToken && pub) {
Expand All @@ -27,12 +31,13 @@ app.get('/auth', async (req, res) => {
.verifyIdToken(idToken)
.then((decodedToken) => {
const uid = decodedToken.uid;
publicState.user().get('uidToPub').get(uid).put(pub);
publicState.user().get('follow').get(pub).put(true);
user.get('uidToPub').get(uid).put(pub);
user.get('follow').get(pub).put(true);
console.log('linked user', pub.slice(0,6), '... to uid', uid.slice(0,6), '...');
res.send('');
})
.catch((error) => {
res.status(400).send({message: 'infernal server error'});
console.error('firebase authentication failure: ', error);
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"express": "^4.17.1",
"firebase-admin": "^9.5.0",
"gun": "^0.2020.520"
"gun": "amark/gun"
},
"devDependencies": {
"eslint": "^7.22.0",
Expand Down
25 changes: 14 additions & 11 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ <h1>Iris sms auth</h1>
const errorContainer = document.getElementById('error');
const phoneNumberForm = document.getElementById('phone-number-form');
const verificationCodeForm = document.getElementById('verification-code-form');

function displayError(error) {
console.error(error);
errorContainer.style = 'display:block;color:red';
errorContainer.innerHTML = error && error.message;
}

phoneNumberForm.onsubmit = function(event) {
event.preventDefault();
const appVerifier = window.recaptchaVerifier;
Expand All @@ -112,9 +119,7 @@ <h1>Iris sms auth</h1>
console.log(confirmationResult);
// ...
}).catch((error) => {
console.error(error);
errorContainer.style = 'display:block';
errorContainer.innerHTML = error && error.message;
displayError(error);
appVerifier.render().then(function(widgetId) {
grecaptcha.reset(widgetId);
});
Expand All @@ -127,17 +132,15 @@ <h1>Iris sms auth</h1>
event.preventDefault();
const code = document.getElementById('verification-code').value;
window.confirmationResult.confirm(code).then(result => {
console.log('verification successful', result);
console.log('verification successful, registering public key', result);
verificationCodeForm.style = 'display: none';
errorContainer.style = 'display: none';
document.getElementById('success').style = '';
const pub = 'hyECQHwSo7fgr2MVfPyakvayPeixxsaAWVtZ-vbaiSc.TXIp8MnCtrnW6n2MrYquWPcc-DTmZzMBmc2yaGv9gIU';
fetch(`/auth?token=asdf&pub=${pub}`);
}).catch(error => {
console.error(error);
errorContainer.style = 'display:block;color:red';
errorContainer.innerHTML = error && error.message;
});
return firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
fetch(`/auth?token=${idToken}&pub=${pub}`);
document.getElementById('success').style = '';
});
}).catch(displayError);
}
</script>
</body>
Expand Down
5 changes: 2 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1309,10 +1309,9 @@ gtoken@^5.0.4:
google-p12-pem "^3.0.3"
jws "^4.0.0"

gun@^0.2020.520:
gun@amark/gun:
version "0.2020.520"
resolved "https://registry.yarnpkg.com/gun/-/gun-0.2020.520.tgz#3bf7c9c0c83ba04aee94c0157c4d4e0a64e865ad"
integrity sha512-hMDmj4QQnejweZEy4njbyNlll+GO7H8XIq/SdQ23kFV29B6HqA0+bQmhLCxWKLwQvfRXx9SgSXG/ImGrER/CqQ==
resolved "https://codeload.github.com/amark/gun/tar.gz/4dc672eeb4a9a45f31d9cd71536525ba4a02fc64"
dependencies:
ws "^7.2.1"
optionalDependencies:
Expand Down

0 comments on commit 563d722

Please sign in to comment.