Convert to TS - final tweaks (#36)
This commit is contained in:
parent
f8dad545bb
commit
7604976b98
@ -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()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user