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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import Vue from 'vue'; import Vuex from 'vuex'; import VueAxios from 'vue-axios'; import { VueAuthenticate } from 'vue-auth-client'; import axios from 'axios';
Vue.use(Vuex); Vue.use(VueAxios, axios); const vueAuth = new VueAuthenticate(Vue.prototype.$http, { baseUrl: '', // Your API domain providers: { oauth2: { url: '/oauth/token', authorizationEndpoint: 'https://laravel.test/oauth/authorize', clientId: '4', responseParams: { code: 'code', grant_type: 'authorization_code', client_secret: 'WOfm88oUTYQgYdCB0RhndBY7tgUYh4LGg3vP4nof', clientId: 'client_id', redirectUri: 'redirect_uri' } }, }, });
export default new Vuex.Store({ state: { loading: false, oauth2: { access_token: '', expires_in: '', refresh_token: '', }, }, getters: { getToken: state => { let oauth2 = Object.assign({}, state.oauth2) if (!oauth2.expires_in) { oauth2 = window.sessionStorage.getItem('oauth2') oauth2 = oauth2 ? JSON.parse(oauth2) : state.oauth2 } if (new Date().getTime() < oauth2.expires_in) { return oauth2.access_token; } return; }, }, mutations: { setLoading (state, payload) { state.loading = payload; }, setOauth2(state, payload) { window.sessionStorage.setItem('oauth2', JSON.stringify(payload)) state.oauth2 = payload; }, loadOauth2(state) { let oauth2 = window.sessionStorage.getItem('oauth2') oauth2 = oauth2 ? JSON.parse(oauth2) : state.oauth2 state.oauth2 = oauth2; }, }, actions: { login (context) { vueAuth.authenticate('oauth2').then(response => { if (response.status === 200) { context.commit('setOauth2', { access_token: `${response.data.token_type} ${response.data.access_token}`, expires_in: new Date().getTime() + response.data.expires_in * 1000, refresh_token: response.data.refresh_token, }); } }); }, setLoading(context, payload) { context.commit('setLoading', payload); }, }, modules: {} });
|