mirror of
https://github.com/dborth/vbagx.git
synced 2024-12-29 04:01:49 +01:00
Updates:
- Change retirieve data for user to localForage instead of localStorage - Fix retrieve theme data on start. -Fix user logout function.
This commit is contained in:
parent
6971466c39
commit
65104aeb2e
@ -1,14 +1,20 @@
|
||||
import url from './key';
|
||||
import { url, dbConfig } from './key';
|
||||
import axios from 'axios';
|
||||
import * as localForage from "localforage";
|
||||
|
||||
const http = axios.create({
|
||||
baseURL: `${url}`,
|
||||
});
|
||||
|
||||
const token = localStorage.getItem('token')
|
||||
localForage.config(dbConfig);
|
||||
|
||||
if (token) {
|
||||
async function checkToken (){
|
||||
let token = await localForage.getItem('token');
|
||||
if (token != null){
|
||||
http.defaults.headers.common['Authorization'] = token;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default http;
|
||||
checkToken();
|
||||
|
||||
export { http, localForage };
|
@ -14,7 +14,7 @@ import cordovaApp from './cordova-app.js';
|
||||
import routes from './routes.js';
|
||||
|
||||
// Import htpp (axios) instance
|
||||
import http from './api/config.js';
|
||||
import { http } from './api/config.js';
|
||||
|
||||
// Import main app component
|
||||
import App from '../app.f7.html';
|
||||
@ -68,6 +68,9 @@ var app = new Framework7({
|
||||
cordovaApp.init(f7);
|
||||
}
|
||||
|
||||
// Retrieve data for user on app start
|
||||
user.dispatch('checkData').then(() =>{ });
|
||||
|
||||
//Intercept axios call to determine if it gets (401 Unauthorized) response
|
||||
http.interceptors.response.use(undefined, function (err) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { createStore } from 'framework7';
|
||||
import $ from 'dom7';
|
||||
import * as localForage from "localforage";
|
||||
import { localForage } from "./../api/config.js";
|
||||
|
||||
const theme = createStore({
|
||||
state: {
|
||||
@ -18,8 +18,8 @@ const theme = createStore({
|
||||
dispatch('changeTheme', theme);
|
||||
},
|
||||
async initTheme({state, dispatch}, selector){
|
||||
let items = await localForage.length();
|
||||
if (items > 0){
|
||||
let item = await localForage.getItem('theme');
|
||||
if (item != null){
|
||||
await dispatch('checkTheme', selector);
|
||||
}
|
||||
},
|
||||
|
@ -1,46 +1,67 @@
|
||||
import { createStore } from 'framework7';
|
||||
import http from './../api/config.js';
|
||||
import { http, localForage } from './../api/config.js';
|
||||
|
||||
const user = createStore({
|
||||
state: {
|
||||
status: localStorage.getItem('token') ? 'success' : '',
|
||||
token: localStorage.getItem('token') || '',
|
||||
status: '',
|
||||
token: '',
|
||||
user: {},
|
||||
roles: [],
|
||||
},
|
||||
actions: {
|
||||
logIn({ state, dispatch }, data){
|
||||
return new Promise((resolve, reject) => {
|
||||
async setBasicData({ dispatch }, { token, user, roles }){
|
||||
await localForage.setItem('user', user);
|
||||
await localForage.setItem('token', token);
|
||||
await localForage.setItem('roles', roles);
|
||||
dispatch('auth_success', { token, user, roles });
|
||||
},
|
||||
async getBasicData({ dispatch }){
|
||||
const result = {
|
||||
user: await localForage.getItem('user'),
|
||||
token: await localForage.getItem('token'),
|
||||
roles: await localForage.getItem('roles'),
|
||||
}
|
||||
dispatch('auth_success', result);
|
||||
},
|
||||
async clearBasicData(){
|
||||
await localForage.removeItem('user');
|
||||
await localForage.removeItem('token');
|
||||
await localForage.removeItem('roles');
|
||||
},
|
||||
async checkData({ dispatch }){
|
||||
let token = await localForage.getItem('token');
|
||||
if (token != null){
|
||||
await dispatch('getBasicData');
|
||||
}
|
||||
},
|
||||
async logIn({ state, dispatch }, data){
|
||||
dispatch('auth_request');
|
||||
http.post('/user/login', data).then(response => {
|
||||
try {
|
||||
const response = await http.post('/user/login', data);
|
||||
const token = response.data.token;
|
||||
delete response.data.token;
|
||||
const user = response.data.user;
|
||||
const roles = [1, 4, 14]; //Owner, Customer, Seller, Deliverer//response.data.roles;
|
||||
localStorage.setItem('token', token);
|
||||
http.defaults.headers.common['Authorization'] = token;
|
||||
dispatch('auth_success', {token, user, roles});
|
||||
resolve(response);
|
||||
})
|
||||
.catch(err => {
|
||||
await dispatch('setBasicData', { token, user, roles });
|
||||
return response;
|
||||
} catch (error) {
|
||||
dispatch('auth_error');
|
||||
localStorage.removeItem('token');
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
await clearBasicData();
|
||||
throw new error;
|
||||
}
|
||||
},
|
||||
logOut({ state, dispatch }){
|
||||
return new Promise((resolve, reject) => {
|
||||
async logOut({ state, dispatch }){
|
||||
dispatch('logout');
|
||||
localStorage.removeItem('token');
|
||||
await dispatch('clearBasicData');
|
||||
delete http.defaults.headers.common['Authorization'];
|
||||
resolve();
|
||||
});
|
||||
},
|
||||
//Mutations
|
||||
logout({ state }){
|
||||
state.status = '';
|
||||
state.token = '';
|
||||
state.user = {};
|
||||
state.roles = [];
|
||||
},
|
||||
auth_request({ state }){
|
||||
state.status = 'loading';
|
||||
|
Loading…
Reference in New Issue
Block a user