Skip to content

Commit c163e21

Browse files
committed
Added jwt-decode in user auth module
- Adjusted variables in user state token - Updated signalR to disconnect before leaving the page
1 parent 7cfb16d commit c163e21

File tree

9 files changed

+42
-11
lines changed

9 files changed

+42
-11
lines changed

src/Client/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"dependencies": {
1010
"@aspnet/signalr": "^1.1.0",
11+
"jwt-decode": "^2.2.0",
1112
"material-design-icons-iconfont": "^4.0.4",
1213
"moment": "^2.24.0",
1314
"roboto-fontface": "^0.10.0",

src/Client/src/components/_shared/navigation.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<v-list dense>
1111
<v-list-tile v-if="isAuthenticated">
1212
<v-list-tile-content>
13-
<v-list-tile-title>Logged in as <b class="orange--text"> {{ currentUser.fullName }}</b></v-list-tile-title>
13+
<v-list-tile-title>Logged in as <b class="orange--text"> {{ currentUser.full_name }}</b></v-list-tile-title>
1414
</v-list-tile-content>
1515
</v-list-tile>
1616

src/Client/src/components/employee/update.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
label="New Password"
113113
type="Password"
114114
color="orange"
115-
v-model="form.newPassword"
115+
v-model="form.password"
116116
required
117117
:rules="[required, minLength]"
118118
:disabled="!formStates.form2.isEdit"

src/Client/src/components/logs/list.vue

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ export default {
191191
]
192192
193193
if(this.isRole('Employee')) {
194-
const userId = this.currentUser.empId
195-
this.items = this.logs.data.filter(m => m.employeeId === userId)
194+
const empId = this.currentUser.employee_id
195+
this.items = this.logs.data.filter(m => m.employeeId === empId)
196196
this.headers = [
197197
{ text: 'Time In', value: 'timeIn' },
198198
{ text: 'Time Out', value: 'timeOut' }
@@ -270,7 +270,16 @@ export default {
270270
},
271271
272272
created () {
273+
// connect to websocket
274+
BroadcastConnection.start()
275+
273276
this.setDefaultMeta()
274-
}
277+
},
278+
279+
// disconnect the websocket before leaving component
280+
beforeRouteLeave (to, from, next) {
281+
BroadcastConnection.stop()
282+
next()
283+
},
275284
}
276285
</script>

src/Client/src/plugins/axios.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ _axios.interceptors.response.use(
6565
break;
6666

6767
default:
68-
message = error.response.data || defaultErrorMessage
68+
message = error.response.data.error_description || defaultErrorMessage
6969
break;
7070
}
7171

src/Client/src/services/broadcast-service.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ const connection = new signalR.HubConnectionBuilder()
44
.withUrl(process.env.VUE_APP_BASE_URL + 'broadcast')
55
.build();
66

7-
connection.start().catch(err => document.write(err));
8-
97
export default connection

src/Client/src/store/modules/auth-module.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import JwtService from '@/services/jwt-service'
2+
import decode from 'jwt-decode'
23

34
const state = {
45
user: {},
@@ -16,8 +17,25 @@ const getters = {
1617

1718
const mutations = {
1819
SET_AUTH (state, data) {
20+
// decode payload
21+
const decodedJwt = decode(data.access_token)
22+
23+
// set up the payload in user state
24+
state.user = {
25+
username: decodedJwt['sub'],
26+
27+
employee_id: decodedJwt['employee_id'],
28+
29+
// default key name in jwt
30+
full_name: decodedJwt['full_name'],
31+
32+
// role generated by asp.net
33+
roles: [
34+
decodedJwt['http://schemas.microsoft.com/ws/2008/06/identity/claims/role']
35+
]
36+
}
37+
1938
state.isAuthenticated = true
20-
state.user = data.user
2139
JwtService.saveToken(data.access_token)
2240
},
2341
DESTROY_AUTH (state) {

src/Client/src/views/login.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export default {
7979
await this.$store.dispatch('SET_SETTINGS', defaultSettings)
8080
8181
// redirect according to role
82-
const currentUserRole = result.user.roles
83-
if(currentUserRole.includes('Admin')) {
82+
const currentUserRole = this.currentUser.roles
83+
if(currentUserRole === 'Admin') {
8484
this.$router.push({ name: 'employees' })
8585
} else {
8686
this.$router.push({ name: 'logs' })

0 commit comments

Comments
 (0)