Skip to content

Commit

Permalink
Add more resilient and fully check DB connexion
Browse files Browse the repository at this point in the history
  • Loading branch information
sapk committed Jul 12, 2016
1 parent 9d128f0 commit 777bc77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 6 additions & 3 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 @@ -59,11 +60,11 @@ S.db.users = {
login : function(user,pass,silent){
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);
}
return Promise.reject(err);
return err;
}else{
if(response.ok) {
//We are logged in
Expand All @@ -73,7 +74,7 @@ S.db.users = {
return response;
});
}else{
return Promise.reject(response);
return err;
}
}
});
Expand Down Expand Up @@ -199,8 +200,10 @@ S.db.fiches = {
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
Expand Down
17 changes: 13 additions & 4 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).catch(function(err){
console.log(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 777bc77

Please sign in to comment.