From f2f9aa30b49921bcc5767f36b54b2cba2610d876 Mon Sep 17 00:00:00 2001 From: joelkleppinger Date: Thu, 19 Oct 2017 15:42:16 +0000 Subject: [PATCH] Add RefreshSession action and necessary RefreshSessionObj --- src/actions.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/actions.js b/src/actions.js index a6263cc..c6eaab0 100644 --- a/src/actions.js +++ b/src/actions.js @@ -13,6 +13,7 @@ function constructUser(cognitoUser, session) { IdToken: session.getIdToken().getJwtToken(), AccessToken: session.getAccessToken().getJwtToken(), RefreshToken: session.getRefreshToken().getToken(), + RefreshTokenObj: session.getRefreshToken() }, attributes: {}, }; @@ -199,6 +200,29 @@ export default function actionsFactory(config) { }); }, + // Only for authenticated users + refreshSession ({ commit, state }) { + const refreshToken = state.user.tokens.RefreshTokenObj + + const cognitoUser = new CognitoUser({ + Pool: cognitoUserPool, + Username: state.user.username, + }); + + // Restore session without making an additional call to API + cognitoUser.signInUserSession = cognitoUser.getCognitoUserSession(state.user.tokens); + + return new Promise((resolve, reject) => cognitoUser.refreshSession(refreshToken, + (err, session) => { + if (!err) { + commit(types.AUTHENTICATE, constructUser(cognitoUser, session)); + resolve(); + return; + } + reject(err); + })); + }, + // Only for authenticated users updateAttributes({ commit, state }, payload) { return new Promise((resolve, reject) => {