Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: videsk/jwt-webauth
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.5.0
Choose a base ref
...
head repository: videsk/jwt-webauth
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 8 commits
  • 4 files changed
  • 1 contributor

Commits on Apr 7, 2022

  1. Copy the full SHA
    c21afd5 View commit details
  2. Merge pull request #47 from videsk/develop

    fix(storage): return undefined not null when get tokens
    matiaslopezd authored Apr 7, 2022
    Copy the full SHA
    9ffbd10 View commit details
  3. Copy the full SHA
    0d811a4 View commit details
  4. Merge pull request #48 from videsk/develop

    fix(clean): fixed key name for delete from storage
    matiaslopezd authored Apr 7, 2022
    Copy the full SHA
    184e1b3 View commit details

Commits on Apr 22, 2022

  1. feat: new login method

    Previously if exist expired tokens on storage conflict with the new access and refresh token, so was added login method where force use new tokens and discard older
    matiaslopezd committed Apr 22, 2022
    Copy the full SHA
    a63798b View commit details
  2. Merge pull request #49 from videsk/develop

    feat: new login method
    matiaslopezd authored Apr 22, 2022
    Copy the full SHA
    68db158 View commit details

Commits on May 18, 2022

  1. fix(storage): initialization storage fixed

    Before this fix every time WebAuth was initialized the storage was set as sessionStorage. Now only change if calls the method login.
    matiaslopezd committed May 18, 2022
    Copy the full SHA
    b0ed6f7 View commit details
  2. Merge pull request #50 from videsk/develop

    fix(storage): initialization storage fixed
    matiaslopezd authored May 18, 2022
    Copy the full SHA
    94067c6 View commit details
Showing with 185 additions and 33 deletions.
  1. +51 −9 .idea/workspace.xml
  2. +26 −8 src/index.js
  3. +100 −14 test/auth.test.js
  4. +8 −2 test/server.js
60 changes: 51 additions & 9 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 26 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -42,17 +42,28 @@ class WebAuth {
return this;
}

/**
* Login forcing new accessToken and refreshToken
* @param accessToken
* @param refreshToken
* @param remember
* @returns {Promise<String|undefined>}
*/
login(accessToken, refreshToken, remember = false) {
this.storage = remember ? 'localStorage' : 'sessionStorage';
return this.set(accessToken, refreshToken, remember, true);
}

/**
* Set tokens
* @param access {String} - Access token
* @param refresh {String} - Refresh token
* @param remember {Boolean} - Store in session or local storage
* @param forceNew {Boolean=} - Set if is login to drop older
* @returns {Promise<String|undefined>}
*/
async set(access = '', refresh = '', remember = false) {
this.running = true;
this.storage = remember ? 'localStorage' : 'sessionStorage';
const { accessToken = access, refreshToken = refresh } = this.getTokens();
async set(access = '', refresh = '', remember = false, forceNew = false) {
const { accessToken = access, refreshToken = refresh } = !forceNew ? this.getTokens() : { accessToken: access, refreshToken: refresh };
this.debug('log', 'Initializing WebAuth with tokens', accessToken, refreshToken);
if (!accessToken) return this.fire('empty', accessToken);
const tokens = [accessToken];
@@ -63,7 +74,10 @@ class WebAuth {
const isValid = await this.fire('verify', accessToken, refreshToken, this.events.expired, this.events.error);
this.debug('info', 'The verification of accessToken is', isValid);
if (!isValid && !refreshToken) return this.fire('expired', 'accessToken');
if (isValid) this.fire('ready');
if (isValid) {
this.running = true;
this.fire('ready');
}
return isValid ? this.observer() : this.renew();
} catch (error) {
this.debug('error', 'Error trying to verifying accessToken.', error);
@@ -106,6 +120,10 @@ class WebAuth {
this.debug('info', 'accessToken has renewed', newAccessToken);
this.setToken('accessToken', newAccessToken);
this.fire('renewed');
if (!this.running) {
this.running = true;
this.fire('ready');
}
return this.observer();
} catch (error) {
this.debug('error', 'Error trying to renew accessToken', error);
@@ -120,7 +138,7 @@ class WebAuth {
*/
clean() {
this.debug('log', 'Cleaning store...');
const keys = Object.keys(this.keys);
const keys = Object.values(this.keys);
keys.forEach(key => {
window.localStorage.removeItem(key);
window.sessionStorage.removeItem(key);
@@ -160,8 +178,8 @@ class WebAuth {
*/
getTokens(token) {
const tokens = {
accessToken: window[this.storage].getItem(this.keys.accessToken),
refreshToken: window[this.storage].getItem(this.keys.refreshToken)
accessToken: window[this.storage].getItem(this.keys.accessToken) || undefined,
refreshToken: window[this.storage].getItem(this.keys.refreshToken) || undefined
};
this.debug('log', `Getting token from ${this.storage}`, tokens);
return token ? tokens[token] : tokens;
Loading