tested shared Vuex store, created d8 userblock, enabled JSONAPI module
This commit is contained in:
14
web/themes/custom/materiotheme/vuejs/api/json-axios.js
Normal file
14
web/themes/custom/materiotheme/vuejs/api/json-axios.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import axios from 'axios'
|
||||
|
||||
// https://github.com/alvar0hurtad0/drupal-vuejs-todo/blob/master/frontend/src/api/axiosInterceptor.js
|
||||
|
||||
// console.log('drupalSettings', drupalSettings);
|
||||
|
||||
export const JSONAPI = axios.create({
|
||||
baseURL: `http://dev.materio.com/jsonapi`,
|
||||
headers: {
|
||||
Accept: 'application/vnd.api+json'
|
||||
// Authorization: 'Bearer {token}',
|
||||
// "Content-Type": "application/json"
|
||||
}
|
||||
})
|
@@ -4,7 +4,7 @@ import axios from 'axios'
|
||||
|
||||
// console.log('drupalSettings', drupalSettings);
|
||||
|
||||
export const HTTP = axios.create({
|
||||
export const REST = axios.create({
|
||||
baseURL: `http://dev.materio.com`,
|
||||
headers: {
|
||||
Authorization: 'Bearer {token}',
|
@@ -0,0 +1,22 @@
|
||||
<template lang="html">
|
||||
<div id="block-pagetitle" class="">
|
||||
<h1>Test Shared Store ( logged in: {{ isloggedin }} )</h1>
|
||||
<h2 v-if="isloggedin">Shared store is working</h2>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { mapState, mapActions } from 'vuex'
|
||||
|
||||
export default {
|
||||
computed: {
|
||||
...mapState({
|
||||
token: state => state.User.token,
|
||||
isloggedin: state => state.User.isloggedin
|
||||
})
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
</style>
|
@@ -1,5 +1,5 @@
|
||||
<template lang="html">
|
||||
<UserTools v-if="token" />
|
||||
<UserTools v-if="isloggedin" />
|
||||
<Login v-else />
|
||||
</template>
|
||||
|
||||
@@ -15,7 +15,7 @@ export default {
|
||||
// },
|
||||
computed: {
|
||||
...mapState({
|
||||
token: state => state.User.token
|
||||
isloggedin: state => state.User.isloggedin
|
||||
})
|
||||
},
|
||||
components: {
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { HTTP } from 'vuejs/rest/http-axios'
|
||||
import { REST } from 'vuejs/api/rest-axios'
|
||||
import { JSONAPI } from 'vuejs/api/json-axios'
|
||||
import qs from 'querystring'
|
||||
|
||||
export default {
|
||||
@@ -10,8 +11,8 @@ export default {
|
||||
// username: '',
|
||||
mail:'',
|
||||
token: null,
|
||||
logout_token: null
|
||||
// isloggedin: false
|
||||
logout_token: null,
|
||||
isloggedin: false
|
||||
},
|
||||
|
||||
// getters
|
||||
@@ -19,31 +20,64 @@ export default {
|
||||
|
||||
// mutations
|
||||
mutations : {
|
||||
setUser (state, data) {
|
||||
setToken (state, data) {
|
||||
state.uid = data.current_user.uid
|
||||
// state.username = data.username
|
||||
state.mail = data.current_user.mail
|
||||
state.token = data.csrf_token
|
||||
state.isloggedin = true
|
||||
state.logout_token = data.logout_token
|
||||
},
|
||||
setUid (state, uid) {
|
||||
state.uid = uid
|
||||
state.isloggedin = true
|
||||
},
|
||||
setUser (state, data) {
|
||||
state.mail = data.mail[0].value
|
||||
state.uuid = data.uuid[0].value
|
||||
},
|
||||
setLoggedOut (state) {
|
||||
state.uid= null
|
||||
state.mail = ''
|
||||
state.token = null
|
||||
state.isloggedin = false
|
||||
state.logout_token = null
|
||||
}
|
||||
},
|
||||
|
||||
// actions
|
||||
actions : {
|
||||
getToken ({ commit, state }, credentials) {
|
||||
HTTP.post('/user/login?_format=json', credentials)
|
||||
getToken ({ dispatch, commit, state }, credentials) {
|
||||
REST.post('/user/login?_format=json', credentials)
|
||||
.then(({ data }) => {
|
||||
console.log('data', data)
|
||||
commit('setUser', data)
|
||||
console.log('user getToken data', data)
|
||||
commit('setToken', data)
|
||||
dispatch('getUser')
|
||||
})
|
||||
.catch(( error ) => {
|
||||
console.log('Issue with login', error)
|
||||
console.warn('Issue with login', error)
|
||||
Promise.reject(error)
|
||||
})
|
||||
},
|
||||
getUser ({ commit, state }) {
|
||||
let params = {
|
||||
token: state.token
|
||||
}
|
||||
REST.get(`/user/${state.uid}?_format=json`, params)
|
||||
.then(({ data }) => {
|
||||
console.log('user REST getUser data', data)
|
||||
commit('setUser', data)
|
||||
JSONAPI.get(`/user/user/${state.uuid}`)
|
||||
.then(({ data }) => {
|
||||
console.log('user JSONAPI getUser data', data)
|
||||
})
|
||||
.catch(( error ) => {
|
||||
console.warn('Issue with getUser', error)
|
||||
Promise.reject(error)
|
||||
})
|
||||
})
|
||||
.catch(( error ) => {
|
||||
console.warn('Issue with getUser', error)
|
||||
Promise.reject(error)
|
||||
})
|
||||
},
|
||||
@@ -51,13 +85,13 @@ export default {
|
||||
let credentials = qs.stringify({
|
||||
token: state.token
|
||||
})
|
||||
HTTP.post('/user/logout', credentials)
|
||||
REST.post('/user/logout', credentials)
|
||||
.then((resp) => {
|
||||
console.log('resp', resp)
|
||||
commit('setLoggedOut')
|
||||
})
|
||||
.catch(( error ) => {
|
||||
console.log('Issue with logout', error)
|
||||
console.warn('Issue with logout', error)
|
||||
Promise.reject(error)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user