浏览代码

Globally catch exceptions

Pitu 6 年之前
父节点
当前提交
ab0839f1f5
共有 2 个文件被更改,包括 44 次插入63 次删除
  1. 12 23
      src/site/plugins/axios.js
  2. 32 40
      src/site/store/index.js

+ 12 - 23
src/site/plugins/axios.js

@@ -1,29 +1,18 @@
-/*
-import Vue from 'vue';
-import axios from 'axios';
-import VueAxios from 'vue-axios';
-
-Vue.use(VueAxios, axios);
-Vue.axios.defaults.headers.common.Accept = 'application/vnd.lolisafe.json';
-*/
-
-export default function({ $axios, redirect }) {
+export default function({ $axios, store }) {
+	$axios.setHeader('accept', 'application/vnd.lolisafe.json');
 	$axios.onRequest(config => {
-		console.log(`Making request to > /${config.url}`);
+		if (store.state.token) {
+			config.headers.common['Authorization'] = `bearer ${store.state.token}`;
+		}
 	});
-	$axios.setHeader('accept', 'application/vnd.lolisafe.json');
 
 	$axios.onError(error => {
-		// console.log('=====');
-		console.log(error);
-		// console.log('=====');
-		/*
-		$toast.open({
-			duration: 2500,
-			message: 'testing',
-			position: 'is-bottom',
-			type: error ? 'is-danger' : 'is-success'
-		});
-		*/
+		if (process.env.development) console.error('[AXIOS Error]', error);
+		if (process.browser) {
+			store.dispatch('alert', {
+				text: error.response.data.message,
+				error: true
+			});
+		}
 	});
 }

+ 32 - 40
src/site/store/index.js

@@ -1,15 +1,9 @@
-/*
-import Vue from 'vue';
-import axios from 'axios';
-*/
-
-const cookieparser = process.server ? require('cookieparser') : null;
-
 export const state = () => ({
 	loggedIn: false,
 	user: null,
 	token: null,
-	config: null
+	config: null,
+	alert: null
 });
 
 /* eslint-disable no-shadow */
@@ -18,26 +12,21 @@ export const mutations = {
 		state.loggedIn = payload;
 	},
 	user(state, payload) {
-		if (!payload) {
-			state.user = null;
-			return;
-		}
 		state.user = payload;
 	},
 	token(state, payload) {
-		if (!payload) {
-			state.token = null;
-			return;
-		}
 		state.token = payload;
 	},
 	config(state, payload) {
 		state.config = payload;
+	},
+	alert(state, payload) {
+		state.alert = payload;
 	}
 };
 
 export const actions = {
-	async nuxtServerInit({ commit }, { app, req }) {
+	async nuxtServerInit({ commit, dispatch }, { app, req }) {
 		commit('config', {
 			version: process.env.npm_package_version,
 			URL: process.env.DOMAIN,
@@ -50,34 +39,37 @@ export const actions = {
 			enableAccounts: process.env.USER_ACCOUNTS == 'true' ? true : false
 		});
 
-		let token = null;
-		if (req.headers.cookie) {
-			try {
-				token = cookieparser.parse(req.headers.cookie).token;
-				console.log(token);
-				commit('loggedIn', true);
-				commit('token', token);
-
-				app.$axios.setToken(token, 'Bearer');
+		const cookies = this.$cookies.getAll();
+		if (!cookies.token) return dispatch('logout');
 
-				const data = await this.$axios.$get(`verify`);
-				if (!data || !data.user);
-				commit('user', data.user);
-			} catch (error) {
-				// TODO: Deactivate this on production
-				console.error(error);
-			}
-		}
-		commit('token', token);
-		if (!token) {
-			app.$axios.setToken('');
-			commit('user', null);
-			commit('loggedIn', false);
+		commit('token', cookies.token);
+		try {
+			const response = await this.$axios.$get('verify');
+			dispatch('login', {
+				token: cookies.token,
+				user: response.user
+			});
+		} catch (error) {
+			dispatch('logout');
 		}
 	},
-	login({ commit }, { app, token, user }) {
+	login({ commit }, { token, user }) {
+		this.$cookies.set('token', token);
 		commit('token', token);
 		commit('user', user);
 		commit('loggedIn', true);
+	},
+	logout({ commit }) {
+		this.$cookies.remove('token');
+		commit('token', null);
+		commit('user', null);
+		commit('loggedIn', false);
+	},
+	alert({ commit }, payload) {
+		if (!payload) return commit('alert', null);
+		commit('alert', {
+			text: payload.text,
+			error: payload.error
+		});
 	}
 };