import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const Auth = {
state: {
auth: false, // Авторизован ли пользователь
user: {}, // Данные о пользователе
token: {}, // Токен для API
},
mutations: {
getAuthentication: (state, data) => {
return Vue.axios.post('/oauth/token', data)
.then(response => {
console.log(response.data)
return response.data
})
},
/**
* Сохраняем данные токена
* @param state
*/
tokenAuth: (state, token) => {
state.token = token
},
/**
* Сохраняем информацию о том, что пользователь авторизован
* @param state
* @param data
*/
auth: (state, data) => {
state.auth = true
state.user = data
},
/**
* "Выходим" из аккаунта и удаляем все данные с auth()
* @param state
*/
logout: (state) => {
state.auth = false
state.user = {}
state.user = {}
}
},
}
export default Auth
import Vue from 'vue';
import VueRouter from 'vue-router'
import Meta from 'vue-meta'
import store from '../store'
Vue.use(VueRouter)
Vue.use(Meta)
import Home from '../components/Home.vue'
import News from '../components/News.vue'
import Games from '../components/Games.vue'
import Users from '../components/Users/Users.vue'
import Logout from '../components/Users/Logout.vue'
import NotFound from '../components/pages/NotFound.vue'
// Авторизация
import Login from '../components/Auth/Login.vue'
import Register from '../components/Auth/Register.vue'
const router = new VueRouter({
mode: 'history',
routes: [
/* Нейтральные страницы */
{
path: '/',
name: 'home',
component: Home
},
{
path: '/news',
name: 'news',
component: News,
},
{
path: '/games',
name: 'games',
component: Games,
},
/* Для пользователей */
{
path: '/users/:id',
name: 'users',
component: Users,
meta: {
isAuth: true
},
},
{
path: '/logout',
name: 'users.logout',
component: Logout,
meta: {
isAuth: true
}
},
/* Для гостей */
{
path: '/auth/login',
name: 'auth.login',
component: Login,
meta: {
isGuest: false
}
},
{
path: '/auth/register',
name: 'auth.register',
component: Register,
meta: {
isGuest: true
}
},
/* Web-серверные ошибки и сообщения */
{
/* 404 - Page not found */
path: '/404',
name: 'notfound',
component: NotFound
}, {
path: '*',
redirect: '/404'
}
],
linkActiveClass: '',
linkExactActiveClass: ''
})
router.beforeEach(
(to, from, next) => {
if(to.matched.some(record => record.meta.isAuth)){
if(store.state.Auth.auth){
next({
path: '/feed'
})
} else next()
}else if(to.matched.some(record => record.meta.isGuest)){
if(!store.state.Auth.auth){
next({
path: '/auth/login'
})
}else{
next()
}
} else next()
}
)
export default router
а php можно изучить не выходя из дома