move auth into vuex state
This commit is contained in:
14
src/App.vue
14
src/App.vue
@@ -38,12 +38,24 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import TopNav from './components/TopNav.vue';
|
import TopNav from './components/TopNav.vue';
|
||||||
|
import store from './store'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'App',
|
name: 'App',
|
||||||
|
store,
|
||||||
components: {
|
components: {
|
||||||
TopNav,
|
TopNav,
|
||||||
}
|
},
|
||||||
|
// pre-route auth checking
|
||||||
|
beforeRouteEnter(to, from, next) {
|
||||||
|
// if user is authenticated, continue
|
||||||
|
if (store.getters.isAuthenticated) return next();
|
||||||
|
|
||||||
|
// otherwise, redirect to login, preserving the requested route
|
||||||
|
const { name, fullPath } = to;
|
||||||
|
const query = (name !== undefined) ? { redirect: name } : { prev: fullPath };
|
||||||
|
return next({ name: 'login', query });
|
||||||
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
export function getUser() {
|
|
||||||
return Promise.resolve(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isAuthenticated() {
|
|
||||||
return getUser().then(Boolean);
|
|
||||||
}
|
|
||||||
14
src/main.js
14
src/main.js
@@ -1,21 +1,17 @@
|
|||||||
import Vue from 'vue';
|
import Vue from 'vue';
|
||||||
import router from './router';
|
import router from './router';
|
||||||
import store from './store';
|
import store from './store';
|
||||||
import { isAuthenticated } from './lib/authentication';
|
|
||||||
|
|
||||||
// pre-route auth checking
|
// pre-route auth checking
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
// check if any of the matched routes require authentication
|
// check if any of the matched routes require authentication
|
||||||
if (to.matched.some(record => record.meta.requiresAuthentication)) {
|
if (to.matched.some(record => record.meta.requiresAuthentication)) {
|
||||||
return isAuthenticated().then((user) => {
|
if (store.getters.isAuthenticated) return next();
|
||||||
// if user is authenticated, continue
|
|
||||||
if (user) return next();
|
|
||||||
|
|
||||||
// otherwise, redirect to login, preserving the requested route
|
// otherwise, redirect to login, preserving the requested route
|
||||||
const { name, fullPath } = to;
|
const { name, fullPath } = to;
|
||||||
const query = (name !== undefined) ? { redirect: name } : { prev: fullPath };
|
const query = (name !== undefined) ? { redirect: name } : { prev: fullPath };
|
||||||
return next({ name: 'login', query });
|
return next({ name: 'login', query });
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import { isAuthenticated } from '../lib/authentication';
|
import { isAuthenticated } from '../lib/authentication';
|
||||||
import TopNav from '../components/TopNav.vue';
|
import TopNav from '../components/TopNav.vue';
|
||||||
|
import store from '../store'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'login-page',
|
name: 'login-page',
|
||||||
@@ -35,22 +36,21 @@
|
|||||||
sendTo: {},
|
sendTo: {},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
beforeRouteEnter (to, from, next) {
|
beforeCreate() {
|
||||||
const getRedirect = () => {
|
const getRedirect = (query) => {
|
||||||
const { prev, redirect } = to.query;
|
const { prev, redirect } = query;
|
||||||
if (redirect !== undefined) return { name: redirect };
|
if (redirect !== undefined) return { name: redirect };
|
||||||
if (prev !== undefined) return { path: prev };
|
if (prev !== undefined) return { path: prev };
|
||||||
return { name: 'customers' };
|
return { name: 'app' };
|
||||||
}
|
}
|
||||||
|
|
||||||
return isAuthenticated().then(user => {
|
const sendTo = getRedirect(this.$route.query);
|
||||||
const sendTo = getRedirect();
|
|
||||||
// if already logged in, redirect
|
|
||||||
if (user) return next(sendTo);
|
|
||||||
|
|
||||||
// preserve the sendTo property, render the login page
|
// if authenticated, set to correct spot in the app
|
||||||
next(vm => vm.sendTo = sendTo);
|
if (store.getters.isAuthenticated) return this.$router.replace(sendTo);
|
||||||
});
|
|
||||||
|
// if not authenticated, save redirect info for post-authentication redirect
|
||||||
|
return this.sendTo = sendTo;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
import Vue from 'vue';
|
import Vue from 'vue';
|
||||||
import VueRouter from 'vue-router';
|
import VueRouter from 'vue-router';
|
||||||
|
|
||||||
import App from './App.vue';
|
import App from '../App.vue';
|
||||||
|
|
||||||
import Login from './pages/Login.vue';
|
import Login from '../pages/Login.vue';
|
||||||
import Customers from './pages/Customers.vue';
|
import Customers from '../pages/Customers.vue';
|
||||||
import Orders from './pages/Orders.vue';
|
import Orders from '../pages/Orders.vue';
|
||||||
import About from './pages/About.vue';
|
import About from '../pages/About.vue';
|
||||||
|
|
||||||
Vue.use(VueRouter);
|
Vue.use(VueRouter);
|
||||||
|
|
||||||
10
src/store/authentication.js
Normal file
10
src/store/authentication.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
export default {
|
||||||
|
state: {
|
||||||
|
user: null,
|
||||||
|
},
|
||||||
|
getters: {
|
||||||
|
isAuthenticated(state) {
|
||||||
|
return state.user !== null;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -1,11 +1,16 @@
|
|||||||
import Vue from 'vue';
|
import Vue from 'vue';
|
||||||
import Vuex from 'vuex';
|
import Vuex from 'vuex';
|
||||||
|
|
||||||
|
import authentication from './authentication';
|
||||||
|
|
||||||
Vue.use(Vuex);
|
Vue.use(Vuex);
|
||||||
|
|
||||||
const store = new Vuex.Store({
|
const store = new Vuex.Store({
|
||||||
state: {
|
state: {
|
||||||
},
|
},
|
||||||
|
modules: {
|
||||||
|
authentication,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default store;
|
export default store;
|
||||||
Reference in New Issue
Block a user