Skip to content

Commit a05a9eb

Browse files
author
Walker Leite
committed
feat(Login): add sessionError with authorization error redirection
1 parent 77c6fcf commit a05a9eb

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

template/client/router.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const router = new VueRouter({
2323
path: '/login',
2424
name: 'login',
2525
component: Login,
26+
props: true,
2627
}, {
2728
path: '/profile',
2829
name: 'profile',

template/client/store/modules/auth/actions.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ import router from '@/router';
44
/**
55
* Sync loopback token with current state
66
*/
7-
export function syncToken({commit}) {
7+
export function syncToken({commit, dispatch}) {
88
if (loopback.token) {
99
commit('setAccessToken', loopback.token);
10+
return dispatch(
11+
'loadAccount',
12+
loopback.token.userId,
13+
).catch((err) => {
14+
commit('setAccessToken', null);
15+
return err;
16+
});
1017
}
18+
return Promise.resolve();
1119
}
1220

13-
/**
14-
* Sync router for auth
15-
*/
16-
export function syncRouter({state, dispatch}, myRouter) {
17-
dispatch('syncToken');
18-
19-
myRouter.beforeEach((to, from, next) => {
21+
function evaluateRoute(state, to, from, next) {
22+
return (sessionError) => {
2023
if (to.matched.some(record => record.meta.requiresAuth)) {
2124
// this route requires auth, check if logged in
2225
// if not, redirect to login page (except when it's profile route and
@@ -26,14 +29,27 @@ export function syncRouter({state, dispatch}, myRouter) {
2629
} else if (!state.access_token) {
2730
next({
2831
name: 'login',
32+
params: {
33+
sessionError,
34+
},
2935
});
3036
} else {
31-
dispatch('loadAccount', state.access_token.userId)
32-
.then(next);
37+
next();
3338
}
3439
} else {
3540
next(); // make sure to always call next()!
3641
}
42+
};
43+
}
44+
45+
/**
46+
* Sync router for auth
47+
*/
48+
export function syncRouter({state, dispatch}, myRouter) {
49+
myRouter.beforeEach((to, from, next) => {
50+
dispatch('syncToken').then(
51+
evaluateRoute(state, to, from, next)
52+
);
3753
});
3854
}
3955

@@ -60,7 +76,6 @@ export function signIn({commit, dispatch, state}, {email, password}) {
6076
loopback.removeToken();
6177
}
6278

63-
router.push({name: 'dashboard'});
6479
return dispatch('loadAccount', state.access_token.userId);
6580
});
6681
}
@@ -94,9 +109,9 @@ export function loadAccount({commit}, userId) {
94109
return loopback
95110
.get(`/Accounts/${userId}`)
96111
.then(acc => commit('setAccount', acc))
97-
.catch(() => {
112+
.catch((err) => {
98113
loopback.removeToken();
99-
router.push({name: 'login'});
114+
return Promise.reject(err);
100115
});
101116
}
102117

template/client/view/Login.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@
9595

9696
<script>
9797
export default {
98+
name: 'Login',
99+
props: {
100+
sessionError: {
101+
type: Error,
102+
default: null,
103+
},
104+
},
98105
data() {
99106
return {
100107
email: null,
@@ -106,6 +113,14 @@ export default {
106113
loading: false,
107114
};
108115
},
116+
watch: {
117+
sessionError: {
118+
handler(err) {
119+
this.error = err;
120+
},
121+
immediate: true,
122+
},
123+
},
109124
methods: {
110125
onSubmit(evt) {
111126
evt.preventDefault();
@@ -118,6 +133,7 @@ export default {
118133
password: this.password,
119134
})
120135
.then(() => {
136+
this.$router.push({name: 'agenda'});
121137
this.loading = false;
122138
})
123139
.catch((err) => {

0 commit comments

Comments
 (0)