@@ -12,20 +12,26 @@ import VerifyEmail from '../views/VerifyEmail';
1212
1313import store from '../store' ;
1414
15- const redirectUnauthorizedToLogin = ( to , from , next ) => {
16- if ( ! store . getters [ 'auth/isAuthenticated' ] ) {
17- next ( '/login' ) ;
18- } else {
19- next ( ) ;
20- }
15+ const requireAuthenticated = ( to , from , next ) => {
16+ store . dispatch ( 'auth/initialize' )
17+ . then ( ( ) => {
18+ if ( ! store . getters [ 'auth/isAuthenticated' ] ) {
19+ next ( '/login' ) ;
20+ } else {
21+ next ( ) ;
22+ }
23+ } ) ;
2124} ;
2225
23- const redirectAuthorizedToHome = ( to , from , next ) => {
24- if ( store . getters [ 'auth/isAuthenticated' ] ) {
25- next ( '/home' ) ;
26- } else {
27- next ( ) ;
28- }
26+ const requireUnauthenticated = ( to , from , next ) => {
27+ store . dispatch ( 'auth/initialize' )
28+ . then ( ( ) => {
29+ if ( store . getters [ 'auth/isAuthenticated' ] ) {
30+ next ( '/home' ) ;
31+ } else {
32+ next ( ) ;
33+ }
34+ } ) ;
2935} ;
3036
3137const redirectLogout = ( to , from , next ) => {
@@ -38,19 +44,19 @@ Vue.use(Router);
3844export default new Router ( {
3945 saveScrollPosition : true ,
4046 routes : [
41- {
42- path : '/about' ,
43- component : About ,
44- beforeEnter : redirectUnauthorizedToLogin ,
45- } ,
4647 {
4748 path : '/' ,
4849 redirect : '/home' ,
4950 } ,
51+ {
52+ path : '/about' ,
53+ component : About ,
54+ beforeEnter : requireAuthenticated ,
55+ } ,
5056 {
5157 path : '/home' ,
5258 component : Home ,
53- beforeEnter : redirectUnauthorizedToLogin ,
59+ beforeEnter : requireAuthenticated ,
5460 } ,
5561 {
5662 path : '/password_reset' ,
@@ -71,7 +77,7 @@ export default new Router({
7177 {
7278 path : '/login' ,
7379 component : Login ,
74- beforeEnter : redirectAuthorizedToHome ,
80+ beforeEnter : requireUnauthenticated ,
7581 } ,
7682 {
7783 path : '/logout' ,
0 commit comments