Skip to content

Commit

Permalink
Merge pull request #100 from adentes-org/develop
Browse files Browse the repository at this point in the history
Detect token in DB to reset localdb with same name
  • Loading branch information
sapk authored Jul 12, 2016
2 parents 24bd684 + 777bc77 commit b7e7b0e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
4 changes: 2 additions & 2 deletions www/assets/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ S.init = function(cordova){

requirejs(['pouchdb-authentication', 'app/sofia.tool', 'app/sofia.template', 'app/sofia.config'], function () {
$("head").append('<link rel="stylesheet" type="text/css" href="dist/platform/' + cordova.platformId + '/css/style.css">');
requirejs(['platform/' + cordova.platformId + '/init', 'app/sofia.db'], function () {
requirejs(['app/sofia.vue', 'app/sofia.data', 'app/sofia.user', 'app/sofia.app'], function () {
requirejs(['platform/' + cordova.platformId + '/init', 'app/sofia.db', 'app/sofia.user'], function () {
requirejs(['app/sofia.vue', 'app/sofia.data', 'app/sofia.app'], function () {
window.setTimeout(S.app.initialize,250); // Add time if all not already loaded for safety
});
});
Expand Down
60 changes: 43 additions & 17 deletions www/assets/js/sofia.db.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ S.db = {
S.db.remoteDB = new PouchDB(S.config.db._full_url, {skipSetup: true});
},
clearLocal : function(){
console.log("Reset localDB !");
return S.db.localDB.destroy();
}
};
Expand Down Expand Up @@ -57,26 +58,26 @@ S.db.config = {
};
S.db.users = {
login : function(user,pass,silent){
//TODO don't use jquery promise
var deferred = new $.Deferred();
S.db.remoteDB.login(user, pass, function (err, response) {
return S.db.remoteDB.login(user, pass, function (err, response) {
if (err) {
console.log(err);
console.log(!silent,err.status,err.message,err);
if(!silent){
alert(err.message);
}
deferred.reject(err);
return err;
}else{
if(response.ok) {
//We are logged in
console.log("We are logged in !", response);
S.user.set(user,pass,response);
S.db.fiches.startSync();
deferred.resolve(response);
return S.db.fiches.startSync().then(function(){
return response;
});
}else{
return err;
}
}
});
return deferred.promise();
}
};

Expand Down Expand Up @@ -183,19 +184,44 @@ S.db.fiches = {
}).on('complete', function (info) {
console.log("Pouchdb.sync.complete event",info,Date());
// replication was canceled!
S.db.fiches.watch(S.db.localDB.sync(S.db.remoteDB, {
live: true,
retry: true
})); //Reload Sync
S.db.fiches.sync(); //Reload Sync
});
},
sync : function() {
console.log("Sync in place !");
return S.db.fiches.watch(S.db.localDB.sync(S.db.remoteDB, {
live: true,
retry: true
}));
},
startSync : function() {
console.log("Starting sync ...");
S.db.fiches.watch(S.db.localDB.sync(S.db.remoteDB, {
live: true,
retry: true
}));
console.log("Sync in place !");
//Check if DB as change
return S.db.remoteDB.get("_design/sofia-config").then(function(remote){
return S.db.localDB.get('_design/sofia-config').then(function(local){
if(remote.token === local.token){
console.log("Same DB Token detected !",remote, local);
S.db.fiches.sync(); //Same Db base everything is ok
}else{
console.log("Diff DB Token detected !",remote, local);
S.db.clearLocal().then(function () { //Clear local DB
S.db.localDB = new PouchDB(S.config.db._local_url); //Restart local DB
S.db.fiches.sync(); //Same Db base everything is ok
}).catch(function (err) {
console.log(err);
});
}
});
}).catch(function (err) {
console.log("Error detected in string sycn maybe Db are diff");
console.log(err); //in case of error we reset local DB
return S.db.clearLocal().then(function () { //Clear local DB
S.db.localDB = new PouchDB(S.config.db._local_url); //Restart local DB
S.db.fiches.sync(); //Same Db base everything is ok
}).catch(function (err) {
console.log(err);
});
});
},
post : function(obj) { //Create
return S.db.localDB.post(obj);
Expand Down
19 changes: 14 additions & 5 deletions www/assets/js/sofia.vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,15 @@ S.vue = {
},
beforeEach: function (transition) {
if (transition.to.path !== '/_login' && !S.user._current.isLogged()) {
if(S.config.user.username !== "" && S.config.user.userpass !== "") {
if(S.config.user.username !== "" && S.config.user.userpass !== "" && S.config.db.url !== "" && S.config.db.name !== "") {
//We have something to try !
S.user.login(S.config.user.username, S.config.user.userpass, true).fail(function(err){
console.log(err);
S.user.login(S.config.user.username, S.config.user.userpass, true).catch(function(err){
console.log(err.status,err);
switch (err.status) {
case 400: //this plugin only works for the http/https adapter (url malformeted on undefined)
delete localStorage['sofia-server-config'];
transition.redirect("/_login");
break;
case 401: //Wrong cred
S.user.reset(); //We clear cache if their are bad
transition.redirect("/_login");
Expand All @@ -128,10 +132,15 @@ S.vue = {
}).then(function(user){
//We are logged
console.log("Receiving the user : ",user);
S.vue.router.go("/");
if(typeof user != "undefined"){
S.vue.router.go("/");
}else{
transition.redirect("/_login"); //TODO backup url coming to redirect after
}
});
}else{
transition.redirect("/_login"); //TODO backup url coming to redirect after
}
transition.redirect("/_login"); //TODO backup url coming to redirect after
} else if (transition.to.path === '/_login' && S.user._current.isLogged()) {
//Case where we go back in history (we are already logged at the front door) so we abort
transition.abort();
Expand Down

0 comments on commit b7e7b0e

Please sign in to comment.