mirror of
https://github.com/dborth/vbagx.git
synced 2024-12-29 04:01:49 +01:00
-Refactor/change user authentication pt2
This commit is contained in:
parent
562df2b88b
commit
9cd5d36a5f
@ -1,11 +1,11 @@
|
|||||||
import User from './users.js';
|
//import User from './users.js';
|
||||||
//import user from './../stores/user.js';
|
import user from './../stores/user.js';
|
||||||
|
|
||||||
let Check = {
|
let Check = {
|
||||||
authorization( { to, resolve } ) {
|
authorization( { to, resolve } ) {
|
||||||
const router = this;
|
const router = this;
|
||||||
//if (user.isLogged.value){
|
if (user.getters.isLogged.value){
|
||||||
if (!User.isLogged()){
|
//if (!User.isLogged()){
|
||||||
router.navigate('/login/', { reloadCurrent: true });
|
router.navigate('/login/', { reloadCurrent: true });
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@ -14,9 +14,9 @@ let Check = {
|
|||||||
},
|
},
|
||||||
permission( { to, resolve, reject } ) {
|
permission( { to, resolve, reject } ) {
|
||||||
const router = this;
|
const router = this;
|
||||||
const userData = JSON.parse(localStorage.getItem("userData"));
|
//const userData = JSON.parse(localStorage.getItem("userData"));
|
||||||
const userRoles = userData['userPermissions'];
|
//const userRoles = userData['userPermissions'];
|
||||||
//const userRoles = user.roles.value;
|
const userRoles = user.getters.roles.value;
|
||||||
|
|
||||||
let currentIndex = -1;
|
let currentIndex = -1;
|
||||||
for (let i = 0; i < router.routes.length; i++) {
|
for (let i = 0; i < router.routes.length; i++) {
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
import LoginPage from '../../pages/login.f7.html';
|
import LoginPage from '../../pages/login.f7.html';
|
||||||
|
|
||||||
import User from '../controllers/users.js';
|
//import User from '../controllers/users.js';
|
||||||
|
import user from '../stores/user.js';
|
||||||
|
|
||||||
function checkAuth( { resolve } ) {
|
function checkAuth( { resolve } ) {
|
||||||
const router = this;
|
const router = this;
|
||||||
if (User.isLogged()){
|
if (user.getters.isLogged.value){
|
||||||
|
//if (User.isLogged()){
|
||||||
router.navigate('/', { reloadCurrent: true });
|
router.navigate('/', { reloadCurrent: true });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (router.currentRoute.name !== "login"){
|
if (router.currentRoute.name !== "login"){
|
||||||
resolve('/login/');
|
resolve('/login/');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import http from './../api/config.js';
|
|||||||
|
|
||||||
const user = createStore({
|
const user = createStore({
|
||||||
state: {
|
state: {
|
||||||
status: '',
|
status: localStorage.getItem('token') ? 'success' : '',
|
||||||
token: localStorage.getItem('token') || '',
|
token: localStorage.getItem('token') || '',
|
||||||
user: {},
|
user: {},
|
||||||
roles: [],
|
roles: [],
|
||||||
@ -12,11 +12,13 @@ const user = createStore({
|
|||||||
logIn({ state, dispatch }, data){
|
logIn({ state, dispatch }, data){
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
dispatch('auth_request');
|
dispatch('auth_request');
|
||||||
http.post('/user/login', data)
|
http.post('/user/login', data).then(response => {
|
||||||
.then(response => {
|
|
||||||
const token = response.data.token;
|
const token = response.data.token;
|
||||||
const user = response.data.user;
|
delete response.data.token;
|
||||||
const roles = response.data.roles;
|
const user = response.data;
|
||||||
|
const roles = [1, 4]; //Owner, Customer, Seller, Deliverer//response.data.roles;
|
||||||
|
console.log(roles);
|
||||||
|
console.log(user);
|
||||||
localStorage.setItem('token', token);
|
localStorage.setItem('token', token);
|
||||||
http.defaults.headers.common['Authorization'] = token;
|
http.defaults.headers.common['Authorization'] = token;
|
||||||
dispatch('auth_success', token, user, roles);
|
dispatch('auth_success', token, user, roles);
|
||||||
@ -29,9 +31,10 @@ const user = createStore({
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
logOut(){
|
logOut({ state, dispatch }){
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
dispatch('logout');
|
dispatch('logout');
|
||||||
|
localStorage.removeItem('token')
|
||||||
http.defaults.headers.common['Authorization'];
|
http.defaults.headers.common['Authorization'];
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
@ -41,16 +44,16 @@ const user = createStore({
|
|||||||
state.status = '';
|
state.status = '';
|
||||||
state.token = '';
|
state.token = '';
|
||||||
},
|
},
|
||||||
auth_request(state){
|
auth_request({ state }){
|
||||||
state.status = 'loading';
|
state.status = 'loading';
|
||||||
},
|
},
|
||||||
auth_success(state, token, user, roles){
|
auth_success({ state }, token, user, roles){
|
||||||
state.status = 'success';
|
state.status = 'success';
|
||||||
state.token = token;
|
state.token = token;
|
||||||
state.user = user;
|
state.user = user;
|
||||||
state.roles = roles;
|
state.roles = roles;
|
||||||
},
|
},
|
||||||
auth_error(state){
|
auth_error({ state }){
|
||||||
state.status = 'error'
|
state.status = 'error'
|
||||||
},
|
},
|
||||||
//End mutations
|
//End mutations
|
||||||
@ -59,12 +62,18 @@ const user = createStore({
|
|||||||
isLogged({ state }) {
|
isLogged({ state }) {
|
||||||
return !!state.token;
|
return !!state.token;
|
||||||
},
|
},
|
||||||
|
token({ state }) {
|
||||||
|
return state.token;
|
||||||
|
},
|
||||||
authStatus({ state }) {
|
authStatus({ state }) {
|
||||||
return state.status;
|
return state.status;
|
||||||
},
|
},
|
||||||
roles({ state }){
|
roles({ state }){
|
||||||
return state.roles;
|
return state.roles;
|
||||||
},
|
},
|
||||||
|
displayName({ state }){
|
||||||
|
return state.user;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -27,7 +27,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="accordion-item">
|
<li class="accordion-item">
|
||||||
<a href="#" class="item-content item-link">
|
<a href="#" class="item-content item-link">
|
||||||
<div class="item-inner">
|
<div class="item-inner">
|
||||||
@ -43,6 +42,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
${isSeller && $h`
|
||||||
<li class="accordion-item">
|
<li class="accordion-item">
|
||||||
<a href="#" class="item-content item-link">
|
<a href="#" class="item-content item-link">
|
||||||
<div class="item-inner">
|
<div class="item-inner">
|
||||||
@ -61,6 +61,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
`}
|
||||||
|
${isDeliverer && $h`
|
||||||
<li class="accordion-item">
|
<li class="accordion-item">
|
||||||
<a href="#" class="item-content item-link">
|
<a href="#" class="item-content item-link">
|
||||||
<div class="item-inner">
|
<div class="item-inner">
|
||||||
@ -78,7 +80,9 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
<li id="is-customer-${isCustomer}" class="accordion-item" data-some="${isCustomer}">
|
`}
|
||||||
|
${isCustomer && $h`
|
||||||
|
<li id="is-customer" class="accordion-item" data-some="${isCustomer}">
|
||||||
<a href="#" class="item-content item-link">
|
<a href="#" class="item-content item-link">
|
||||||
<div class="item-inner">
|
<div class="item-inner">
|
||||||
<div class="item-title">Clientes</div>
|
<div class="item-title">Clientes</div>
|
||||||
@ -97,6 +101,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
`}
|
||||||
<li class="accordion-item">
|
<li class="accordion-item">
|
||||||
<a href="#" class="item-content item-link">
|
<a href="#" class="item-content item-link">
|
||||||
<div class="item-inner">
|
<div class="item-inner">
|
||||||
@ -135,8 +140,9 @@ export default (props, { $f7, $on, $update, }) => {
|
|||||||
|
|
||||||
const onPanelOpen = () => {
|
const onPanelOpen = () => {
|
||||||
if (User.isLogged()){
|
if (User.isLogged()){
|
||||||
const userData = JSON.parse(localStorage.getItem("userData"));
|
//const userData = JSON.parse(localStorage.getItem("userData"));
|
||||||
const userRoles = userData['userPermissions'];
|
//const userRoles = userData['userPermissions'];
|
||||||
|
const userRoles = user.getters.roles.value;
|
||||||
|
|
||||||
isLogged = true,
|
isLogged = true,
|
||||||
isCustomer = userRoles.includes(4), //Customer,
|
isCustomer = userRoles.includes(4), //Customer,
|
||||||
|
@ -63,24 +63,36 @@
|
|||||||
<script>
|
<script>
|
||||||
import $ from 'dom7';
|
import $ from 'dom7';
|
||||||
import store from '../../js/stores/index.js';
|
import store from '../../js/stores/index.js';
|
||||||
import User from '../../js/controllers/users.js';
|
import user from '../../js/stores/user.js';
|
||||||
|
//import User from '../../js/controllers/users.js';
|
||||||
|
|
||||||
export default (props, { $f7, $on, $update }) => {
|
export default (props, { $f7, $on, $update, $f7router }) => {
|
||||||
let isLogged = false;
|
let isLogged = user.getters.isLogged.value;
|
||||||
let theme = store.getters.themeName.value;
|
let theme = store.getters.themeName.value;
|
||||||
|
|
||||||
const onPanelOpen = () => {
|
const onPanelOpen = () => {
|
||||||
$('#light-theme').prop('checked', (theme != 'theme-dark') ? true : false );
|
$('#light-theme').prop('checked', (theme != 'theme-dark') ? true : false );
|
||||||
if (User.isLogged()){
|
// if (User.isLogged()){
|
||||||
let data = JSON.parse(localStorage.getItem("userData"));
|
// let data = JSON.parse(localStorage.getItem("userData"));
|
||||||
$("#menu-username").text(data.contactName);
|
// $("#menu-username").text(data.contactName);
|
||||||
isLogged = true;
|
// isLogged = true;
|
||||||
$update();
|
// $update();
|
||||||
|
// }
|
||||||
|
//console.log(user.getters.displayName.value);
|
||||||
|
console.log(user.getters.isLogged.value);
|
||||||
|
console.log(user.getters.authStatus.value);
|
||||||
|
console.log(user.getters.token.value);
|
||||||
|
console.log(user.getters.roles.value);
|
||||||
|
if (isLogged){
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
User.logOff();
|
//User.logOff();
|
||||||
|
user.dispatch('logOut').then( () => {
|
||||||
|
$f7router.navigate('/');
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const setTheme = () => {
|
const setTheme = () => {
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
<div class="item-inner">
|
<div class="item-inner">
|
||||||
<div class="item-title item-label">Usuario</div>
|
<div class="item-title item-label">Usuario</div>
|
||||||
<div class="item-input-wrap">
|
<div class="item-input-wrap">
|
||||||
<input id="username" type="text" name="username" placeholder="Ingresa tu usuario o email" />
|
<input id="username" type="text" name="username" value="${username}" @input="${updateUsername}" placeholder="Ingresa tu usuario o email" autocomplete="off" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
@ -18,7 +18,7 @@
|
|||||||
<div class="item-inner">
|
<div class="item-inner">
|
||||||
<div class="item-title item-label">Contraseña</div>
|
<div class="item-title item-label">Contraseña</div>
|
||||||
<div class="item-input-wrap">
|
<div class="item-input-wrap">
|
||||||
<input id="password" type="password" name="password" placeholder="Ingresa tu contraseña" autocomplete="off" @keypress="${pressEnter}" />
|
<input id="password" type="password" name="password" alue="${password}" @input="${updatePassword}" placeholder="Ingresa tu contraseña" autocomplete="off" @keypress="${pressEnter}" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
@ -38,18 +38,37 @@
|
|||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import $ from 'dom7';
|
import $ from 'dom7';
|
||||||
import User from '../js/controllers/users.js';
|
//import User from '../js/controllers/users.js';
|
||||||
|
import user from '../js/stores/user.js';
|
||||||
|
|
||||||
export default (props, { $f7, $on, $update, $f7router }) => {
|
export default (props, { $f7, $on, $update, $f7router }) => {
|
||||||
let version = $f7.version;
|
let version = $f7.version;
|
||||||
|
let username = '';
|
||||||
|
let password = '';
|
||||||
|
|
||||||
|
const updateUsername = (e) => {
|
||||||
|
username = e.target.value;
|
||||||
|
$update();
|
||||||
|
};
|
||||||
|
|
||||||
|
const updatePassword = (e) => {
|
||||||
|
password = e.target.value;
|
||||||
|
$update();
|
||||||
|
}
|
||||||
|
|
||||||
const checkLogin = () => {
|
const checkLogin = () => {
|
||||||
$f7.preloader.show();
|
$f7.preloader.show();
|
||||||
var username = $('#username').val();
|
const data = {
|
||||||
var password = $('#password').val();
|
UserData : username,
|
||||||
User.checkLogin(username, password).then(function(result){
|
UserPassword : password,
|
||||||
|
};
|
||||||
|
// var username = $('#username').val();
|
||||||
|
// var password = $('#password').val();
|
||||||
|
// User.checkLogin(username, password).then(function(result){
|
||||||
|
user.dispatch('logIn', data)
|
||||||
|
.then(function(result){
|
||||||
$f7.preloader.hide();
|
$f7.preloader.hide();
|
||||||
if (result === true){
|
if (result.status === 200){
|
||||||
$f7router.navigate('/');
|
$f7router.navigate('/');
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
Loading…
Reference in New Issue
Block a user