-Remove old user login handling way

-Read/write theme use in pesistent way
This commit is contained in:
Juan Carlos Ruvalcaba 2021-04-24 16:50:04 -07:00
parent 7918379dff
commit a3bef880a0
4 changed files with 29 additions and 108 deletions

View File

@ -9,11 +9,20 @@
</template> </template>
<script> <script>
import themes from './js/stores/theme.js'; import themes from './js/stores/theme.js';
import * as localForage from "localforage";
export default (props, { $f7, $update }) => { export default (props, { $f7, $, $update, $onMounted }) => {
let theme = themes.getters.Name.value; let theme = themes.getters.Name.value;
$onMounted(() => {
localForage.getItem('theme').then( (value) => {
theme = value;
if (value != 'theme-dark'){ $("#app").removeClass("theme-dark"); }
themes.dispatch('changeTheme', theme);
});
});
return $render; return $render;
} }
</script> </script>

View File

@ -1,94 +0,0 @@
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 this.getLogin(user, pass);
if (result.data.status === false){
throw new Error (userdata.message);
}
let userData = result.data;
let params = { //fixed
url: "",
method : "GET",
contentType: "application/x-www-form-urlencoded",
crossDomain: true,
dataType: "json",
headers: { "Authorization" : userData.data.token }
};
localStorage.setItem("request_params", JSON.stringify(params));
delete userData.data.token;
userData.data["userPermissions"] = [1, 4, 14, 15]; //Owner, Customer, Seller, Deliverer
localStorage.setItem("userData",JSON.stringify(userData.data));
return true;
},
isLogged(){
let x = ["userData"];
let isLogged = true;
for (var i=0; i<x.length; i++){
if(localStorage.getItem(x[i]) === undefined || localStorage.getItem(x[i]) === null || localStorage.getItem(x[i]) === "0"){
isLogged = false;
break;
}
}
isLogged = this.checkLogOff();
return isLogged;
},
checkLogOff(){
let req = JSON.parse(localStorage.getItem("request_params"));
if (req === undefined || req === null) return false;
let data = this.decodeJWT(req.headers.Authorization);
var today = new Date(); //Current date
var expirationDate = new Date(data.expiration);
if (today.getTime() > expirationDate.getTime()){
return false;
}
else if (today < expirationDate){
}
else{
}
return true;
},
logOff (){
localStorage.removeItem("userData");
localStorage.removeItem("request_params");
location.reload();
},
decodeJWT (token) {
var base64Url = token.split('.')[1];
var base64 = decodeURIComponent(atob(base64Url).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(base64);
},
};
export default User;

View File

@ -126,11 +126,11 @@
</div> </div>
</template> </template>
<script> <script>
import $ from 'dom7'; import * as localForage from "localforage";
import themes from '../../js/stores/theme.js'; import themes from '../../js/stores/theme.js';
import user from '../../js/stores/user.js'; import user from '../../js/stores/user.js';
export default (props, { $f7, $on, $update, }) => { export default (props, { $f7, $, $onMounted, $update, }) => {
let isLogged = user.getters.isLogged.value; let isLogged = user.getters.isLogged.value;
let isSeller = false; let isSeller = false;
let isCustomer = false; let isCustomer = false;
@ -139,13 +139,20 @@ export default (props, { $f7, $on, $update, }) => {
const userRoles = user.getters.roles.value; const userRoles = user.getters.roles.value;
const onPanelOpen = () => { const onPanelOpen = () => {
};
$onMounted(() => {
localForage.getItem('theme').then( (value) => {
if (value != 'theme-dark'){ $(".panel-left").removeClass("theme-dark"); }
});
if (isLogged){ if (isLogged){
isCustomer = userRoles.includes(4); //Customer, isCustomer = userRoles.includes(4); //Customer,
isSeller = userRoles.includes(14); //Seller, isSeller = userRoles.includes(14); //Seller,
isDeliverer = userRoles.includes(15); //Deliverer, isDeliverer = userRoles.includes(15); //Deliverer,
$update(); $update();
} }
}; });
return $render; return $render;
} }

View File

@ -61,24 +61,25 @@
</div> </div>
</template> </template>
<script> <script>
import $ from 'dom7';
import * as localForage from "localforage"; import * as localForage from "localforage";
import themes from '../../js/stores/theme.js'; import themes from '../../js/stores/theme.js';
import user from '../../js/stores/user.js'; import user from '../../js/stores/user.js';
export default (props, { $f7, $on, $update, $f7router }) => { export default (props, { $f7, $, $on, $onMounted, $update, $f7router }) => {
let theme = themes.getters.Name.value; let theme = themes.getters.Name.value;
let isLogged = user.getters.isLogged.value; let isLogged = user.getters.isLogged.value;
let displayName = user.getters.displayName.value['contactName'] || ''; let displayName = user.getters.displayName.value['contactName'] || '';
const onPanelOpen = () => { const onPanelOpen = () => {
localForage.getItem('theme').then((value)=>{
//if (value != 'theme-dark'){$('#light-theme').click()}
//$('#light-theme').prop('checked', (value != 'theme-dark') ? true : false );
//$update();
});
}; };
$onMounted(() => {
$('#light-theme').prop('checked', (theme != 'theme-dark') ? true : false );
localForage.getItem('theme').then( (value) => {
if (value != 'theme-dark'){ $(".panel-right").removeClass("theme-dark"); }
});
});
const logout = () => { const logout = () => {
user.dispatch('logOut').then( () => { user.dispatch('logOut').then( () => {
$f7router.navigate('/'); $f7router.navigate('/');
@ -94,9 +95,7 @@ export default (props, { $f7, $on, $update, $f7router }) => {
$("#app, .panel-right").addClass("theme-dark"); $("#app, .panel-right").addClass("theme-dark");
} }
theme = !$("#light-theme").prop('checked') ? 'theme-dark' : ''; theme = !$("#light-theme").prop('checked') ? 'theme-dark' : '';
localForage.setItem('theme', theme).then((val)=>{ localForage.setItem('theme', theme).then( (val) => {
// console.log("... ", theme);
// console.log(",,, ",val);
themes.dispatch('changeTheme', theme); themes.dispatch('changeTheme', theme);
}); });
}; };