Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* import {checkIfSessionActive} from '.../session.service';
* @CanActivate(checkIfSessionActive)
*/
export function checkIfSessionActive(next: ComponentInstruction, prev) : Promise<boolean> {
let injector = Injector.resolveAndCreate([SessionService,
ApiClientService,
TokenService,
LoggerService,
LocalStorageService,
UtilQueryStringService]);
let sessionService = injector.get(SessionService);
console.log('STEP 1a');
let fnPromise = function(resolve,reject) {
console.log('STEP 1b');
let isSessionActivePromise = sessionService.isSessionActive();
console.log('STEP 1c');
let fnHandleFail = function() {
reject();
/*
let injector = Injector.resolveAndCreate([Router]);
let router = injector.get(Router);
router.navigate( ['Login']);
*/
window.location.assign('http://localhost:7320/dist/dev/login');
};
isSessionActivePromise.then((isActive:boolean) => {
if(isActive) {
resolve(true);
} else {
fnHandleFail();
}
})
.catch((error) => {
/**
* @TODO We might be more indiscriminate about this.
* The session check can fail for server errors or lack of connectivity.
*/
fnHandleFail();
});
};
return new Promise<boolean>(fnPromise);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.