-Move ajax handler file and connection file
-Add axios interceptor, not tested
-Move login service to user handling logic
-Remove "user model" service file
This commit is contained in:
Juan Carlos Ruvalcaba 2021-04-21 13:56:26 -07:00
parent d4a9ed07a0
commit 562df2b88b
6 changed files with 36 additions and 31 deletions

2
.gitignore vendored
View File

@ -43,5 +43,5 @@ cordova/www/
www/
#Hide connection file
/src/js/models/ConnectionMode.js
/src/js/api/ConnectionMode.js
/src/js/api/key.js

View File

@ -13,6 +13,9 @@ import cordovaApp from './cordova-app.js';
// Import Routes
import routes from './routes.js';
// Import htpp (axios) instance
import http from './api/config.js';
// Import main app component
import App from '../app.f7.html';
@ -50,6 +53,16 @@ var app = new Framework7({
// Init cordova APIs (see cordova-app.js)
cordovaApp.init(f7);
}
//Intercept axios call to determine if it gets (401 Unauthorized) response
http.interceptors.response.use(undefined, function (err) {
return new Promise(function (resolve, reject) {
if (err.status === 401 && err.config && !err.config.__isRetryRequest) {
this.$store.dispatch(logout);
}
throw err;
});
});
},
},
});

View File

@ -1,11 +1,31 @@
import { Users } from '../models/Users.js'
import AjaxHandler from '../api/AjaxHandler.js';
import endpoint from '../api/ConnectionMode.js';
let User = {
async getLogin (userdata, password){
try{
let params = {
method : "POST",
url : `${endpoint}/user/login`,
data : {
UserData : userdata,
UserPassword : password,
},
dataType : "json",
}
var ajax = new AjaxHandler();
let result = await ajax.request(params);
return result;
}
catch(err){
throw err;
}
},
async checkLogin(user, pass){
if (user === "" || pass === ""){
throw new Error ("Ingrese usuario/contraseña");
}
let result = await Users.getLogin(user, pass);
let result = await this.getLogin(user, pass);
if (result.data.status === false){
throw new Error (userdata.message);
@ -48,7 +68,6 @@ let User = {
var expirationDate = new Date(data.expiration);
if (today.getTime() > expirationDate.getTime()){
//this.logOff();
return false;
}
else if (today < expirationDate){

View File

@ -1,26 +0,0 @@
import AjaxHandler from './AjaxHandler.js';
import endpoint from './ConnectionMode.js';
let Users = {
async getLogin (userdata, password){
try{
let params = {
method : "POST",
url : `${endpoint}/user/login`,
data : {
UserData : userdata,
UserPassword : password,
},
dataType : "json",
}
var ajax = new AjaxHandler();
let result = await ajax.request(params);
return result;
}
catch(err){
throw err;
}
},
};
export { Users };

View File

@ -32,7 +32,6 @@ const user = createStore({
logOut(){
return new Promise((resolve, reject) => {
dispatch('logout');
localStorage.removeItem('roles');
http.defaults.headers.common['Authorization'];
resolve();
});