mirror of
https://github.com/dborth/vbagx.git
synced 2024-12-29 12:11:51 +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 axios from 'axios';
|
||||||
|
import * as localForage from "localforage";
|
||||||
|
|
||||||
const http = axios.create({
|
const http = axios.create({
|
||||||
baseURL: `${url}`,
|
baseURL: `${url}`,
|
||||||
});
|
});
|
||||||
|
|
||||||
const token = localStorage.getItem('token')
|
localForage.config(dbConfig);
|
||||||
|
|
||||||
if (token) {
|
async function checkToken (){
|
||||||
http.defaults.headers.common['Authorization'] = token;
|
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 routes from './routes.js';
|
||||||
|
|
||||||
// Import htpp (axios) instance
|
// Import htpp (axios) instance
|
||||||
import http from './api/config.js';
|
import { http } from './api/config.js';
|
||||||
|
|
||||||
// Import main app component
|
// Import main app component
|
||||||
import App from '../app.f7.html';
|
import App from '../app.f7.html';
|
||||||
@ -68,6 +68,9 @@ var app = new Framework7({
|
|||||||
cordovaApp.init(f7);
|
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
|
//Intercept axios call to determine if it gets (401 Unauthorized) response
|
||||||
http.interceptors.response.use(undefined, function (err) {
|
http.interceptors.response.use(undefined, function (err) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { createStore } from 'framework7';
|
import { createStore } from 'framework7';
|
||||||
import $ from 'dom7';
|
import $ from 'dom7';
|
||||||
import * as localForage from "localforage";
|
import { localForage } from "./../api/config.js";
|
||||||
|
|
||||||
const theme = createStore({
|
const theme = createStore({
|
||||||
state: {
|
state: {
|
||||||
@ -18,8 +18,8 @@ const theme = createStore({
|
|||||||
dispatch('changeTheme', theme);
|
dispatch('changeTheme', theme);
|
||||||
},
|
},
|
||||||
async initTheme({state, dispatch}, selector){
|
async initTheme({state, dispatch}, selector){
|
||||||
let items = await localForage.length();
|
let item = await localForage.getItem('theme');
|
||||||
if (items > 0){
|
if (item != null){
|
||||||
await dispatch('checkTheme', selector);
|
await dispatch('checkTheme', selector);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -1,46 +1,67 @@
|
|||||||
import { createStore } from 'framework7';
|
import { createStore } from 'framework7';
|
||||||
import http from './../api/config.js';
|
import { http, localForage } from './../api/config.js';
|
||||||
|
|
||||||
const user = createStore({
|
const user = createStore({
|
||||||
state: {
|
state: {
|
||||||
status: localStorage.getItem('token') ? 'success' : '',
|
status: '',
|
||||||
token: localStorage.getItem('token') || '',
|
token: '',
|
||||||
user: {},
|
user: {},
|
||||||
roles: [],
|
roles: [],
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
logIn({ state, dispatch }, data){
|
async setBasicData({ dispatch }, { token, user, roles }){
|
||||||
return new Promise((resolve, reject) => {
|
await localForage.setItem('user', user);
|
||||||
dispatch('auth_request');
|
await localForage.setItem('token', token);
|
||||||
http.post('/user/login', data).then(response => {
|
await localForage.setItem('roles', roles);
|
||||||
const token = response.data.token;
|
dispatch('auth_success', { token, user, roles });
|
||||||
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 => {
|
|
||||||
dispatch('auth_error');
|
|
||||||
localStorage.removeItem('token');
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
logOut({ state, dispatch }){
|
async getBasicData({ dispatch }){
|
||||||
return new Promise((resolve, reject) => {
|
const result = {
|
||||||
dispatch('logout');
|
user: await localForage.getItem('user'),
|
||||||
localStorage.removeItem('token');
|
token: await localForage.getItem('token'),
|
||||||
delete http.defaults.headers.common['Authorization'];
|
roles: await localForage.getItem('roles'),
|
||||||
resolve();
|
}
|
||||||
});
|
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');
|
||||||
|
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;
|
||||||
|
http.defaults.headers.common['Authorization'] = token;
|
||||||
|
await dispatch('setBasicData', { token, user, roles });
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
dispatch('auth_error');
|
||||||
|
await clearBasicData();
|
||||||
|
throw new error;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async logOut({ state, dispatch }){
|
||||||
|
dispatch('logout');
|
||||||
|
await dispatch('clearBasicData');
|
||||||
|
delete http.defaults.headers.common['Authorization'];
|
||||||
},
|
},
|
||||||
//Mutations
|
//Mutations
|
||||||
logout({ state }){
|
logout({ state }){
|
||||||
state.status = '';
|
state.status = '';
|
||||||
state.token = '';
|
state.token = '';
|
||||||
|
state.user = {};
|
||||||
|
state.roles = [];
|
||||||
},
|
},
|
||||||
auth_request({ state }){
|
auth_request({ state }){
|
||||||
state.status = 'loading';
|
state.status = 'loading';
|
||||||
|
Loading…
Reference in New Issue
Block a user