import * as tnsOauth from './tns-oauth' ; import * as TnsOAuth from './tns-oauth-interfaces' ; export class AuthHelper { public credentials : TnsOAuth . ITnsOAuthCredentials ; public tokenResult : TnsOAuth . ITnsOAuthTokenResult ; constructor ( ) { this . tokenResult = tnsOauth . getTokenFromCache ( ) ; } public login ( successPage ? ) { return new Promise ( ( resolve , reject ) => { tnsOauth . loginViaAuthorizationCodeFlow ( this . credentials , successPage ) . then ( ( response ) => { this . tokenResult = response ; resolve ( response . accessToken ) ; } ) . catch ( ( er ) => { reject ( er ) ; } ) ; } ) ; } public refreshToken ( ) { return new Promise ( ( resolve , reject ) => { tnsOauth . refreshToken ( this . credentials ) . then ( ( response ) => { this . tokenResult = response ; resolve ( response . accessToken ) ; } ) . catch ( ( er ) => { reject ( er ) ; } ) ; } ) ; } public _logout ( successPage ? , cookieDomains ? ) { return new Promise < void > ( ( resolve , reject ) => { try { tnsOauth . logout ( cookieDomains , successPage ) ; this . tokenResult = null ; resolve ( ) ; } catch ( er ) { reject ( er ) ; } } ) ; } public accessTokenExpired ( ) { if ( this . tokenResult && this . tokenResult . accessTokenExpiration ) { return this . tokenResult . accessTokenExpiration < ( new Date ( ) ) ; } else { return true ; } } public refreshTokenExpired ( ) { if ( this . tokenResult && this . tokenResult . refreshTokenExpiration ) { if ( this . tokenResult . refreshTokenExpiration ) { return this . tokenResult . refreshTokenExpiration < ( new Date ( ) ) ; } else { return false ; } } else { return true ; } } }