From c97f2f97c3d34591820bdaeea627f9660be2fc80 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Sat, 8 Oct 2016 12:36:08 +0200 Subject: [PATCH] add refresh token method in auth service --- src/api/auth.js | 8 ++++++++ test/auth.js | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/api/auth.js b/src/api/auth.js index 24dfcae..089d3c3 100644 --- a/src/api/auth.js +++ b/src/api/auth.js @@ -46,6 +46,14 @@ export default class Auth { }); } + refreshToken() { + const config = this.storage.json(); + const token = this.storage.getToken(); + return Auth._requestNewToken({token: token.name}, config).then(token => { + this.storage.saveToken(token) + }) + } + static _requestNewToken(token, config = {}) { return axios.post('/api/tokens/refresh/', token, config).then(response => { return response.data.token; diff --git a/test/auth.js b/test/auth.js index f0e3fa1..5076fb1 100644 --- a/test/auth.js +++ b/test/auth.js @@ -84,3 +84,14 @@ test('login custom endpoint', t => { t.is(JSON.parse(storage.getItem(LOCAL_STORAGE_KEY)).jwt, token); }); }); + +test('refresh token', t => { + const token = '3e3231'; + const storage = new LocalStorageMock(); + const auth = AuthFactory(token, storage); + const newToken = 'wibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9eyJzdWIiOiIxMjM0NTY3ODkwIi'; + nock('https://lesspass.com').post('/api/tokens/refresh/', {token}).reply(200, {token: newToken}); + return auth.refreshToken().then(() => { + t.is(JSON.parse(storage.getItem(LOCAL_STORAGE_KEY)).jwt, newToken); + }); +}); \ No newline at end of file