Convert to TS - final tweaks (#36)

This commit is contained in:
Daniel J. Summers 2019-11-22 08:51:32 -06:00
parent f8dad545bb
commit 7604976b98
2 changed files with 14 additions and 26 deletions

View File

@ -5,7 +5,7 @@ import { EventEmitter } from 'events'
import { Mutations, AppState } from '@/store/types' import { Mutations, AppState } from '@/store/types'
import Auth0Config from './auth0-variables' import Auth0Config from './auth0-variables'
import { Session } from './types' import { Session, Token } from './types'
// Auth0 web authentication instance to use for our calls // Auth0 web authentication instance to use for our calls
const webAuth = new auth0.WebAuth({ const webAuth = new auth0.WebAuth({
@ -79,10 +79,8 @@ class AuthService extends EventEmitter {
*/ */
setSession (authResult: Auth0DecodedHash) { setSession (authResult: Auth0DecodedHash) {
this.session.profile = authResult.idTokenPayload this.session.profile = authResult.idTokenPayload
this.session.id.token = authResult.idToken! this.session.id = new Token(authResult.idToken!, this.session.profile.exp * 1000)
this.session.id.expiry = this.session.profile.exp * 1000 this.session.access = new Token(authResult.accessToken!, authResult.expiresIn! * 1000 + Date.now())
this.session.access.token = authResult.accessToken!
this.session.access.expiry = authResult.expiresIn! * 1000 + Date.now()
localStorage.setItem(this.AUTH_SESSION, JSON.stringify(this.session)) 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 * Refresh this instance's session from the one in local storage
*/ */
refreshSession () { refreshSession () {
const emptySession = {
profile: {},
id: {
token: null,
expiry: null
},
access: {
token: null,
expiry: null
}
}
this.session = this.session =
localStorage.getItem(this.AUTH_SESSION) localStorage.getItem(this.AUTH_SESSION)
? JSON.parse(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? * Is there a user authenticated?
*/ */
isAuthenticated () { isAuthenticated () {
return this.session.id.isValid() return this.session && this.session.id && this.session.id.isValid()
} }
/** /**
* Is the current access token valid? * Is the current access token valid?
*/ */
isAccessTokenValid () { isAccessTokenValid () {
return this.session.access.isValid() return this.session && this.session.access && this.session.access.isValid()
} }
/** /**

View File

@ -1,10 +1,11 @@
/** A token and expiration set */ /** A token and expiration set */
export class Token { export class Token {
/** The actual token */ /**
token: string = '' * Create a new token
* @param token The actual token
/** The expiration for the token */ * @param expiry The expiration for the token
expiry: number = 0 */
constructor (public token: string, public expiry: number) { } // eslint-disable-line no-useless-constructor
/** Whether this token is currently valid */ /** Whether this token is currently valid */
isValid (): boolean { isValid (): boolean {
@ -18,8 +19,8 @@ export class Session {
profile: any = {} profile: any = {}
/** The user's ID token */ /** The user's ID token */
id = new Token() id = new Token('', 0)
/** The user's access token */ /** The user's access token */
access = new Token() access = new Token('', 0)
} }