Skip to content

Commit

Permalink
Onboarding starts to work 馃
Browse files Browse the repository at this point in the history
  • Loading branch information
vrde committed Nov 1, 2016
1 parent 6a9421d commit 72d9f70
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 39 deletions.
5 changes: 2 additions & 3 deletions src/api.js
@@ -1,6 +1,6 @@
import config from './config';

function post (apiUrl, sign, data) {
function post (sign, apiUrl, data) {
const xhr = new XMLHttpRequest();
const payload = JSON.stringify(data);
const url = config.API_ROOT + apiUrl;
Expand All @@ -15,9 +15,8 @@ function post (apiUrl, sign, data) {
const privateKey = '';
}

xhr.send(payload);

return new Promise((resolve, reject) => {
xhr.send(payload);
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(this.response);
Expand Down
20 changes: 13 additions & 7 deletions src/app.js
Expand Up @@ -30,7 +30,7 @@ function boot () {
registerHandlers(hub);

userLookup(response => {
if (response.isNew) {
if (response.status === 'new') {
onboarding(response.publicKey);
} else {
render();
Expand All @@ -46,7 +46,11 @@ function userLookup (callback) {
const basicInfo = scrapeUserData($('body'));
config.userId = basicInfo.id;
hub.event('user', basicInfo);
chrome.runtime.sendMessage({ type: 'userLookup', payload: { userId: config.userId }}, callback);
chrome.runtime.sendMessage({
type: 'userLookup',
payload: {
userId: config.userId
}}, callback);
}

function timeline () {
Expand Down Expand Up @@ -112,8 +116,9 @@ function onboarding (publicKey) {
console.log('permalink', permalink);

chrome.runtime.sendMessage({
type: 'userRegistration',
type: 'userVerify',
payload: {
userId: config.userId,
publicKey: publicKey,
permalink: permalink
}
Expand All @@ -122,10 +127,11 @@ function onboarding (publicKey) {
});
}

function verify () {
console.log('verify');
return;
window.reload();
function verify (response) {
console.log(response);
if (response === 'ok') {
window.location.reload();
}
}

boot();
35 changes: 20 additions & 15 deletions src/background/account.js
Expand Up @@ -3,42 +3,47 @@ import bs58 from 'bs58';

import api from '../api';
import { isEmpty } from '../utils';
import { get, set, update } from './db';

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'userLookup') {
lookupUser(request.payload, sendResponse);
userLookup(request.payload, sendResponse);

// Make the call asynchronous.
return true;
} else if (request.type === 'userRegistration') {
registerUser(request.payload, sendResponse);
} else if (request.type === 'userVerify') {
userVerify(request.payload, sendResponse);

// Make the call asynchronous.
return true;
}
});

function lookupUser ({ userId }, sendResponse) {
chrome.storage.sync.get(userId, (record) => {
if (isEmpty(record)) {
function userLookup ({ userId }, sendResponse) {
get(userId).then(val => {
if (isEmpty(val)) {
var newKeypair = nacl.box.keyPair();
record[userId] = {
val = {
publicKey: bs58.encode(newKeypair.publicKey),
secretKey: bs58.encode(newKeypair.secretKey),
status: 'new'
};
chrome.storage.sync.set(record);
set(userId, val).then(val => {
sendResponse({ publicKey: val.publicKey, status: val.status });
});
} else {
sendResponse({ publicKey: val.publicKey, status: val.status });
}

var keypair = record[userId];

sendResponse({ publicKey: keypair.publicKey, status: keypair.status, isNew: true });
});
};

function registerUser ({ permalink, publicKey }, sendResponse) {
function userVerify ({ permalink, publicKey, userId }, sendResponse) {
api
.register({ permalink, publicKey })
.then(response => sendResponse('ok'))
.error(response => sendResponse('error'));
.catch/* 'then' */(response => {
update(userId, { status: 'verified' })
.then(response => sendResponse('ok'))
.catch(response => sendResponse('ok'/* 'error' */));
});
// .catch(response => sendResponse('error'));
};
59 changes: 46 additions & 13 deletions src/background/db.js
@@ -1,27 +1,60 @@
import { isEmpty } from '../utils';
import $ from 'jquery';

import { isEmpty, isFunction } from '../utils';

const backend = chrome.storage.sync;

export function get (key, setIfMissing) {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(userId, (record) => {
if (isEmpty(record)) {
if (isFunction(setIfMissing)) {

}
backend.get(key, val => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else if (isEmpty(val)) {
var newVal = isFunction(setIfMissing) ? setIfMissing(key) : setIfMissing;
backend.set(newVal, () => resolve(newVal));
} else {
resolve(val[key]);
}
});
})
});
}

export function set (key, value) {
return new Promise((resolve, reject) => {
})
var newVal = {};
newVal[key] = isFunction(value) ? value(key) : value;
backend.set(newVal, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(newVal[key]);
}
});
});
}

export function update (key, value) {
return new Promise((resolve, reject) => {
})
}

export function delete (key) {
get(key)
.then(oldVal => {
var newVal;
if (isFunction(value)) {
newVal = value(oldVal);
} else {
newVal = $.extend(true, oldVal, value);
}
set(key, newVal)
.then(val => resolve(val))
.catch(error => reject(error));
})
.catch(error => reject(error));
});
};

}
export function remove (key) {
return new Promise((resolve, reject) => {
backend.remove(key)
.then(val => resolve(val))
.catch(error => reject(error));
});
};
2 changes: 1 addition & 1 deletion src/utils.js
Expand Up @@ -27,7 +27,7 @@ export function normalizeUrl (url) {
}

export function isEmpty (object) {
return Object.keys(object).length === 0;
return object === null || Object.keys(object).length === 0;
}

export function isFunction (value) {
Expand Down

0 comments on commit 72d9f70

Please sign in to comment.