From 7604976b98767f18882eac7cdc917a051cd7b4d6 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 22 Nov 2019 08:51:32 -0600 Subject: [PATCH] Convert to TS - final tweaks (#36) --- src/app/src/auth/index.ts | 25 ++++++------------------- src/app/src/auth/types.ts | 15 ++++++++------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/app/src/auth/index.ts b/src/app/src/auth/index.ts index f10010d..e041c1e 100644 --- a/src/app/src/auth/index.ts +++ b/src/app/src/auth/index.ts @@ -5,7 +5,7 @@ import { EventEmitter } from 'events' import { Mutations, AppState } from '@/store/types' import Auth0Config from './auth0-variables' -import { Session } from './types' +import { Session, Token } from './types' // Auth0 web authentication instance to use for our calls const webAuth = new auth0.WebAuth({ @@ -79,10 +79,8 @@ class AuthService extends EventEmitter { */ setSession (authResult: Auth0DecodedHash) { this.session.profile = authResult.idTokenPayload - this.session.id.token = authResult.idToken! - this.session.id.expiry = this.session.profile.exp * 1000 - this.session.access.token = authResult.accessToken! - this.session.access.expiry = authResult.expiresIn! * 1000 + Date.now() + this.session.id = new Token(authResult.idToken!, this.session.profile.exp * 1000) + this.session.access = new Token(authResult.accessToken!, authResult.expiresIn! * 1000 + Date.now()) localStorage.setItem(this.AUTH_SESSION, JSON.stringify(this.session)) @@ -97,21 +95,10 @@ class AuthService extends EventEmitter { * Refresh this instance's session from the one in local storage */ refreshSession () { - const emptySession = { - profile: {}, - id: { - token: null, - expiry: null - }, - access: { - token: null, - expiry: null - } - } this.session = localStorage.getItem(this.AUTH_SESSION) ? JSON.parse(localStorage.getItem(this.AUTH_SESSION) || '{}') - : emptySession + : new Session() } /** @@ -158,14 +145,14 @@ class AuthService extends EventEmitter { * Is there a user authenticated? */ isAuthenticated () { - return this.session.id.isValid() + return this.session && this.session.id && this.session.id.isValid() } /** * Is the current access token valid? */ isAccessTokenValid () { - return this.session.access.isValid() + return this.session && this.session.access && this.session.access.isValid() } /** diff --git a/src/app/src/auth/types.ts b/src/app/src/auth/types.ts index e3bc8af..19ed0a5 100644 --- a/src/app/src/auth/types.ts +++ b/src/app/src/auth/types.ts @@ -1,10 +1,11 @@ /** A token and expiration set */ export class Token { - /** The actual token */ - token: string = '' - - /** The expiration for the token */ - expiry: number = 0 + /** + * Create a new token + * @param token The actual token + * @param expiry The expiration for the token + */ + constructor (public token: string, public expiry: number) { } // eslint-disable-line no-useless-constructor /** Whether this token is currently valid */ isValid (): boolean { @@ -18,8 +19,8 @@ export class Session { profile: any = {} /** The user's ID token */ - id = new Token() + id = new Token('', 0) /** The user's access token */ - access = new Token() + access = new Token('', 0) }