From 77a47f7dd9b8cf365ff3ca5f32c7d82e1dd8d20c Mon Sep 17 00:00:00 2001 From: joelkleppinger Date: Fri, 20 Oct 2017 15:51:49 +0000 Subject: [PATCH] Add cognitoAuthenticated getter to determine whether the session is still active --- src/actions.js | 1 + src/getters.js | 10 ++++++++++ src/index.js | 2 ++ 3 files changed, 13 insertions(+) create mode 100644 src/getters.js diff --git a/src/actions.js b/src/actions.js index a6263cc..6280a72 100644 --- a/src/actions.js +++ b/src/actions.js @@ -14,6 +14,7 @@ function constructUser(cognitoUser, session) { AccessToken: session.getAccessToken().getJwtToken(), RefreshToken: session.getRefreshToken().getToken(), }, + expiration: session.getAccessToken().getExpiration(), attributes: {}, }; } diff --git a/src/getters.js b/src/getters.js new file mode 100644 index 0000000..db2b0a8 --- /dev/null +++ b/src/getters.js @@ -0,0 +1,10 @@ +export default { + cognitoAuthenticated: state => { + let now = Math.floor(new Date() / 1000) + + return !(state.user === null + || (state.user && state.user.tokens === null) + || now > state.user.expiration + ) + } +}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 9f281ac..0ce6dd3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import ActionsFactory from './actions'; import mutations from './mutations'; +import getters from './getters'; const state = { user: null, @@ -27,5 +28,6 @@ export default class CognitoAuth { this.state = state; this.actions = new ActionsFactory(config); this.mutations = mutations; + this.getters = getters; } }