Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

Commit f68c9d0

Browse files
committed
finalize signin, signup, router fixes, axios interceptors update, header auth. check
1 parent f738849 commit f68c9d0

File tree

7 files changed

+61
-52
lines changed

7 files changed

+61
-52
lines changed

MBO/src/components/Account/Signin.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,21 @@ export default {
5858
this.$validator.validateAll().then(success => {
5959
if (!success) return
6060
this.state.loading = true
61-
this.signin(this.credentials)
61+
this.signin(this.credentials).catch(err => {
62+
if (err.response.status === 400) {
63+
this.$toast.error({
64+
title: 'Signin failed',
65+
message: err.response.data.error_description
66+
})
67+
} else {
68+
this.$toast.error({
69+
title: 'Error',
70+
message: 'An unknown error occurred'
71+
})
72+
}
73+
}).then(() => {
74+
this.state.loading = false
75+
})
6276
})
6377
}
6478
}

MBO/src/components/Account/Signup.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,23 @@ export default {
6565
this.signup(this.credentials).then(res => {
6666
this.$toast.success({
6767
title: 'Account created',
68-
message: 'You will be redirected to signin page in few seconds. '
68+
message: 'You will be redirected to signin page in few seconds. ',
69+
timeOut: 2500
6970
})
7071
window.setTimeout(() => {
71-
this.$router.push('signin')
72+
this.$router.push('/signin')
7273
}, 3000)
7374
}).catch(err => {
7475
if (err.response.status === 400) {
7576
this.$toast.error({
7677
title: err.response.data.Message,
7778
message: err.response.data.ModelState[''][0]
7879
})
80+
} else {
81+
this.$toast.error({
82+
title: 'Error',
83+
message: 'An unknown error occurred'
84+
})
7985
}
8086
}).then(() => {
8187
this.state.loading = false

MBO/src/components/App.vue

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<script>
1313
import appHeader from './Layout/Header'
1414
import appFooter from './Layout/Footer'
15-
import { mapGetters } from 'vuex'
1615
1716
export default {
1817
data () {
@@ -33,17 +32,7 @@ export default {
3332
} else {
3433
this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
3534
}
36-
},
37-
'isAuthenticated' (val) {
38-
console.log('authentication', val)
39-
if (val) {
40-
} else {
41-
42-
}
4335
}
44-
},
45-
computed: {
46-
...mapGetters(['isAuthenticated'])
4736
}
4837
}
4938

MBO/src/components/Layout/Header.vue

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
<md-whiteframe class="md-large" md-tag="md-toolbar" md-elevation="0">
3232
<div class="md-toolbar-container"></div>
3333
<div class="md-toolbar-container">
34-
<h2 class="md-title" v-show="!isAuthenticated">My Account</h2>
35-
<h2 class="md-title" v-show="isAuthenticated">{{userInfo.Email}}</h2>
34+
<h2 class="md-title" v-if="!isAuthenticated && !userInfo">My Account</h2>
35+
<h2 class="md-title" v-else>{{userInfo.Email}}</h2>
3636
<span style="flex: 1;"></span>
3737
</div>
3838
</md-whiteframe>
@@ -179,38 +179,33 @@
179179
</div>
180180
</template>
181181
<script>
182-
import {
183-
signout
184-
} from 'services/account'
182+
import {mapGetters, mapActions} from 'vuex'
185183
186184
export default {
187185
name: 'app-header',
188186
watch: {
189187
'$route' () {
190188
window.setTimeout(this.$refs.leftSidenav.close, 500)
189+
},
190+
'isAuthenticated' (val) {
191+
if (!val) {
192+
this.$router.push({
193+
path: '/'
194+
})
195+
}
191196
}
192197
},
193198
computed: {
194-
isAuthenticated () {
195-
return this.$store.getters.isAuthenticated
196-
},
197-
userInfo () {
198-
return this.$store.getters.getUserInfo || {}
199-
}
199+
...mapGetters(['isAuthenticated', 'userInfo'])
200200
},
201201
methods: {
202+
...mapActions(['signout']),
202203
openDialog (ref) {
203204
this.$refs[ref].open()
204205
},
205206
onClose (type) {
206207
if (type === 'ok') {
207-
signout().then(res => {
208-
this.$router.push({
209-
path: '/signout'
210-
})
211-
}).catch(err => {
212-
console.log(err)
213-
})
208+
this.signout()
214209
}
215210
},
216211
toggleLeftSidenav () {
@@ -221,8 +216,7 @@ export default {
221216
path
222217
})
223218
}
224-
},
225-
mounted () { }
219+
}
226220
}
227221
228222
</script>

MBO/src/utilities/axios/Index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@ import axios from 'axios'
22
import store from 'utilities/store'
33
import router from 'utilities/router'
44

5-
axios.interceptors.request.use(config => {
5+
axios.interceptors.request.use(cfg => {
66
if (store.getters.isAuthenticated) {
77
const auth = store.getters.getAuth
8-
config.headers.Authorization = `${auth.token_type} ${auth.access_token}`
8+
cfg.headers.Authorization = `${auth.token_type} ${auth.access_token}`
99
}
10-
return config
11-
}, error => {
12-
return Promise.reject(error)
10+
return cfg
11+
}, err => {
12+
return Promise.reject(err)
1313
})
1414

15-
axios.interceptors.response.use(response => response, error => {
16-
switch (error.response.status) {
15+
axios.interceptors.response.use(res => res, err => {
16+
switch (err.response.status) {
1717
case 401:
1818
store.commit('removeAuthentication')
19-
router.push('/signin?redirect=' + router.app._route.path)
20-
break
21-
case 500:
19+
router.push({
20+
path: '/signin',
21+
query: {
22+
redirect: router.app._route.fullPath
23+
}
24+
})
2225
break
2326
default:
24-
return Promise.reject(error)
27+
return Promise.reject(err)
2528
}
2629
})
2730

MBO/src/utilities/router/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@ const router = new Router({
136136

137137
router.beforeEach((to, from, next) => {
138138
if (store.getters.isAuthenticated) {
139-
const notValid = ['signin', 'signup', 'recover']
139+
const notValid = ['/signin', '/signup']
140140

141-
if (notValid.lastIndexOf(to.name) >= 0) {
141+
if (notValid.lastIndexOf(to.path) >= 0) {
142142
next({
143143
path: '/dashboard'
144144
})
145145
} else if (to.path === '/signout') {
146146
store.commit('removeAuthentication')
147147
next({
148-
path: '/home'
148+
path: '/'
149149
})
150150
} else {
151151
next()
@@ -160,7 +160,7 @@ router.beforeEach((to, from, next) => {
160160
})
161161
} else if (to.path === '/signout') {
162162
next({
163-
path: '/home'
163+
path: '/'
164164
})
165165
} else {
166166
next()

MBO/src/utilities/store/user/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ export default {
2626
return account.signup(credentials)
2727
},
2828
signin (context, credentials) {
29-
account.signin(credentials).then((res) => {
29+
return account.signin(credentials).then((res) => {
3030
context.commit('setAuthentication', res.data)
3131
account.getUserInfo().then((res) => {
3232
context.commit('setUserInfo', res.data)
33-
}).catch(() => {})
34-
}).catch(() => {})
33+
})
34+
})
3535
},
3636
signout (context) {
3737
account.signout().then(() => {
@@ -48,6 +48,9 @@ export default {
4848
}
4949
},
5050
getters: {
51+
getAuth (state) {
52+
return state.auth
53+
},
5154
isAuthenticated (state) {
5255
return state.auth !== null && typeof state.auth !== 'undefined'
5356
},

0 commit comments

Comments
 (0)