aboutsummaryrefslogtreecommitdiff
path: root/src/site/store/index.js
blob: a681fbe0bb26c0e1d4681c623a593896f3715c9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Vue from 'vue';
import Vuex from 'vuex';

const state = {
	loggedIn: false,
	user: {},
	token: null,
	config: null
};

/* eslint-disable no-shadow */
const mutations = {
	loggedIn(state, payload) {
		state.loggedIn = payload;
	},
	user(state, payload) {
		if (!payload) {
			state.user = {};
			localStorage.removeItem('lolisafe-user');
			return;
		}
		localStorage.setItem('lolisafe-user', JSON.stringify(payload));
		state.user = payload;
	},
	token(state, payload) {
		if (!payload) {
			localStorage.removeItem('lolisafe-token');
			state.token = null;
			return;
		}
		localStorage.setItem('lolisafe-token', payload);
		setAuthorizationHeader(payload);
		state.token = payload;
	},
	config(state, payload) {
		state.config = payload;
	}
};

const actions = {
	nuxtServerInit({ commit }, { req }) {
		const config = require('~/config.js');
		commit('config', config);
	}
};

const setAuthorizationHeader = payload => {
	console.log('hihi');
	Vue.axios.defaults.headers.common.Authorization = payload ? `Bearer ${payload}` : '';
};

const store = () => new Vuex.Store({
	state,
	mutations,
	actions
});

export default store;