added profile and token refresh

This commit is contained in:
Tobias Kurze
2019-04-05 16:20:54 +02:00
parent 91edb27529
commit 9a91f40c65
3 changed files with 47 additions and 3 deletions

View File

@@ -4,7 +4,7 @@
<section class="hero is-primary"> <section class="hero is-primary">
<div class="hero-body"> <div class="hero-body">
<div class="container has-text-centered"> <div class="container has-text-centered">
<h2 class="title">Profile</h2> <h2 class="title">Profile ({{tokenValidity}})</h2>
<p>{{profile.id}}</p> <p>{{profile.id}}</p>
<p class="subtitle error-msg">{{ errorMsg }}</p> <p class="subtitle error-msg">{{ errorMsg }}</p>
</div> </div>
@@ -16,12 +16,14 @@
<script> <script>
import { EventBus } from '@/utils' import { EventBus } from '@/utils'
import {getRemainingJwtValiditySeconds} from "../utils";
export default { export default {
data () { data () {
return { return {
email: '', email: '',
errorMsg: '', errorMsg: '',
tokenValidity: -1,
}; };
}, },
methods: { methods: {
@@ -37,6 +39,13 @@
}); });
this.$store.dispatch('loadProfile'); this.$store.dispatch('loadProfile');
this.$nextTick(() =>{
window.setInterval(() => {
this.$log.debug(getRemainingJwtValiditySeconds(this.$store.state.access_token));
this.tokenValidity = getRemainingJwtValiditySeconds(this.$store.state.access_token);
}, 1000);
});
}, },
beforeDestroy () { beforeDestroy () {

View File

@@ -13,7 +13,7 @@ import {
register, register,
oidc_login, fetchUsers, getFreshToken, fetchProfile, oidc_login, fetchUsers, getFreshToken, fetchProfile,
} from '@/api'; } from '@/api';
import { isValidJwt, EventBus } from '@/utils'; import {isValidJwt, EventBus, getRemainingJwtValiditySeconds} from '@/utils';
Vue.use(Vuex); Vue.use(Vuex);
@@ -61,6 +61,22 @@ const actions = {
EventBus.$emit('failedLoadingUsers', error); EventBus.$emit('failedLoadingUsers', error);
}); });
}, },
loadProfileAuthCheck(context:any){
if (!getters.isAuthenticated) {
EventBus.$emit('accessTokenInvalid');
if (!getters.isRefreshTokenValid) {
Vue.$log.warn('Access and refresh token invalid! User must login again!');
EventBus.$emit('refreshTokenInvalid');
EventBus.$emit('accessAndRefreshTokenInvalid');
} else {
return context.dispatch('refreshToken').then( () => {
context.commit('loadProfile');
});
}
} else {
context.commit('loadProfile');
}
},
loadProfile(context: any) { loadProfile(context: any) {
return fetchProfile(context.state.access_token) return fetchProfile(context.state.access_token)
.then((response) => { .then((response) => {
@@ -100,7 +116,7 @@ const actions = {
}); });
}, },
refreshToken(context: any) { refreshToken(context: any) {
// context.commit('setUserData', { userData }); EventBus.$emit('refreshingToken');
return getFreshToken(context.state.refresh_token) return getFreshToken(context.state.refresh_token)
.then((response) => context.commit('setTokens', { tokens: response.data })) .then((response) => context.commit('setTokens', { tokens: response.data }))
.catch((error) => { .catch((error) => {
@@ -184,6 +200,15 @@ const getters = {
isAuthenticated(sState: any) { isAuthenticated(sState: any) {
return isValidJwt(sState.access_token); return isValidJwt(sState.access_token);
}, },
isRefreshTokenValid(sState: any) {
return isValidJwt(sState.refresh_token);
},
getAccessTokenValidity(sState: any) {
return getRemainingJwtValiditySeconds(sState.access_token);
},
getRefreshTokenValidity(sState: any) {
return getRemainingJwtValiditySeconds(sState.refresh_token);
},
getLoginProviders(sState: any) { getLoginProviders(sState: any) {
return sState.loginProviders; return sState.loginProviders;
}, },

View File

@@ -13,3 +13,13 @@ export function isValidJwt(jwt: any) {
const now = new Date(); const now = new Date();
return now < exp; return now < exp;
} }
export function getRemainingJwtValiditySeconds(jwt: any) {
if (!jwt || jwt.split('.').length < 3) {
return false;
}
const data = JSON.parse(atob(jwt.split('.')[1]));
const exp = new Date(data.exp * 1000);
const diff = new Date().getTime() - exp.getTime();
return Math.abs(diff / 1000);
}