Sfoglia il codice sorgente

Add admin field to vuex and set this field when a user logs in

sbkwgh 8 anni fa
parent
commit
b748912177
3 ha cambiato i file con 26 aggiunte e 6 eliminazioni
  1. 3 0
      src/App.vue
  2. 19 6
      src/main.js
  3. 4 0
      src/store/index.js

+ 3 - 0
src/App.vue

@@ -205,6 +205,7 @@
 				).then(res => {
 					this.loadingLogout = false
 					this.$store.commit('setUsername', '')
+					this.$store.commit('setAdmin', res.data.admin)
 
 					this.$router.push('/')
 				}).catch(err => {
@@ -251,6 +252,7 @@
 					}).then(res => {
 						this.signup.loading = false
 						this.$store.commit('setUsername', res.data.username)
+						this.$store.commit('setAdmin', res.data.admin)
 						this.closeAccountModal()
 
 						socket.emit('login')
@@ -282,6 +284,7 @@
 				}).then(res => {
 					this.login.loading = false
 					this.$store.commit('setUsername', res.data.username)
+					this.$store.commit('setAdmin', res.data.admin)
 					this.closeAccountModal()
 
 					socket.emit('login')

+ 19 - 6
src/main.js

@@ -129,10 +129,23 @@ let Root = new Vue({
 	router
 })
 
-let usernameCookie = document.cookie
+let cookieDict = document.cookie
 	.split(';')
-	.map(c => c.split('='))
-	.filter(pair => pair[0].trim() === 'username')
-	.map(pair => pair[1])[0]
-
-if(usernameCookie) Root.$store.commit('setUsername', usernameCookie)
+	.map(a => a.split('=').map(a => a.trim()) )
+	.map(a => {
+		let k = a[0], v = a[1]
+		return { [k] : v }
+	})
+	.reduce((combinedObj, o) => {
+		let key = Object.keys(o)[0]
+		combinedObj[key] = o[key]
+
+		return combinedObj
+	}, {})
+
+if(cookieDict.username) Root.$store.commit('setUsername', cookieDict.username)
+if(cookieDict.admin === 'false') {
+	Root.$store.commit('setAdmin', false)
+} else if(cookieDict.admin === 'true') {
+	Root.$store.commit('setAdmin', true)
+}

+ 4 - 0
src/store/index.js

@@ -18,6 +18,7 @@ export default new Vuex.Store({
 		accountTabs: 0,
 		accountModal: false,
 		username: '',
+		admin: false,
 
 		ajaxErrors: [],
 		ajaxErrorsModal: false
@@ -81,6 +82,9 @@ export default new Vuex.Store({
 		setUsername (state, value) {
 			state.username = value
 		},
+		setAdmin (state, value) {
+			state.admin = value
+		},
 		setForumName (state, value) {
 			state.meta.name = value
 		},